Spring配置事务在DAO层和业务逻辑层
时间:2011-03-26
Spring通过AOP实现声明式事务管理。通常通过TransactionProxyFactoryBean设置Spring事务代理。我们需要一个目标对象包装在事务代理中。这个目标对象一般是一个普通Java对象的bean。当我们定义TransactionProxyFactoryBean时,必须提供一个相关的 PlatformTransactionManager的引用和事务属性。 事务属性含有上面描述的事务定义。
PlatformTransactionManager:
HibernateTransactionManager需要一个SessionFactory的引用
JtaTransactionManager
一.把事务放置在了DAO层:
<!—hibernateTransactionManager-->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!—DAO层接口实现-->
<bean id="companyDAOTarget"
class="com.vstsoft.querycompany.dao.impl.CompanyDAOImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!—spring对DAO层的事务代理-->
<bean id="companyDAOProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref local="companyDAOTarget" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="find*">
PROPAGATION_REQUIRED,readOnly
</prop>
</props>
</property>
</bean>
<!—业务层接口实现,把DAO注入到Service里面-->
<bean name="companyManageTarget"
class="com.vstsoft.querycompany.service.impl.CompanyManageTarget">
<property name="companyDAO">
<ref bean="companyDAOProxy" />
</property>
</bean>
<!—spring对Service层的代理-->
<bean id="companyManageProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.vstsoft.querycompany.service.CompanyManage</value>
</property>
<property name="target">
<ref bean="companyManageTarget" />
</property>
</bean>
<!—配置struts访问,把service层注入到action里面-->
<bean name="/company"
class="com.vstsoft.querycompany.web.action.CompanyAction" singleton="false">
<property name="companyManage">
|