การกำหนดค่าการทำธุรกรรมในไฟล์การกำหนดค่าสปริงมักจะประกอบด้วยสามส่วนคือ แหล่งข้อมูล, TransactionManager และ กลไกพร็อกซี ไม่ว่าจะกำหนดวิธีการกำหนดค่าแบบใดกลไกพร็อกซีจะเปลี่ยนไปโดยทั่วไป
DataSource และ TransactionManager จะเปลี่ยนไปตามวิธีการเข้าถึงข้อมูลเท่านั้น ตัวอย่างเช่นเมื่อใช้ Hibernate สำหรับการเข้าถึงข้อมูล DataSource เป็น SessionFactory และ TransactionManager จะถูกนำมาใช้เป็น hibernatetransactionManager
รายละเอียดมีดังนี้:
ตามกลไกพร็อกซีที่แตกต่างกันมีการสรุปวิธีการกำหนดค่าธุรกรรมสปริงห้าวิธีและไฟล์การกำหนดค่ามีดังนี้:
วิธีแรก: ถั่วแต่ละตัวมีตัวแทน
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xsi: schemalocation = "http:/ http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> name = "configurationclass" value = "org.hibernate.cfg.annotationConfiguration" /> </ebean> <!-กำหนดตัวจัดการธุรกรรม (การทำธุรกรรมประกาศ)-> <bean id = "transactionManager"> <property name = "sessionFactory" ref = "sessionfactory" /> name = "sessionfactory" ref = "sessionfactory" /> </ebean> <bean id = "userdao"> <!-กำหนดค่าตัวจัดการธุรกรรม-> <property name = "transactionManager" ref = "transactionManager" /> <property name = "target" ref = "userDaotarget" /> < /> <!-กำหนดค่าคุณสมบัติการทำธุรกรรม-> <property name = "TransactionAtTributes"> <props> <prop key = "*"> propagation_required </prop>
วิธีที่สอง: ถั่วทั้งหมดแชร์คลาสฐานพร็อกซี
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xsi: schemalocation = "http:/ http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> name = "configurationclass" value = "org.hibernate.cfg.annotationConfiguration" /> </ebean> <!-กำหนดตัวจัดการธุรกรรม (การทำธุรกรรมที่ประกาศ)-> <bean id = "transactionManager"> <property name = "SessionFactory" ref = "SessionFactory" กำหนดค่า Transaction Manager-> <property name = "transactionManager" ref = "transactionManager"/> <!-กำหนดค่าคุณสมบัติการทำธุรกรรม-> <property name = "TransactionAtTributes"> <props> <prop key = "*"> การเผยแพร่ </required </prop> </props> name = "sessionfactory" ref = "sessionfactory"/> </ebean> <bean id = "userdao" parent = "transactionBase"> <property name = "target" ref = "userdaotarget"/> </ebean> </epeans>
วิธีที่สาม: ใช้ interceptor
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xsi: schemalocation = "http:/ http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> name = "configurationclass" value = "org.hibernate.cfg.annotationConfiguration" /> </ebean> <!-กำหนดตัวจัดการธุรกรรม (การทำธุรกรรมประกาศ)-> <bean id = "transactionManager"> <property name = "SessionFactory" ref = "SessionFactory" ref = "transactionManager"/> <!-กำหนดค่าคุณสมบัติการทำธุรกรรม-> <property name = "TransactionAtTributes"> <props> <prop key = "*"> propagation_required </prop> </props> </props> </property> name = "interceptornames"> <list> <value> transactionInterceptor </value> </list> </property> </ebean> <!-กำหนดค่า dao-> <bean id = "userdao"> <property name = "sessionfactory" ref = "SessionFactory"
วิธีที่สี่: Interceptor ที่กำหนดค่าโดยใช้ TX Tag
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http:/ XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://ww.springframework http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://ww.springframework.org/schema http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://ww.springframework.org/schema <บริบท: Annotation-config /> <บริบท: Component-Scan base-base-package = "com.bluesky" /> <bean id = "sessionfactory"> <property name = "configlocation" value = "classpath: hibernate.cfg.xml" /> <property name = "การกำหนดค่า" กำหนดตัวจัดการธุรกรรม (การทำธุรกรรมที่ประกาศ)-> <bean id = "transactionManager"> <property name = "sessionfactory" ref = "sessionfactory"/> </epean> <tx: idern id = "txadvice" transaction-manager = "TransactionManager"> <tx: Attributes> <aop: config> <aop: pointcut id = "interceptorpointcuts" expression = "การดำเนินการ (*com.bluesky.spring.dao.*.*(.. ))" /> <aop: คำแนะนำที่ปรึกษา-ref = "txadvice" pointcut-ref = "interceptorpointcuts" />
วิธีที่ห้า: คำอธิบายประกอบเต็มรูปแบบ
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http:/ XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://ww.springframework http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://ww.springframework.org/schema http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://ww.springframework.org/schema <บริบท: Annotation-config /> <บริบท: Component-Scan base-base-package = "com.bluesky" /> <tx: transaction-driven transaction-manager = "TransactionManager" /> <bean id = "SessionFactory"> <property name = "การกำหนดค่า value = "org.hibernate.cfg.annotationConfiguration"/> </ebean> <!-กำหนดตัวจัดการธุรกรรม (ธุรกรรมประกาศ)-> <bean id = "transactionManager"> <property name = "sessionfactory" ref = "sessionfactory"
ในเวลานี้คุณต้องเพิ่มคำอธิบายประกอบ @Transactional ลงใน DAO ดังนี้:
แพ็คเกจ com.mktao.spring.dao; นำเข้า java.util.list; นำเข้า org.hibernate.sessionfactory; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.orm.hibernate3.support.hibernatedaosupport; นำเข้า org.springframework.stereotype.component; นำเข้า com.bluesky.spring.domain.user; @transactional@component ("userdao") คลาสสาธารณะ UserDaoimpl ขยาย HiberNatedaoSupport ดำเนินการ UserDao {รายการสาธารณะ <ผู้ใช้> ListUsers () {return this.getSession (). createquery ("จากผู้ใช้") รายการ (); -ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น