Spring configuration transactions at the DAO Layer and service layer
Last Update:2018-07-21
Source: Internet
Author: User
"Reprinted from Http://www.blogjava.net/hk2000c/archive/2007/11/01/157508.html"
"Thank the original author: hk2000c"
Spring implements declarative transaction management through AOP. The spring transaction agent is typically set up through Transactionproxyfactorybean. We need a target object wrapped in the transaction agent. This target object is typically a bean of a normal Java object. When we define Transactionproxyfactorybean, we must provide an associated Platformtransactionmanager reference and transaction attributes. The transaction attribute contains the transaction definition described above.
Platformtransactionmanager:
Hibernatetransactionmanager needs a sessionfactory reference.
Jtatransactionmanager
A To place a transaction on the DAO Layer:
Java code <!-hibernateTransactionManager--> <bean id= "TransactionManager" class= " Org.springframework.orm.hibernate3.HibernateTransactionManager "> <property name= "Sessionfactory" > <ref local= "Sessionfactory" /> </property> </bean> <!-dao Layer Interface Implementation--> <bean id= "Companydaotarget" class= " Com.vstsoft.querycompany.dao.impl.CompanyDAOImpl "> <property name= "Sessionfactory" > <ref local= "SessionfactoRy " /> </property> </bean> <!-spring to the DAO layer's transaction Agent--> <bean id= "Companydaoproxy" class= "Org.springframework.transaction.interceptor.TransactionProxyFactoryBean" > <property name= "TransactionManager" > <ref Bean= "TransactionManager" /> </property> <property name= "target" > <ref local= " Companydaotarget " /> </property> &NBSP;&NBSP;&NBSP;&NBSP; <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> <!-Business layer Interface implementation, Inject DAO into service--> <bean name= "Companymanagetarget" class= "Com.vstsoft.querycompany.service.impl.CompanyManageTarget" > <property name= "Companydao" > <ref bean= "CompanyDAOProxy" / > </property> </bean> <!-spring to service layer agent--> <bean id= "Companymanageproxy" class= "Org.springframework.aop.framework.ProxyFactoryBean" > <property name= "Proxyinterfaces" > &nBsp; <value>com.vstsoft.querycompany.service.companymanage</value > </property> <property name= "Target" > <ref bean= "Companymanagetarget" /> </property> </bean> <!- Configure struts access, inject the service layer into the action--> <bean name= "/company" class= "Com.vstsoft.querycompany.web.action.CompanyAction" singleton= "false" > <property name= "Companymanage" > <ref local= " Companymanageproxy " /> </property> </bean>
Two The transaction is placed at the service level:
Java code <!-jtaTransactionManager--> <bean id= "Jtatransactionmanager" class= " Org.springframework.transaction.jta.JtaTransactionManager " /> <!-dao layer Interface Implementation--> <bean id= "Companydaotarget" class= " Com.vstsoft.querycompany.dao.impl.CompanyDAOImpl "> <property name= "Sessionfactory" > <ref local= "Sessionfactory" /> </property> </bean> <!-spring proxy--> for DAO layer <bean id= "Companydaoproxy" class= " Org.springframework.aop.framework.ProxyFactoryBean "> <property name= "Proxyinterfaces" > <value> com.vstsoft.querycompany.dao.companydao</value>