<ref local="companyManageProxy" />
</property>
</bean>
Spring配置事务在DAO层和业务逻辑层(2)
时间:2011-03-26
二.把事务放置在了Service层:
<!—jtaTransactionManager-->
<bean id="jtaTransactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager" />
<!—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.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.vstsoft.querycompany.dao.CompanyDAO</value>
</property>
<property name="target">
<ref bean="companyDAOTarget" />
</property>
</bean>
<!—业务层接口实现,把DAO注入到Service里面-->
<bean name="companyManageTarget"
class="com.vstsoft.querycompany.service.impl.CompanyManageTarget">
<property name="companyDAO">
<ref bean="companyDAOProxy" />
</property>
</bean>
<!—spring代理业务层的事务管理-->
<bean id="companyManageProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="jtaTransactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="set*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</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">
<ref local="companyManageProxy" />
</property>
</bean>
在service层的接口实现CompanyManageImpl里面有个setData方法:按顺序执行数据查询,数据删除,数据插入数据库行为,如果哪一步出异常(运行时异常),事务回滚,只有所有行为都没成功,事务提交。 |