在spring基础文件中配置数据源,事务管理和事务管理代理bean的。
<!-- ======================== Properties ======================== -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:conf/configure.properties</value>
</list>
</property>
</bean>
<!-- ======================== data configure ========================== -->
<!-- Apache Database Connection Pool -->
<bean id="dataSource"<!--BasicDataSource类的别名-->
class="org.apache.commons.dbcp.BasicDataSource""<!--BasicDataSource类的别名所指向的真正类-->
destroy-method="close">
<property name="driverClassName">"<!--对应到BasicDataSource类中的属性-->
<value>${db.driver}</value>"<!--该属性值绑定到BasicDataSource类中的属性此处为property中name多对应的属性-->
</property>
<property name="url">
<value>${db.url}</value>
</property>
<property name="username">
<value>${db.user}</value>
</property>
<property name="password">
<value>${db.password}</value>
</property>
<property name="initialSize">
<value>15</value>
</property>
<property name="maxActive">
<value>30</value>
</property>
<property name="maxIdle">
<value>30</value>
</property>
<property name="minIdle">
<value>5</value>
</property>
</bean>
<!-- Transaction manager for a single JDBC DataSource DAO IoC -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource" />
</property>
<!--
- A parent bean definition which is a base definition for transaction proxies.
- It's marked as abstract, since it's not supposed to be instantiated itself.
- We set shared transaction attributes here, following our naming patterns.
- The attributes can still be overridden in child bean definitions.
-
-->
<bean id="baseTransactionProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true"><!-- 为了避免ApplicationContext对它预先的初始化 -->
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
</bean>
如果有很多的bean的某些定义都是类似的(比如对于transaction的定义),那么可以给他们的定义作一个模板。
使用parent可以实现这一点。(注意parent并非意味着两个bean之间存在着java的继承关系,只是表示他们的定义之间存在着共同的部分)。
child bean使用parent来继承模板的定义,同样还可以覆盖模板的定义。
很多时候模板并不需要被实例化,为了避免ApplicationContext对它预先的初始化,一般设置abstract="true"
为了提高事务代理的复用程度,没有在baseTransactionProxy设置target属性。此时设置abstract="true"
下面以一个用户管理UserInfo 用户管理模块为例,设置模块中各个bean的依赖注入和事务的管理
首先定义service实现类
<!-- user target bean -->
<bean id="userTarget"
class="com.zhjy.gmp.service.impl.UserInfoServiceImpl">
<property name="userDAO">
<ref local="userDAO" />
</property>
<!-- 部门DAO -->
<property name="orgDAO">
<ref local="orgInfoDAO" />
</property>
<!-- 角色DAO -->
<property name="roleDAO">
<ref local="roleDAO" />
</property>
</bean>
这里注入了UserInfoServiceImpl所需要的三个dao实例。
接下来对该service进行事务管理的配置
<!-- user service bean -->
<bean id="userService" parent="baseTransactionProxy">
<property name="target">
<ref bean="userTarget" />
</property>
</bean>
此处通过target属性将userTarget实例注入到事务代理中。
最后定义dao实例
<!-- user dao bean -->
<bean id="userDAO" class="com.zhjy.gmp.dao.impl.UserInfoDAOImpl">
<property name="dataSource">
<ref bean="dataSource" /><!-- ref bean 与 ref local 的区别,后者表示从本地的(同一个)xml文件中来寻找bean,前者是全局范围内寻找bean。 -->
</property>
</bean>