JSP学习笔记(五十九):简化Spring的配置文件
受这篇文章(主题:Spring 事务简化配置)的启发,我对原有的Spring配置文件进行了修改,原配置文件:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/jdbc.properties" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName}">
</property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="jdbcTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<bean id="txProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="jdbcTransactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
添加dao,service,action的配置如下:
<bean id="userDAO" class="com.modou.dao.jdbc.UserJdbcDAO">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<bean id="userTarget"
class="com.modou.service.jdbc.UserServiceJdbcImpl">
<property name="userDAO">
<ref local="userDAO" />
</property>
</bean>
<bean id="userService" parent="txProxyTemplate">
<property name="target">
<ref local="userTarget" />
</property>
</bean>
<bean id="userAction"
class="com.modou.web.action.userListAction">
<property name="userService">
<ref local="userService" />
</property>
</bean>
修改后的配置文件如下:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/jdbc.properties" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName}">
</property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="jdbcTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="jdbcTransactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>*Service</value>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
修改后,添加dao,service,action的配置如下:
<bean id="userDAO" class="com.modou.dao.jdbc.UserJdbcDAO"
autowire="byName" />
<bean id="userService"
class="com.modou.service.jdbc.UserServiceJdbcImpl" autowire="byName" />
<bean id="userAction"
class="com.modou.web.action.userListAction"
autowire="byName" />
比原来的简单了不少,看起来也更清晰,这样所有以Service结尾的bean都会自动受代理事务的管理。