Post

๐Ÿฅœ [Spring] Two Architecture Layer & Bean ์„ค์ •๋ฌธ์„œ ๋ชจ๋“ˆํ™”

Two Architecture Layer

image-38

Two Architecture Layer ๋Š”

presentation layer์™€ business logic layer ์ด๋‹ค.

Presentation Layer์™€ Business Logic Layer๋ฅผ ์‚ดํŽด๋ณด๋ฉด

Business Logic Layer๋Š” ์šฐ๋ฆฌ๊ฐ€ ์•Œ๊ณ  ์žˆ๋Š” ๋ถ€๋ถ„์ด๋‹ค.

Presentation Layer๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์ด๋Š” ๊ฒฐ๊ณผํŽ˜์ด์ง€๋ฅผ ๋ณด๋‚ด์ค€๋‹ค.


๋™์ž‘ ์ˆœ์„œ

Container ๋Š” ์‹œ์ž‘ํ•˜์ž๋งˆ์ž web.xml ์„ ์ฝ์–ด๋“ค์ธ๋‹ค.

๊ทธ ์ค‘ ๋จผ์ € ์„ ์ฐพ์•„ `servletContext` ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

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>
This post is licensed under CC BY 4.0 by the author.