Die Transaktionskonfiguration in Federkonfigurationsdateien besteht immer aus drei Teilen, nämlich DataSource-, Transaktionsmanager- und Proxy -Mechanismus. Unabhängig davon, welche Konfigurationsmethode konfiguriert ist, wird der Proxy -Mechanismus im Allgemeinen geändert.
DataSource und TransactionManager ändern sich nur entsprechend der Datenzugriffsmethode. Bei der Verwendung von Hibernate für den Datenzugriff ist DataSource beispielsweise tatsächlich ein Sitzungsfaktor, und TransactionManager wird als HibernatetransactionManager implementiert.
Die Details sind wie folgt:
Gemäß den verschiedenen Proxy -Mechanismen werden fünf Feder -Transaktionskonfigurationsmethoden zusammengefasst und die Konfigurationsdateien wie folgt:
Die erste Methode: Jede Bean hat einen Agenten
<? 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/context/sping-context-conext-context-ntext-ntext-2.- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> <bean id =" sessionfactory "> < name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> </bean> <!-- Define the transaction manager (declarative transaction) --> <bean id="transactionManager" > <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- Configure DAO --> <bean id="userDaoTarget"> <property name = "sessionfactory" ref = "sessionFactory" /> < /bean> <bean id = "userDao"> <!-transaktionsmanager konfigurieren-> <Eigenschaft name = "transactionManager" ref = "transactionManager" /> <property name = "target" ref = "userdaotarget" /> <proxyinterfaces " value = "com.bluesky.spring.dao.generatordao"/> <!-Transaktionseigenschaften konfigurieren-> <Eigenschaft name = "transactionAttributes"> <props> <PROP KEY = "*"> Propagation_Required </prop> </props> </property> </beans>
Die zweite Methode: Alle Bohnen teilen sich eine Proxy -Basisklasse
<? 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/context/sping-context-conext-context-ntext-ntext-2.- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> <bean id =" sessionfactory "> < name = "configurationClass" value = "org.hiberNate.cfg.annotationConfiguration" /> < /bean> <!-Transaktionsmanager definieren (deklarierte Transaktion)-> <bean id = "transactionManager"> <Eigenschaft name = "SessionFactory" Refactory "," sactractory " /> < /uw> < /bean> < /bean> <Bean <Bean". <!-transaktionsmanager konfigurieren-> <Eigenschaft name = "transactionManager" ref = "transactionManager"/> <!-transaktionseigenschaften konfigurieren-> <Eigenschaft name = "transactionAttributes"> <props> <PROPE KEY = "*"> Propagation_required </Prop> </Props> </ubiefs> </proban> configure dao dao---> </teenda. name = "sessionfactory" ref = "sessionfactory"/> </bean> <bean id = "userDao" parent = "transactionbase"> <Eigenschaft name = "target" ref = "userDaotarget"/> </bean> </beans>
Der dritte Weg: Verwenden Sie einen Interceptor
<? 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/context/sping-context-conext-context-ntext-ntext-2.- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> <bean id =" sessionfactory "> < name = "configurationClass" value = "org.hiberNate.cfg.AnnotationConfiguration" /> < /bean> <!-Transaktionsmanager definieren (deklarierte Transaktion)-> <bean id = "transactionManager"> <Eigenschaft name = "SessionFactory" Refactory "SessionFactory" /> < /> < /Bean> < /bean> <Bean> <Bean <Bean <Bean <Bean <Bean <Bean <Bean> <Bean> <Bean <Bean> <Bean <Bean <Bean> <Bean <Bean> <Bean <Bean <Bean> <Bean> <Bean <Bean> <Bean <Bean <Bean <Bean> ref = "transactionManager"/> <!-transaktionseigenschaften konfigurieren-> <Eigenschaft name = "transactionAttributes"> <props> <prop taste = "*"> Propagation_Required </prop> </props> </props> </property> </bean> <bean> </property> </</<List> -List> </</itlist> </<List> name = "interceptorNames"> <list> <Wert> TransactionInterceptor </value> </list> </property> </bean> <!-konfigurieren DAO-> <bean id = "userDao"> <Eigenschaft name = "SessionFactory" Ref = "SessionFactory"/> </bean> </bean>
Die vierte Methode: Interceptor mit TX -Tag konfiguriert
<? xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.spingframework.org/schwa XSI: ScheMalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/contex http://www.springframework.org/schema/context/spring-context-2.xsd http://www.springFramework.org/schema/aop http://www.springframework.org/Schema/aop/aPring-aop-2.-- http://www.springframework.org/schema/aop/spring-aop-2.5sd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/sping-tx-2.5.5.5. <CONTEXT: Annotation-Config /> <Context: Komponent-scan-Basis-Package = "com.bluesky" /> <bean id = "sessionfactory"> <Eigenschaft name = "configLocation" value = "classPath: hibernate.cfg.xml" /> <Eigentum name = "configurationClass" value = "org.hibernate.hibernate.hibernate.hibernate.hibernate.hibernate.hibernate.hibernate.hibernate.hibernate.hibernate.hibernate.hibernate.hibernate.hibernate.hibernate.hibernate.hibernate.hibernate.hibernate <!-Definieren Sie den Transaktionsmanager (deklarierte Transaktion)-> <bean id = "transactionManager"> <Eigenschaft name = "sessionfactory" ref = "sessionFactory" /> < /bean> <tx: Ratschläge id = "txadvice" transaction-Manager = "transactionManager"> <tx: Attribute> <tx: method name = "*". </tx: Ratschläge> <AOP: config> <AOP: pointcut id = "interceptorpointCuts" Expression = "Execution (*com.bulesky.spring.dao.
Die fünfte Methode: volle Annotation
<? xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.spingframework.org/schwa XSI: ScheMalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/contex http://www.springframework.org/schema/context/spring-context-2.xsd http://www.springFramework.org/schema/aop http://www.springframework.org/Schema/aop/aPring-aop-2.-- http://www.springframework.org/schema/aop/spring-aop-2.5sd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/sping-tx-2.5.5.5. <Context: Annotation-Konfiguration /> <Kontext: Komponent-scan-Basis-Package = "Com.Bluesky" /> <tx: Annotationsgetriebene Transaktionsmanager = "TransactionManager" /> <bean id = "sessionfactory"> <Property name = "configLoclocation" configuration = "classSpath: hibernate.cfg.xml" /< value = "org.hiberNate.cfg.annotationConfiguration"/> </bean> <!-Definieren Sie den Transaktionsmanager (deklarative Transaktion)-> <bean id = "transactionManager"> <Eigenschaftsname = "SessionFactory" Ref = "SessionFactory"/> </bean> </beans>
Zu diesem Zeitpunkt müssen Sie dem DAO @Transactional Annotation wie folgt hinzufügen:
Paket com.mktao.spring.dao; importieren 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") öffentliche Klasse UserDaOImpl erweitert hibernatedaOsupport implementiert userDao {public list <Unter> listUsers () {return this } ...}Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.