Spring構成ファイルのトランザクション構成は、常にDataSource、TransactionManager 、およびプロキシメカニズムの3つの部分で構成されています。どの構成方法が構成されていても、プロキシメカニズムが一般的に変更されます。
DataSourceとTransactionManagerは、データアクセス方法に従ってのみ変更されます。たとえば、Hibernateをデータアクセスに使用する場合、DataSourceは実際にはSessionFactoryであり、TransactionManagerはHibernateTransactionManagerとして実装されます。
詳細は次のとおりです。
さまざまなプロキシメカニズムによると、5つのSpringトランザクション構成方法が要約されており、構成ファイルは次のとおりです。
最初の方法:各豆にはエージェントがあります
<?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://ww.springframework http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context-15.5.5.x--context-2.5 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd " name = "ConfigurationClass" value = "org.hibernate.cfg.annotationConfiguration" /> < /bean> <! - トランザクションマネージャー(宣言的トランザクション) - > <bean id = "transactionmanager"> <プロパティname = "sessionfactory" ref = "sessionfactory" /> < name = "sessionfactory" ref = "sessionfactory" /> < /bean> <bean id = "userdao"> <! - configure transaction manager-> <property name = "transactionmanager" ref = "transactionmanager" /> <プロパティ名= " value = "com.bluesky.spring.dao.generatordao"/> <! - configure transactionプロパティ - > <プロパティname = "transactionattributes"> <props> <prop key = "*"> propagation
2番目の方法:すべての豆がプロキシベースクラスを共有しています
<?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://ww.springframework http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context-15.5.5.x--context-2.5 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd " name = "ConfigurationClass" value = "org.hibernate.cfg.annotationConfiguration" /> < /bean> <! - transaction Manager(宣言されたトランザクション) - > <bean id = "transactionmanager"> <プロパティname = "sessionfactory" ref = "sessionfactory" />> < <! - トランザクションマネージャーの構成 - > <プロパティ名= "TransactionManager" ref = "transactionManager"/> <! - トランザクションプロパティを構成 - > <プロパティ名= "> <props"> <props> <prop key = "*" name = "sessionfactory" ref = "sessionfactory"/> </bean> <bean id = "userdao" parent = "transactionbase"> <プロパティname = "ref =" userdaotarget "/> </bean> </bean> </bean>
3番目の方法:インターセプターを使用します
<?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://ww.springframework http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context-15.5.5.x--context-2.5 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd " name = "ConfigurationClass" value = "org.hibernate.cfg.annotationConfiguration" /> < /bean> <! - transaction Manager(宣言されたトランザクション) - > <bean id = "transactionmanager"> <Property name = "sessionfactory" ref = "sessionfactory" />> ref = "transactionManager"/> <! - Transactionプロパティを構成 - > <プロパティ名= "TransactionAttributes"> <props> <prop key = "*"> propagation_required </required> </property> </bean> <bean> <property name = "beannames"> <値> name = "interceptornames"> <list> <value> transactionInterceptor </value> </list> </property> </bean> <! - configure dao-> <bean id = "userdao"> <property name = "sessionfactory" ref = "sessionfactory"/> </bean> </beans>
4番目の方法: TXタグを使用して構成されたインターセプター
<?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 = " xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www. http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/spring--1.5.5.x. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/TX http://www.springframework/sorg/schema/spring-tx-2.5.x- <context:annotation-config /> <context:component-scan base-package = "com.bluesky" /> <bean id = "sessionfactory"> <プロパティ名= "configlocation" value = "classpath:hibernate.cfg.xml" /> <プロパティname = "configurationclass" value = "トランザクションマネージャー(宣言されたトランザクション) - > <bean id = "transactionmanager"> <property name = "sessionfactory" ref = "sessionfactory" /> < /bean> <tx:アドバイスid = "txadvice" transaction-manager = "transactionmanager"> <tx:属性> <tx:method name = " </tx:advise> <aop:config> <aop:pointcut id = "interceptorpointcuts" expression = "execution(*com.bluesky.spring.dao。
5番目の方法:完全な注釈
<?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 = " xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www. http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/spring--1.5.5.x. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/TX http://www.springframework/sorg/schema/spring-tx-2.5.x- <context:annotation-config /> <context:component-scan base-package = "com.bluesky" /> <tx:annotation-driven transaction-manager = "transactionmanager" /> <bean id = "sessionfactory"> <プロパティ名= "configlocation" value = "classpath:hibernate.cfg.xml" /> < value = "org.hibernate.cfg.AnnotationConfiguration"/> </bean> <! - トランザクションマネージャー(宣言的トランザクション) - > <bean id = "transactionmanager"> <property name = "sessionfactory" ref = "sessionfactory"/> </bean> </beans> </beans>
この時点では、次のように、@TransactionAl AnnotationをDAOに追加する必要があります。
パッケージcom.mktao.spring.dao; java.util.listをインポートします。 Import org.hibernate.sessionFactory; Import org.springframework.beans.factory.annotation.autowired; import org.springframework.orm.hibernate3.support.hibernatedaosupport; org.springframework.stereotype.componentをインポートします。 com.bluesky.spring.domain.userをインポートします。 @transactional@component( "userdao")public class userdaoimpl extens hibernatedaosupport emplments userdao {public list <user> listusers(){return this.getSession()。createquery( "from user")。リスト(); } ...}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。