Конфигурация транзакции в файлах конфигурации пружины всегда состоит из трех частей, а именно: DataSource, TransactionManager и прокси -механизм. Независимо от того, какой метод конфигурации настроен, механизм прокси обычно меняется.
DataSource и TransactionManager будут изменяться только в соответствии с методом доступа данных. Например, при использовании Hibernate для доступа к данным DataSource на самом деле является SessionFactory, а TransactionManager реализуется как HibernateTransactionManager.
Детали следующие:
Согласно различным прокси -механизмам, суммируются пять методов конфигурации пружинной транзакции, а файлы конфигурации следующие:
Первый метод: у каждого боба есть агент
<? 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://wwww.springframework.orbema. 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.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> <bean id =" sessionfactory "> <proport name =" configlocation " name = "configurationClass" value = "org.hibernate.cfg.annotationConfiguration" /> < /bean> <!-определить менеджер транзакций (декларативная транзакция)-> <bean id = "transactionManager"> <свойство = "SessionFactory" ref = "SessionFactory" /> < /bean> <! name = "sessionFactory" ref = "sessionFactory" /> < /bean> <bean id = "userDao"> <!-Configure Transaction Manager-> <Property name = "transactionManager" ref = "TransactionManager" /> <name = "target" ref = "userDaotarget" /> name = "proxeInterfaces" value = "com.bluesshore.s.s.s.s.s. /> <!-Настроить свойства транзакции-> <name = "transactionAttributes"> <props> <prop key = "*"> propagation_required </prop> </props> </propetion> </beans>
Второй метод: все бобы имеют класс прокси -базы
<? 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://wwww.springframework.orbema. 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.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> <bean id =" sessionfactory "> <proport name =" configlocation " 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"> <!-- Configure Диспетчер транзакций-> <name = name = "transactionManager" ref = "transactionManager"/> <!-Настройка свойств транзакции-> <name = "transactionAttributes"> <props> <prop key = "*"> propagation_required </prop> </props> </propetion> </bean> <! name = "sessionFactory" ref = "sessionFactory"/> </bean> <bean id = "userdao" parent = "transactionbase"> <name = "target" ref = "userDaotarget"/> </bean> </beans>
Третий путь: используйте перехватчик
<? 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://wwww.springframework.orbema. 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.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> <bean id =" sessionfactory "> <proport name =" configlocation " 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-> <name = "TransactionAttributes"> <props> <prop key = "*"> Propagation_Required </prop> </props> </props> </properation> </bean> <bean> name = "beanname"> <sile> <ditule> <ditule>*value>*value> <dult>*dao> </dao> </dao properation> <dule> <dule> <date> <dao> </dao> </dao properation> <Dao> </dao> </dao. name = "receptornames"> <sist> <datue> transactionInterceptor </value> </list> </property> </bean> <!-Настройка dao-> <bean id = "userdao"> <name = "sessionFactory" ref = "sessionFactory"/> </bean> </beans>
Четвертый метод: Interceptor, настроенный с использованием тега 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 = "http://www.springframe.orshema. xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.spramework.org/schema/context 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--15. /> <context: component-scan base-package = "com.bluesky" /> <bean id = "sessionfactory"> <name = "configlocation" value = "classpath: hibernate.cfg.xml" /> <name = "configurationclass" value = "org.hibernate.cfg.annationConfiguration" /> < /bean < /bean < /bean < /bean> < /bean> < /bean> < /bean> < /bean> < /bean> 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 = "receptorpointcuts" Expression = "execution (*com.bluesky.spring.dao.*.*(..))" /> <aop: консультант-консультант-ref = "txadvice" pointcut-ref = "receptorpointcuts" /> < /aop: config> < /beans>
Пятый метод: полная аннотация
<? 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 = "http://www.springframe.orshema. xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.spramework.org/schema/context 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--15. /> <context: component-scan base-package = "com.bluesky" /> <tx: аннотационная транзакция-manager = "transactionmanager" /> <bean id = "sessionfactory"> <name = "configlocation" value = "classpath: hibernate.cfg.xml" /> <name = "configurationlclass" value = "org.hibernate.cfg.annotationConfiguration"/> </bean> <!-определить менеджер транзакций (декларативная транзакция)-> <bean id = "transactionManager"> <name = "sessionFactory" ref = "sessionFactory"/> </bean> </beans>
В настоящее время вам нужно добавить @transactional Annotation в DAO, следующим образом:
пакет com.mktao.spring.dao; импортировать java.util.list; Импорт org.hibernate.sessionFactory; Импорт org.springframework.beans.factory.annotation.autowired; импорт org.springframework.orm.hibernate3.support.hibernatedaosupport; Import org.springframework.stereotype.component; Импорт com.bluesky.spring.domain.user; Transactional@component ("userDao") открытый класс userDaoImpl Extends hibernatedAosupport реализует userdao {public list <user> listusers () {return this.getSession (). CreateQuery ("от user").@); } ...}Выше всего содержание этой статьи. Я надеюсь, что это будет полезно для каждого обучения, и я надеюсь, что все будут поддерживать Wulin.com больше.