The previous period has been a more in-depth study of spring's transactional configuration, which, although configured for spring's transaction configuration, has not been clearly understood. Through this learning to find spring's transaction configuration as long as the idea is clear, or better grasp.
Summarized as follows:
In the spring configuration file, there are always three components in the transaction configuration, namely the DataSource, TransactionManager, and proxy mechanisms, regardless of the configuration, the general change is only the agent mechanism of this part.
DataSource, TransactionManager These two parts only according to the data access mode changes, such as using hibernate for data access, DataSource is actually sessionfactory, The implementation of TransactionManager is Hibernatetransactionmanager.
The following figure:
Based on the different agent mechanism, the configuration of five spring transactions is summarized, and the configuration files are as follows:
The first way: Each bean has an agent
[HTML] View plain copy? <?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "http://" Www.springframework.org/schema/beans " xmlns:xsi=" http://www.w3.org/2001/ Xmlschema-instance " xmlns:context=" http://www.springframework.org/schema/ Context " xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP " xsi:schemalocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ spring-beans-2.5.xsd http:// www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http:// Www.springframework.org/schema/aop/spring-aop-2.5.xsd "> < Bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" > <property name= "configlocation" value= " Classpath:hibernate.cfg.xml " /> <property name= "ConfigurationClass" value= "org.hibernate.cfg.AnnotationConfiguration" /> </bean> <!-- Defining the transaction manager (declarative transaction) --> <bean id = "TransactionManager" class= " Org.springframework.orm.hibernate3.HibernateTransactionManager "> <property name= "Sessionfactory" ref= "Sessionfactory" /> </bean> <!-- configuration dao --> <bean id= "Userdaotarget" class= "Com.bluesky.spring.dao.UserDaoImpl" > < Property name= "Sessionfactory" ref= "Sessionfactory" /> </bean> <bean id= "Userdao " class=" Org.springframework.transaction.interceptor.TransactionProxyFactoryBean "> <!-- Configuration transaction manager --> <property name= " TransactionManager " ref=" TransactionManager " /> <property name= "target" ref= "UserDaoTarget" /> <property name= " Proxyinterfaces " value=" Com.bluesky.spring.dao.GeneratorDao " /> <!-- Configuration transaction Properties --> <property name= "Transactionattributes" > <props> <prop key= "*" >PROPAGATION_REQUIRED</prop> </props> </property> </bean> </beans>
The second way: all beans share a proxy base class
[HTML] View plain copy? <?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "http://" Www.springframework.org/schema/beans " xmlns:xsi=" http://www.w3.org/2001/ Xmlschema-instance " xmlns:context=" http://www.springframework.org/schema/ Context " xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP " xsi:schemalocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ spring-beans-2.5.xsd http:// www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http:// Www.springframework.org/schema/aop/spring-aop-2.5.xsd "> < Bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" > <property name= "configlocation" value= " Classpath:hibernate.cfg.xml " /> <property name= "ConfigurationClass" value= "org.hibernate.cfg.AnnotationConfiguration" /> </bean> <!-- Defining the transaction manager (declarative transaction) --> <bean id = "TransactionManager" class= " Org.springframework.orm.hibernate3.HibernateTransactionManager "> <property name= "Sessionfactory" ref= "Sessionfactory" /> </bean> <bean id= "Transactionbase" class= "Org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init= "true" abstract= "true" > <!-- Configure the transaction manager --> <property Name= "TransactionManager" ref= "TransactionManager" /> <!-- Configuration transaction Properties --> <property name= "Transactionattributes" > <props> <prop key= "*" >PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- Configure DAO --> <bean id= "Userdaotarget" class= " Com.bluesky.spring.dao.UserDaoImpl "> <property name=" Sessionfactory " ref= "Sessionfactory" /> </bean> <bean id= "Userdao" parent= "Transactionbase" > <property name= "Target" ref= "Userdaotarget" /> </bean> </beans>
The third way: using interceptors
[HTML] view plain copy? <