SSH Deep Adventures (vi) Five Ways to-----Spring transaction configuration in layman's sense

Source: Internet
Author: User
Tags call back

This time in learning ssh in the spring architecture, spring's transaction configuration made a concrete summary. The transaction configuration for spring is just a matter of staying in the listening and speaking stages and summarizing. The overall control. Through this study found that spring's business configuration only to clarify the idea, or better grasp.

Summary for example the following:

In the spring configuration file, the transaction configuration is always composed of three components, each of which is DataSource, TransactionManager, and agent mechanism. Regardless of the configuration method. The general change is only part of the agency mechanism.

DataSource, TransactionManager These two parts will only be based on data access to change, for example, using Hibernate for data interview. DataSource is actually sessionfactory. The implementation of TransactionManager is Hibernatetransactionmanager.

In detail for example with:

Based on the proxy mechanism, the configuration of five spring transactions is summarized, and the configuration files are as follows:

The first way: Each bean has a proxy

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ><?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://w Ww.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:h Ibernate.cfg.xml "/> <property name=" ConfigurationClass "value=" Org.hibernate.cfg.AnnotationConfiguration " /> </bean> <!--define transaction manager (declarative transactions)-- <bean id= "TransactionManager" class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" > <property name= "Sessionfactory" ref= "Sessionfactory"/> </bean> <!--configuration Dao--<bea n id= "Userdaotarget" class= "Com.bluesky.spring.dao.UserDaoImpl" > <property name= "sessionfactory" ref= "session Factory "/> </bean> <bean id=" Userdao "class=" org.springframework.transaction.interceptor.t Ransactionproxyfactorybean "> <!--configuration transaction Manager-<property name=" TransactionManager "ref= "TransactionManager"/> <property name= "target" ref= "Userdaotarget"/> <property name= "p Roxyinterfaces "value=" Com.bluesky.spring.dao.GeneratorDao "/> <!--Configuring transaction Properties--<property name= "Transactionattributes" > <props> <prop key= "*" >propagation_required</prop& Gt </props> </property> </bean> </beans></span></span>




Another way: All beans share a proxy base class

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ><?

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/cont Ext http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.or G/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.AnnotationCo Nfiguration "/> </bean> <!--define 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.TransactionProxyFactoryB Ean "lazy-init=" true "abstract=" true "> <!--Configuration transaction Manager--<property name=" Transac Tionmanager "ref=" TransactionManager "/> <!--Configuring transaction Properties--<property name=" transactionattribut Es "> <props> <prop key=" * ">PROPAGATION_REQUIRED</prop> &lt ;/props> </property> </bean> <!--configuration Dao--<bean id= "Userdaotarget" cl ass= "Com.bluesky.spring.dao.UserDaoImpl" > <property name= "sessionfactory" ref= "Sessionfactory"/> </ Bean> <bean Id= "Userdao" parent= "Transactionbase" > <property name= "target" ref= "Userdaotarget"/> </bean& Gt;</beans></span></span></span>

Third Way: Using interceptors


<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;"    ><?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> <! --Define transaction manager (declarative transaction)--<bean id= "TransactionManager" class= "org.springframework.orm.hibernate3.Hibernate TransactionManager "> <property name=" sessionfactory "ref=" sessionfactory "/> </bean> <b          Ean id= "Transactioninterceptor" class= "Org.springframework.transaction.interceptor.TransactionInterceptor" > <property name= "TransactionManager" ref= "TransactionManager"/> <!--Configuring transaction Properties--<p Roperty name= "Transactionattributes" > <props> <prop key= "*" >propagation_requi red</prop> </props> </property> </bean> <bean class= "org.sp              Ringframework.aop.framework.autoproxy.BeanNameAutoProxyCreator "> <property name=" Beannames ">          <list>        <value>*Dao</value> </list> </property> <property name= "in              Terceptornames "> <list> <value>transactionInterceptor</value> </list> </property> </bean> <!--Configuring DAO--<bean id= "Userdao" class = "Com.bluesky.spring.dao.UserDaoImpl" > <property name= "sessionfactory" ref= "Sessionfactory"/> </bea N></beans></span></span></span>
Fourth Way: Interceptors configured with the TX tag
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;"    ><?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 "xmlns:tx=" http://www.springframework.org/schema/ Tx "Xsi:schemalocation=" Http://www.springframework.org/schema/beans http://www.springframework.org/schema/be Ans/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/s Chema/aop/spring-aop-2.5.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/s Pring-tx-2.5.xsd "> <context:annotation-config/> <Context:component-scan base-package= "Com.bluesky"/> <bean id= "sessionfactory" class= "Org.springfram Ework.orm.hibernate3.LocalSessionFactoryBean "> <property name=" configlocation "value=" classpath:hibernate.c Fg.xml "/> <property name=" ConfigurationClass "value=" Org.hibernate.cfg.AnnotationConfiguration "/> & Lt;/bean> <!--define transaction manager (declarative transaction)--<bean id= "TransactionManager" class= "ORG.SPRINGFRAMEWORK.O Rm.hibernate3.HibernateTransactionManager "> <property name=" sessionfactory "ref=" Sessionfactory "/> &lt            ;/bean> <tx:advice id= "Txadvice" transaction-manager= "TransactionManager" > <tx:attributes> <tx:method name= "*" propagation= "REQUIRED"/> </tx:attributes> </tx:advice> <ao p:config> <aop:pointcut id= "interceptorpointcuts" expression= "Execution (* com.bluesky.spring.dao.* .*(..))" /> &LT;aop:advisor advice-ref= "Txadvice" pointcut-ref= "interceptorpointcuts"/> </aop:config> </beans></span></span></span>

Fifth way: Full annotation
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ><?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 "xmlns:tx=" http://www.springframework.org/schema/tx "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 Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "> <co Ntext:annotation-config/> <context:component-scan base-package= "Com.bluesky"/> <tx:annotation-driven t ransaction-manager= "TransactionManager"/> <bean id= "seSsionfactory "class=" Org.springframework.orm.hibernate3.LocalSessionFactoryBean "> <property na Me= "Configlocation" value= "Classpath:hibernate.cfg.xml"/> <property name= "ConfigurationClass" value= "org.h Ibernate.cfg.AnnotationConfiguration "/> </bean> <!--define transaction manager (declarative transaction)--<bean id=" Transac Tionmanager "class=" Org.springframework.orm.hibernate3.HibernateTransactionManager "> <property name=" s Essionfactory "ref=" sessionfactory "/> </bean> </beans></span></span></span>




At this point, you need to add @transactional annotations on the DAO, such as the following:
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >package Com.bluesky.spring.dao;import Java.util.list;import Org.hibernate.sessionfactory;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.orm.hibernate3.support.hibernatedaosupport;import org.springframework.stereotype.Component; Import Com.bluesky.spring.domain.User; @Transactional @component ("Userdao") public class Userdaoimpl extends Hibernatedaosupport implements Userdao {public    list<user> listUsers () {        return this.getsession (). CreateQuery ("from User"). List ();    }        } </span></span></span>


Summarize



How XML is configured



Strengths:
1. The XML configuration method further reduces coupling, making the application easier to extend. Even further changes to the configuration file do not require project changes and another compilation.


2. When dealing with large volumes of traffic, it should be better to use XML configuration.

Because XML more clearly indicates the relationship between the various objects, calls between the various business classes. At the same time spring's relevant configuration can also be at a glance.
Of course, some people will say that with XML configuration, the large volume of business will make the XML file too large, not easy to view.

This makes it possible for us to write multiple XML configuration files with business decomposition altogether.

Disadvantages:
The configuration file reads and resolves takes a certain amount of time and is difficult to manage when the configuration file is too large. Unable to verify the correctness of the configuration, added the test difficulty.



Advantages and disadvantages of annotation configuration:

Strengths:


1. In the class file. Can reduce maintenance costs, annotation configuration mechanism is very obvious simple
2. No third-party parsing tools are required to complete the task with Java reflection Technology
3. Editing period can verify correctness, error becomes easy
4. Improve development efficiency

Disadvantages:


1. If you need to change the annotation, compile the entire project again
2. The relationship between business classes is not as easy to grasp as the XML configuration.


3. Assume that the annotation in the program, directly affect the code quality. Has a certain effect on the brevity of the code.



There are many SSH content. First, from the macro control. Experience in the project ... To improve the flexibility of the system and reduce maintenance costs, improve the efficiency of the use of a very high number of configuration files to call back and forth. It involves a lot of connections, and this time it really is necessary to draw. Show it all in the picture and draw your own way of thinking.

SSH Deep Adventures (vi) Five Ways to-----Spring transaction configuration in layman's sense

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.