j2ee. 스프링(Spring) 프레임워크 사용예제

예전에 정리했었던 내용입니다.



스프링MVC프로젝트 환경설정 예제

  • /WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 
version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
 <display-name>sample</display-name>
 <description>sample</description> 
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
   classpath*:configure/context-*.xml
  </param-value>
 </context-param> 
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>  
 <servlet>
  <servlet-name>module1-control</servlet-name>
  <servlet-class>
   org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>
    /WEB-INF/module1-mvc.xml
   </param-value>
  </init-param>  
 </servlet>
 <servlet-mapping>
  <servlet-name>module1-control</servlet-name>
  <url-pattern>/module1/*</url-pattern>
 </servlet-mapping> 
 <servlet>
  <servlet-name>module2-control</servlet-name>
  <servlet-class>
   org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>
    /WEB-INF/module2-mvc.xml
   </param-value>
  </init-param>  
 </servlet>
 <servlet-mapping>
  <servlet-name>module2-control</servlet-name>
  <url-pattern>/module2/*</url-pattern>
 </servlet-mapping>  
 <filter>
  <filter-name>Set Character Encoding</filter-name>
  <filter-class>com.unidocs.commonlib.filter.SetCharacterEncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
  <init-param>
   <param-name>listings</param-name>
   <param-value>false</param-value>
  </init-param>
  </filter>
  <filter-mapping>
   <filter-name>Set Character Encoding</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping> 
</web-app>
  • /WEB-INF/module1-mvc.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" 
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd"
>
 <context:component-scan base-package="com.gomdolinara.module1">
  <context:include-filter 
   type="annotation" 
   expression="org.springframework.stereotype.Controller"
   />
  <context:exclude-filter 
   type="annotation" 
   expression="org.springframework.stereotype.Service"
   />
  <context:exclude-filter 
   type="annotation" 
   expression="org.springframework.stereotype.Repository"
   />
 </context:component-scan>
 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
 <bean 
  id="viewResolver" 
  class="org.springframework.web.servlet.view.UrlBasedViewResolver"
  p:viewClass="org.springframework.web.servlet.view.tiles2.TilesView"
  />
</beans>
  • /WEB-INF/module2-mvc.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" 
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd"
>
 <context:component-scan base-package="com.gomdolinara.module2">
  <context:include-filter 
   type="annotation" 
   expression="org.springframework.stereotype.Controller"
   />
  <context:exclude-filter 
   type="annotation" 
   expression="org.springframework.stereotype.Service"
   />
  <context:exclude-filter 
   type="annotation" 
   expression="org.springframework.stereotype.Repository"
   />
 </context:component-scan>
 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
 <bean 
  id="viewResolver" 
  class="org.springframework.web.servlet.view.UrlBasedViewResolver"
  p:viewClass="org.springframework.web.servlet.view.tiles2.TilesView"
  />
</beans>
  • /WEB-INF/module1-tiles.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
 "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
 "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
 
<tiles-definitions>
<definition name="layout" template="/WEB-INF/jsp/layout.jsp">
 <put-attribute name="title">sample</put-attribute>
</definition>
<definition name="errorpage" extends="layout">
 <put-attribute name="content">/WEB-INF/jsp/errorpage.jsp</put-attribute>
</definition>
</tiles-definitions>
  • /WEB-INF/module2-tiles.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
 "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
 "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
 
<tiles-definitions>
<definition name="module2.errorpage" extends="layout">
 <put-attribute name="content">/WEB-INF/jsp/module2/errorpage.jsp</put-attribute>
</definition>
</tiles-definitions>
  • /src/configure/context-common.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" 
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-2.5.xsd
 http://www.springframework.org/schema/tx 
 http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
 http://www.springframework.org/schema/aop 
 http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
 >
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
  <property name="url" value="jdbc:mysql://127.0.0.1:3306/testdb?useUnicode=true"/>
  <property name="username" value="test"/>
  <property name="password" value="test"/>
 </bean>
 
 <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <property name="configLocation" value="classpath:/configure/sql-map-config.xml"/>
  <property name="dataSource" ref="dataSource"/>
 </bean>
 
 <bean id="transactionManager"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource"/>
 </bean>
 
 <tx:annotation-driven transaction-manager="transactionManager"/>
 
 <bean id="tilesConfigurer" 
  class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
  <property name="definitions">
   <list>
    <value>
     /WEB-INF/module1-tiles.xml,
     /WEB-INF/module2-tiles.xml,
    </value>
   </list>
  </property>
 </bean> 
 <context:component-scan base-package="com.gomdolinara">
  <context:exclude-filter 
   type="annotation" 
   expression="org.springframework.stereotype.Controller" 
   />
 </context:component-scan>
</beans>

AOP 예제

@Controller
@Aspect
public class SampleAspect{
 
/**
 * 
 * Model And View 를 리턴하는 컨트롤러 메소드가 종료된 후의 처리를 한다.
 *
 * - 이 클래스가 component-scan 되도록 설정되어야 한다.
 * - <aop:aspectj-autoproxy/> 이 설정되어야 한다.
 *
 * - 이 메소드의 포인트컷 : 
 *   패키지 : 모든 패키지
 *   클래스 : Controller 로 끝나는 이름의 클래스
 *   메소드 : 리턴형이 ModelAndView 인 메소드
 *
 */
@AfterReturning( 
 pointcut="execution(org.springframework.web.servlet.ModelAndView *..*Controller.*(..))", 
 returning="rtn" 
)
public void observeControllerProcess( JoinPoint joinPoint, Object rtn ){
 
 ModelAndView mv = ( ModelAndView ) rtn;
 
 //TODO : 필요한 작업을 수행
 
 System.out.println( 
  "CONTROLLER PROCESS FINALIZED - ENTER TO VIEW : " 
  + mv.getViewName() 
 );
}//method
}//class
  • 참고

웹 어플리케이션에서 Spring WebApplicationContext 획득

WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext( 
    getServletContext() //GenericServlet::getServletContext();
);

다른글 읽기