๐ฅ [Spring] Two Architecture Layer & Bean ์ค์ ๋ฌธ์ ๋ชจ๋ํ
Two Architecture Layer
Two Architecture Layer
๋
presentation layer์ business logic layer ์ด๋ค.
Presentation Layer
์ Business Logic Layer
๋ฅผ ์ดํด๋ณด๋ฉด
Business Logic Layer
๋ ์ฐ๋ฆฌ๊ฐ ์๊ณ ์๋ ๋ถ๋ถ์ด๋ค.
Presentation Layer
๋ ์ฌ์ฉ์์๊ฒ ๋ณด์ด๋ ๊ฒฐ๊ณผํ์ด์ง๋ฅผ ๋ณด๋ด์ค๋ค.
๋์ ์์
Container
๋ ์์ํ์๋ง์ web.xml ์ ์ฝ์ด๋ค์ธ๋ค.
๊ทธ ์ค ๋จผ์
1
2
3
4
5
//web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean/businessLogicBean.xml</param-value>
</context-param>
๊ฑฐ๊ธฐ์ bean/businessLogicBean.xml
์ wiring
ํ๋ค.
๋ง ๊ทธ๋๋ก Business Logic
์ ํด๋นํ๋ bean
์ค์ ๋ฌธ์๋ฅผ ์๋ฏธํ๋ค.
๊ทธ ๋ค์ web.xml
์
<servlet>
ํ๊ทธ๋ฅผ ์ฐพ๊ณ wiring
ํ๋ค.
servlet
์ ์ด๊ธฐํ ์์
์ธ init()
์ ํธ์ถํ ๋ ๋์๊ฐ๋ค.
<init-param>
์ ํตํด bean/presentationBean.xml
์ wiring
ํ๋ค.
์ bean
์ค์ ๋ฌธ์๋ Presentation Layer
์ ํด๋นํ๋ bean์ ์์ฑํ๋ค.
Bean ์ค์ ๋ฌธ์์ ๋ชจ๋ํ
1
2
3
4
5
6
7
8
9
10
11
12
//web.xml
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--classpath ์๋ถ์ด๋ฉด webapp ๋ฐ์ ์ฐพ๋๋ค classpath๋ฅผ ๋ถ์ฌ์ผ webapp ์์์ ์ฐพ๋๋ค -->
<!--servlet๋ง๋๋ ์ฃผ๋ฌธ์์์ ๋น์ ๋ง๋๋ ์ฃผ๋ฌธ์ wireing -->
<param-value>classpath:bean/presentationBean.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
classpath๋ฅผ ๋ถ์ฌ์ฃผ์๋๋ฐ webapp ๋ฐ์ ์๋ ํ์ผ์ ์ฐพ์ ๋๋ classpath๋ฅผ ๋ถ์ฌ์ค์ผ ํ๋ค.
์์ ์ด๋ฌํ ์์ ์ bean ์ค์ ๋ฌธ์์ ๋ชจ๋ํ ์ด๋ค.
์ด์ ๊น์ง๋ ๋ชจ๋ bean ์์ฑ์ ํ๋์ bean ์ค์ ๋ฌธ์์ ๋ฃ์ด๋์๋ค.
ํ์ง๋ง ์ด์ ๋ถํฐ Layer ๋ณ๋ก bean ์ค์ ๋ฌธ์๋ฅผ ๋์๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
//web.xml
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
์์ ์ฝ๋๋ ํ๊ธ์ฒ๋ฆฌ๋ฅผ ํ๋ ์ฝ๋์ด๋ค.
web.xml์ ๋ฃ์ด๋์๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<!-- businessLogicBean.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--My Batis -->
<context:property-placeholder location="classpath:config/dbconn.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.oracle.driver}"/>
<property name="url" value="${jdbc.oracle.url}"/>
<property name="username" value="${jdbc.oracle.username}"/>
<property name="password" value="${jdbc.oracle.password}"/>
</bean>
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:config/SqlMapConfig.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactoryBean"/>
</bean>
<context:component-scan base-package="com.service.spring" />
</beans>
๋น์ฆ๋์ค ๋ก์ง์ ๋ด๋นํ๋ bean ์ค์ ๋ฌธ์์ด๋ค.
ItemDAO์ ItemCatalogImpl์ Annotation์ผ๋ก ์๋์ผ๋ก bean์ด ๋ฑ๋ก๋์ด ์๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- presentationBean.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--Presentation bean -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="com.service.spring.controller" />
</beans>
Presentation Layer์์ ์ฌ์ฉํ๋ bean์ ์ค์ ํ๋ ๋ฌธ์์ด๋ค.
๊ฒฐ๊ณผํ์ด์ง์ ๋ฌผ๋ฆฌ์ ์์น(prefix)์ ํ์ฅ์(suffix)๋ฅผ ์ค์ ํ ์ ์๋ค.
viewResolver๋ผ๊ณ ๋ ํ๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
<!-- sqlMapConfig.xml -->
<environments default="DEVELOPMENT">
<environment id="DEVELOPMENT">
<transactionManager type="JDBC"/>
<dataSource type="UNPOOLED">
<property name="driver" value="${jdbc.oracle.driver}"/>
<property name="url" value="${jdbc.oracle.url}"/>
<property name="username" value="${jdbc.oracle.username}"/>
<property name="password" value="${jdbc.oracle.password}"/>
</dataSource>
</environment>
</environments>
1
2
3
4
5
6
7
<!-- Itemservice.xml (bean ์ค์ ๋ฌธ์) -->
<bean id = "dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.oracle.driver}"/>
<property name="url" value="${jdbc.oracle.url}"/>
<property name="username" value="${jdbc.oracle.username}"/>
<property name="password" value="${jdbc.oracle.password}"/>
</bean>