'2008/05'에 해당되는 글 1건

스프링 어플리케이션 컨텍스트 설정 :: 2008/05/15 23:03

스프링 프레임워크의 버전별 어플리케이션 컨텍스트 설정에 대한 간단한 샘플이다.

1. Spring 1.2에서 DTD 기반의 XML 설정을 지원한다.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
 "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
 <!-- ========================= DATABASE ============================ -->
 <bean id="propertyConfigurer" 
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
   <list>
    <value>jdbc.properties</value>
   </list>
  </property>
 </bean>
 <bean id="dataSource" 
  class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="${jdbc.driverClassName}" />
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.username}" />
  <property name="password" value="${jdbc.password}" />
 </bean>
 <!-- ========================= TRANSACTION ======================== -->
 <bean id="transactionManager"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
 </bean>
 <!-- ========================= O/R BROKER ========================= -->
 <bean id="exampleBroker" 
  class="org.springmodules.orm.orbroker.BrokerFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="configLocation"
   value="classpath:net/java2go/dao/maps/ExampleBrokerSql.xml" />
 </bean>
 <!-- ========================= DAO ================================ -->
 <bean id="exampleDao" class="net.java2go.dao.IBatisExampleDAOImpl">
  <property name="broker" ref="examplenBroker" />
 </bean>
 
 <!-- ========================= SERVICE ============================ -->
 <bean id="exampleService" class="net.java2go.service.ExampleServiceImpl">
  <property name="exampleDao" ref="exampleDao" />
 </bean>
</beans>

2. Spring 2.0에서는 DTD 기반 설정뿐만 아니라 XML 스키마 기반의 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:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
 <!-- ========================= DATABASE ============================ -->
 <bean id="propertyConfigurer"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
   <list>
    <value>jdbc.properties</value>
   </list>
  </property>
 </bean>
 <bean id="dataSource"
  class="com.mchange.v2.c3p0.ComboPooledDataSource"
  destroy-method="close">
  <property name="driverClass" value="${jdbc.driverClassName}" />
  <property name="jdbcUrl" value="${jdbc.url}" />
  <property name="user" value="${jdbc.username}" />
  <property name="password" value="${jdbc.password}" />
  <property name="initialPoolSize"
   value="${jdbc.initialPoolSize}" />
  <property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
  <property name="minPoolSize" value="${jdbc.minPoolSize}" />
  <property name="acquireIncrement"
   value="${jdbc.acquireIncrement}" />
  <property name="acquireRetryAttempts"
   value="${jdbc.acquireRetryAttempts}" />
 </bean>
 <!-- ========================= TRANSACTION ======================== -->
 <!-- transaction -->
 <bean id="transactionManager"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
 </bean>
 <!--tx:annotation-driven transaction-manager="transactionManager" /-->
 <!-- ========================= ASPECT ============================= -->
 <tx:advice id="txAdvice"
  transaction-manager="transactionManager">
  <tx:attributes>
   <tx:method name="*Audit**" propagation="NOT_SUPPORTED" />
   <tx:method name="get*" propagation="SUPPORTS"
    read-only="true" rollback-for="Throwable" />
   <tx:method name="retrieve*" propagation="SUPPORTS"
    read-only="true" rollback-for="Throwable" />
   <tx:method name="verify*" propagation="SUPPORTS"
    read-only="true" rollback-for="Throwable" />
   <tx:method name="check*" propagation="SUPPORTS"
    read-only="true" rollback-for="Throwable" />
   <tx:method name="*" propagation="REQUIRED"
    read-only="false" rollback-for="Throwable" />
  </tx:attributes>
 </tx:advice>
 <bean id="loggingAspect"
  class="net.java2go.aspect.LoggingAspect">
  <property name="thresholdInMills" value="3000" />
 </bean>
 <aop:config>
  <aop:pointcut id="servicePointcut"
   expression="execution(* net.java2go.service..*Service.*(..))" />
  <aop:pointcut id="daoPointcut"
   expression="execution(* net.java2go.dao..*DAO.*(..))" />
  <aop:pointcut id="serviceDaoPointcut"
   expression="execution(* net.java2go.service..*Service.*(..))
    or execution(* net.java2go.dao..*DAO.*(..))" />
  <aop:advisor advice-ref="txAdvice"
   pointcut-ref="servicePointcut" />
  <aop:aspect ref="loggingAspect">
   <aop:around pointcut-ref="servicePointcut"
    method="logProfiling" />
   <aop:after-throwing pointcut-ref="serviceDaoPointcut"
    throwing="ex" method="logException" />
  </aop:aspect>
 </aop:config>
 <!-- ========================= IBATIS ============================= -->
 <bean id="sqlMapClient" 
  class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <property name="configLocation">
   <value>sqlmap-config.xml</value>
  </property>
  <property name="dataSource" ref="dataSource" />
 </bean>
 <!-- ========================= DAO ================================ -->
 <bean id="exampleDao" class="net.java2go.dao.IBatisExampleDAOImpl">
  <property name="sqlMapClient" ref="sqlMapClient" />
 </bean>
 
 <!-- ========================= SERVICE ============================ -->
 <bean id="exampleService" class="net.java2go.service.ExampleServiceImpl">
  <property name="exampleDao" ref="exampleDao" />
 </bean>
</beans>

3. Spring 2.5에서 더 많은 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:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 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/aop
  http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 <!-- ========================= DATABASE ============================ -->
 <context:property-placeholder location="jdbc.properties" />
</beans>