La configuración de la transacción en los archivos de configuración de Spring siempre consta de tres partes, a saber , DataSource, TransactionManager y mecanismo de proxy. No importa qué método de configuración esté configurado, el mecanismo proxy generalmente cambia.
DataSource y TransactionManager solo cambiarán de acuerdo con el método de acceso a datos. Por ejemplo, cuando se usa Hibernate para el acceso a datos, DataSource es en realidad una SessionFactory, y TransactionManager se implementa como HibernateTransactionManager.
Los detalles son los siguientes:
De acuerdo con los diferentes mecanismos proxy, se resumen cinco métodos de configuración de transacción de resorte, y los archivos de configuración son los siguientes:
El primer método: cada frijol tiene un agente
<? xml versión = "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: esquemalocatation 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.-2. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="sessionFactory" > <property name="configLocation" value="classpath:hibernate.cfg.xml" /> <property name = "ConfigurationClass" Value = "org.hibernate.cfg.annotationConfiguration" /> < /bean> <!-Define el administrador de transacciones (transacción declarativa)-> <bean id = "transaccionManager"> <name de la propiedad = "sessionFactory" ref = "sessionFactory" /> < /bean> <!-Configurar Dao-> <<<rumiTarget "Bean" <Seperget "<Bean de bean" <Me Bean "<Bean de bean"> <MOUTERIT name = "sessionFactory" ref = "sessionFactory" /> </ bean> <bean id = "userdao"> <!-configure transaction manager-> <propiedad name = "transaccionManager" ref = "transactionManager" /> <property name = "target" ref = "userDaoTarget" /> <Property name = "proxyinterfaces" valor = "com.blesky.spring.DaoTarget" /> <nameo de propiedad = "Proxyinterfaces" Valor = "Com.Bluesk.Spring.Spring.DaoTarget" /> <Property name = "Proxyinterfaces" Valor = "Com.Bluesky.Spring.DaoTarget" /> <Property Name = "ProXyinterfaces" Valor = "Com.Bluesky.SPRING <
El segundo método: todos los frijoles comparten una clase base proxy
<? xml versión = "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: esquemalocatation 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.-2. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="sessionFactory" > <property name="configLocation" value="classpath:hibernate.cfg.xml" /> <property name = "ConfigurationClass" Value = "org.hibernate.cfg.annotationConfiguration" /> < /bean> <!-Define Transaction Manager (declarado transacción)-> <bean id = "transaccionManager"> <Property name = "sessionFactory" ref = "sessionFactory" /> </ bean> <bean id = "transaccionBase" lazy-init = "verdadero" abstracto "verdadero" verdadero "verdadero" verdadero "! Manager-> <Property Name = "TransActionManager" ref = "TransActionManager"/> <!-Configurar propiedades de transacción-> <Property Name = "TransactionATTributes"> <Props> <PROP KEY = "*"> Propagation_Required </prop> </props> </Property> ref = "SessionFactory"/> </bean> <bean id = "userdao" parent = "transaccionBase"> <Property name = "target" ref = "userDaoTarget"/> </bean> </ beans>
La tercera forma: use un interceptor
<? xml versión = "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: esquemalocatation 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.-2. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="sessionFactory" > <property name="configLocation" value="classpath:hibernate.cfg.xml" /> <property name = "ConfigurationClass" Value = "org.hibernate.cfg.annotationConfiguration" /> < /bean> <!-Define Transaction Manager (declarado transacción)-> <bean id = "TransactionManager"> <Property name = "sessionFactory" ref = "sessionFactory" /> < /bean> <bean id = "transaccioneportor"> <sperty name = "transaccionManager" rehing "" reh "rehing" "" reh "" "rehing" "" "" reh. < <value> TransActionInterceptor </valor> </list> </property> </bean> <!-Configurar dao-> <bean id = "userdao"> <propiedad name = "sessionFactory" ref = "sessionFactory"/> </ bean> </ beans>
El cuarto método: Interceptor configurado usando la etiqueta TX
<? xml versión = "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://wwww.springframework.org/sChMing" " xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/contextExt 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/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.5.5.5.5.5. /> <context: component-scan base-package = "com.bluesky" /> <bean id = "sessionFactory"> <propiedad name = "configlocation" value = "classpath: hibernate.cfg.xml" /> <Property name = "ConfigurationClass" Value = "Org.hibernate.cfg.annotationConfiguration" /> < /<ath!-Confine-the Trasation the TransAction the TransAction the TransAction the TransAction the TransAction the TransAction the Transation the TransAction the TransAction the TransAction the TransAction the TransAction the TRAN TRANA transaction) --> <bean id="transactionManager" > <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <aop:config> <AOP: PointCut ID = "InterceptorPointCuts" Expression = "Execution (*com.bluesky.spring.dao.*.*(..))" /> <aop: advisorsion-ref = "txAdvice" point-ref = "interceptorPointCuts" /> < /aop: config> < /beans>
El quinto método: anotación completa
<? xml versión = "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://wwww.springframework.org/sChMing" " xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/contextExt 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/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.5.5.5.5.5. /> <context: component-scan base-package = "com.bluesky" /> <tx: annotation-maneation-ganager = "transaccionManager" /> <bean id = "sessionFactory"> <Property name = "configLocation" valor = "classpath: hibernate.cfg.xml" /> <name de propiedad = "configuración de configuración" "" value = "org.hibernate.cfg.annotationConfiguration"/> </bean> <!-Defina el administrador de transacciones (transacción declarativa)-> <bean id = "transaccionManager"> <Property name = "sessionFactory" ref = "sessionFactory"/> </ Bean> </beachs>
En este momento, debe agregar @Transactional Annotation al DAO, como sigue:
paquete com.mktao.spring.dao; import java.util.list; importar org.hibernate.sessionFactory; importar org.springframework.beans.factory.annotation.aUtowired; importar org.springframework.orm.Hibernate3.support.HibernatedaSupport; importar org.springframework.stereotype.component; import com.bluesky.spring.domain.user; @Transaccional@component ("userdao") Public Class UserDaoMpl extiende Hibernadaosupport implementa UserDao {public List <Serer> listUsers () {return this.getSession (). CreateQuery ("De User"). List (); } ...}Lo anterior es todo el contenido de este artículo. Espero que sea útil para el aprendizaje de todos y espero que todos apoyen más a Wulin.com.