์๋ฐ ํ๋ก๊ทธ๋จ์ ์ค์ ์ ํฌ๊ฒ xml ๋ฐฉ์, ์ด๋ ธํ ์ด์ ๋ฐฉ์์ผ๋ก ๋๋๋ค. ์คํ๋ง์์๋ ๋๋ถ๋ถ ์ด๋ ธํ ์ด์ ๊ณผ ์๋ฐ ํ์ผ์ ์ด์ฉํ ์ค์ ์ ํ๋ค.
servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<bean id="/notice/list" class="com.newlecture.web.controller.notice.ListController">
<!--
<property name="noticeService" ref="noticeService" />
-->
</bean>
//์ดํ ์๋ต
</beans>
์ด๋ ธํ ์ด์ ์ผ๋ก ์ฃผ์ ํ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ๋ ์ด์ ํ์์น ์์ <property> ํ๊ทธ๋ฅผ ์ญ์ ํ๋ค. ์ด ์ํ๋ก ์คํํ๋ฉด ListController์์ ๋ ํฌ์ธํฐ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค. ์ธํฐ๋ฅผ ์ด์ฉํ ์ฃผ์ ์ด ๋์ง ์์๊ธฐ ๋๋ฌธ์ด๋ค.
context namespace๋ฅผ ์ถ๊ฐํ๊ณ <context:annotation-config /> ํ๊ทธ๋ฅผ ์ถ๊ฐํ๋ค. ์ด ํ๊ทธ๊ฐ ์์ด์ผ ์ด๋ ธํ ์ด์ ์ผ๋ก ์ค์ ํ๋ค๋ ์ธ์์ ํ๊ฒ ๋๋ค.
ListController.java
public class ListController implements Controller{
@Autowired
private NoticeService noticeService;
/*
@Autowired
public void setNoticeService(NoticeService noticeService) {
this.noticeService = noticeService;
System.out.println("์ธํฐ์์ ์ด๋ค ํ๋์ ๋ฐ์ธ๋ฉ ํด์ผ ํ๋ฉด ์ฌ๊ธฐ์ autowired");
}
*/
NoticeService ํ๋์ @Autowired ์ด๋ ธํ ์ด์ ์ ์ถ๊ฐํ๋ค. ๋ง์ฝ ์ธํฐ์์ ์ด๋ค ๋์ ๋ฐ์ธ๋ฉ์ด ํ์ํ๋ค๋ฉด ์ธํฐ์ ์ด๋ ธํ ์ด์ ์ ๋ถ์ผ ์๋ ์๊ฒ ์ผ๋, ๊ทธ๋ด ํ์๊ฐ ์์ผ๋ฉด ํ๋์ ์ถ๊ฐํ๋ ๊ฒ์ด ๊ฐํธํ๋ค.
service-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:annotation-config />
<bean id="noticeService" class="com.newlecture.web.service.jdbc.JDBCNoticeService">
<!-- <property name="dataSource" ref="dataSource"/> -->
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@14.35.198.58:1522/orcl" />
<property name="username" value="USER01" />
<property name="password" value="1111" />
</bean>
</beans>
JDBCNoticeService.java
public class JDBCNoticeService implements NoticeService{
@Autowired
private DataSource dataSource;
service-context.xml, JDBCNoticeService.java๋ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์์ ํ๋ค. property๋ฅผ ์ง์ฐ๊ณ @Autowired๋ฅผ ํ๋์ ์ถ๊ฐํ๋ค.
๋ด๋ ์ฒ ์คํ๋ง ํ๋ ์์ํฌ ๊ฐ์๋ฅผ ๋ฃ๊ณ ์ ๋ฆฌํ ๊ฒ์๊ธ์ ๋๋ค.