Konfigurasi transaksi dalam file konfigurasi pegas selalu terdiri dari tiga bagian, yaitu DataSource, TransactionManager dan mekanisme proxy. Tidak peduli metode konfigurasi mana yang dikonfigurasi, mekanisme proxy umumnya diubah.
DataSource dan TransactionManager hanya akan berubah sesuai dengan metode akses data. Misalnya, saat menggunakan Hibernate untuk akses data, DataSource sebenarnya adalah sesiFactory, dan TransactionManager diimplementasikan sebagai HibernatetransactionManager.
Dengan rincian sebagai berikut:
Menurut mekanisme proxy yang berbeda, lima metode konfigurasi transaksi pegas dirangkum, dan file konfigurasi adalah sebagai berikut:
Metode pertama: Setiap kacang memiliki agen
<? XML Versi = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmls xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xsi: schemalocation = "http:/schema http://www.springframework.org/schema/beans/spring-weans-2.5.xsd http://www.springframework.org/schema/context http:/www.spramework.org/schema/context/www.spramework.org/schema/context 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> <!-Tentukan Transaction Manager (Transaksi Deklaratif)-> <bean id = "transactionManager"> <Properti nama = "sessionfactory" ref = "sessionfactory" /< /bean> <! name = "sessionFactory" ref = "sessionFactory" /> </ bean> <bean id = "userdao"> <!-Monfigurasi Transaction Manager-> <properti name = "transactionManager" ref = "transactionManager" /> <name properti = "target" ref = "userdaotarget" /<name propertyforacer = "properti =" target "ref =" userdaoTarget " /<properti name =" ProxyFaces = "Proxyintaces" value = "com.bluesky.spring.dao.generatordao"/> <!-Mengkonfigurasi Properti Transaksi-> <nama properti = "TransactionAttributes"> <props> <prop key = "*"> propagation_required </prop> </props> </properti> </bean> "
Metode kedua: Semua kacang berbagi kelas dasar proxy
<? XML Versi = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmls xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xsi: schemalocation = "http:/schema http://www.springframework.org/schema/beans/spring-weans-2.5.xsd http://www.springframework.org/schema/context http:/www.spramework.org/schema/context/www.spramework.org/schema/context 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 (declared transaction) --> <bean id="transactionManager" > <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="transactionBase" lazy-init="true" abstract="true"> <!-- Konfigurasikan Transaction Manager-> <Properti Nama = "TransactionManager" Ref = "TransactionManager"/> <!-Mengkonfigurasi Properti Transaksi-> <nama properti = "TransactionAttributes"> <props> <prop key = "*"> propagation_required </prop> </props> </properti> </bean> <!-configure dao-"property </props> </property> </bean> <!-configure dao-" User-ID = " name = "sessionFactory" ref = "sessionfactory"/> </ bean> <bean id = "userdao" parents = "transactionbase"> <properti name = "target" ref = "userdaotarget"/> </bean> </bean>
Cara Ketiga: Gunakan pencegat
<? XML Versi = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmls xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xsi: schemalocation = "http:/schema http://www.springframework.org/schema/beans/spring-weans-2.5.xsd http://www.springframework.org/schema/context http:/www.spramework.org/schema/context/www.spramework.org/schema/context 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 (declared transaction) --> <bean id="transactionManager" > <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="transactionInterceptor" > <property name="transactionManager" ref="transactionManager" /> <!-- Configure transaction properties --> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </props> </property> </bean> <bean> <property name="beanNames"> <list> <value>*Dao</value> </list> </property> <property Name = "IntercepToNTames"> <list> <value> transactionInceptor </ value> </cist> </propert> </t bean> <!-Konfigurasikan dao-> <bean id = "userdao"> <name properti = "sessionfactory" ref = "sessionfactory"/> </bean> </bean> sessionfactory "ref =" sessionfactory "/> </bean> </bean> sesige>
Metode Keempat: Interceptor Dikonfigurasi Menggunakan Tag TX
<? XML Versi = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmls xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http:/schema/aop" xmlns: tx = "http:/schema.aop" XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-weans-2.4.xsd http:/wwww.spramorker http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/aop:aop: http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http:/www.spramework.org/schema/tx :-ponfing/contx. /> <konteks: komponen-scan-package = "com.bluesky" /> <bean id = "sessionfactory"> <name properti = "configLocation" value = "classpath: hibernate.cfg.xml" /<name properti = "configurationclass" value = "org.hibernate.cfg.annoTaction" nilai /org.hibernate.cfg.annoCaction (DECRIFIRASI "< /org.hibernate. transaksi)-> <bean id = "transactionManager"> <name properti = "sessionfactory" ref = "sessionfactory"/> </ bean> <tx: nasihat id = "txadvice" transaction-manager = "transactionagager"> <tx: attributes> <tx: nama metode = "*" propagation = "wajib" <AOP: pointcut id = "interceptorpointcuts" ekspresi = "eksekusi (*com.bluesky.spring.dao.*.*(..))" /> <aop: advisor nasihat-ref = "txadvice" pointcut-ref = "interceptorpointcuts" /< /< /aop: config> < /beans>
Metode kelima: anotasi penuh
<? XML Versi = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmls xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http:/schema/aop" xmlns: tx = "http:/schema.aop" XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-weans-2.4.xsd http:/wwww.spramorker http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/aop:aop: http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http:/www.spramework.org/schema/tx :-ponfing/contx. /> <konteks: komponen-scan-package = "com.bluesky" /> <tx: anotasi-digerakkan transaksi-manager = "transactionManager" /> <bean id = "sessionfactory"> <name properti = "configLocation" value = "classpath: hibernate.cfg.xml" /<configlocation = "classpath: hibernate.cfg.xml" /<configlocation = "classpath" value = "org.hibernate.cfg.AnnotationConfiguration"/> </ bean> <!-Tentukan Transaction Manager (Transaksi Deklaratif)-> <bean id = "TransactionManager"> <nama properti = "sessionfactory" ref = "sessionfactory"/> </bean> </bean> sesi
Pada saat ini, Anda perlu menambahkan anotasi @transactional ke DAO, sebagai berikut:
paket com.mktao.spring.dao; impor java.util.list; impor org.hibernate.SessionFactory; impor org.springframework.beans.factory.annotation.Autowired; impor org.springframework.orm.hibernate3.support.hibernedAosupport; impor org.springframework.stereotype.component; impor com.bluesky.spring.domain.user; @Transactional@component ("userdao") kelas publik userdaoImpl memperluas hibernedAoSupport mengimplementasikan userdao {daftar publik <user> listusers () {return this.getSession (). Createqueery ("from user"). List (); } ...}Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.