스프링 구성 파일의 트랜잭션 구성은 항상 DataSource, TransactionManager 및 프록시 메커니즘의 세 부분으로 구성됩니다. 어떤 구성 메소드가 구성되어 있더라도 프록시 메커니즘이 일반적으로 변경됩니다.
DataSource 및 TransactionManager는 데이터 액세스 방법에 따라 변경됩니다. 예를 들어, 데이터 액세스에 Hibernate를 사용하는 경우 DataSource는 실제로 SessionFactory이며 TransactionManager는 HiberNateTransactionManager로 구현됩니다.
세부 사항은 다음과 같습니다.
다른 프록시 메커니즘에 따르면 5 개의 스프링 트랜잭션 구성 방법이 요약되어 있으며 구성 파일은 다음과 같습니다.
첫 번째 방법 : 각 콩에는 에이전트가 있습니다
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.w3.org/2001/xmlschema-instance" xmlns : context = "http://www.springframework.org/schema/context"xmlns : aop = "http://www.springframework.org/schema/aop"xsi : schemalocation = "http://www.spramwork.org/schema http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/sprging-conxt-2.5 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.cfg.xml ""구성. value = "org.hibernate.cfg.annotationconfiguration" /> < /bean> <!-트랜잭션 관리자 (선언 트랜잭션)-> <bean id = "transactionManager"> <property name = "sessionfactory"ref = "sessionActory" /> < /bean> <!-<bean id = "sessionAtarget"> sessionAtarget "> ref = "sessionFactory" /> < /bean> <bean id = "userDao"> <!-트랜잭션 관리자 구성-> <속성 이름 = "transactionManager"ref = "transactionManager" /> <property name = "target"ref = "userDaOtArget" /<property name = "proxyInterfaces"value = "com.bluesky.sprging.dao. 트랜잭션 속성-> <속성 이름 = "transactionAttributes"> <props> <prop key = "*"> propagation_Required </props> </props> </property> </beans>
두 번째 방법 : 모든 콩이 프록시베이스 클래스를 공유합니다.
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.w3.org/2001/xmlschema-instance" xmlns : context = "http://www.springframework.org/schema/context"xmlns : aop = "http://www.springframework.org/schema/aop"xsi : schemalocation = "http://www.spramwork.org/schema http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/sprging-conxt-2.5 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.cfg.xml ""구성. value = "org.hibernate.cfg.annotationconfiguration" /> < /bean> <!-트랜잭션 관리자 정의 (선언 된 트랜잭션)-> <bean id = "transactionManager"> <property "ref ="sessionfactory "ref ="sessionfactory " /< /bean> <bean> <transactionbase"thantaction "> <trangure" "transure 이름 = "TransactionManager"ref = "TransactionManager"/> <!-트랜잭션 속성을 구성-> <속성 이름 = "transactionAttributes"> <props> <prop key = "*"> propagation_required </props> </props> </property> </bean> <!-<bean id = "sessionDaotarget"> sessionDaotarget "> ref = "sessionFactory"/> </bean> <bean id = "userDao"parent = "transactionbase"> <property name = "target"ref = "userDaotArget"/> </bean> </beans>
세 번째 방법 : 인터셉터를 사용하십시오
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.w3.org/2001/xmlschema-instance" xmlns : context = "http://www.springframework.org/schema/context"xmlns : aop = "http://www.springframework.org/schema/aop"xsi : schemalocation = "http://www.spramwork.org/schema http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/sprging-conxt-2.5 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.cfg.xml ""구성. value = "org.hibernate.cfg.annotationconfiguration" /> < /bean> <!-트랜잭션 관리자 정의 (선언 된 트랜잭션)-> <bean id = "transactionManager"> <property name = "sessionfactory"ref = "sessionfactory" /< /bean> <bean> <transactioninterceptor "> <transactionmanager"transactionmanager "transactionmanager"confatonager "confatonager"transactionmanager 속성 -> <속성 이름 = "transactionAttributes"> <props> <prop key = "*"> propagation_required </props> </props> </props> </property> </bean> <ean> <property name = "beannames"> <list> <list>*dao </value> </list> <property name> <value> TransactionInterceptor </value> </list> </property> </bean> <!-dao-> <bean id = "userDao"> <property name = "sessionFactory"ref = "sessionFactory"/> </bean> </beans>
네 번째 방법 : TX 태그를 사용하여 구성된 인터셉터
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.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.spramwork.org/schema/schema/schema/schema. xsi : schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.xsd http://www.springframework.org/schema/contet http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/sprring--5-2.5.xsd http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.spramework.org/schema/tx-tx-2.5.xsd "> context/> <context : component-scan base-package = "com.bluesky" /> <bean id = "sessionFactory"> <property name = "configlocation"value = "classpath : hibernate.cfg.xml" /> <속성 이름 = "configurationClass"value = "org.hibernate.cfg.annotpiguration (< /bean)"! 트랜잭션)-> <bean id = "transactionManager"> <속성 이름 = "sessionFactory"ref = "sessionFactory"/> </bean> <TX : 조언 ID = "TXADVICE"Transaction-Manager = "TransactionManager"> <TX : Attributes> <TX : 메소드 이름 = "*"propagation = "required"/> </tx : Attributes> <aop : pointcut id = "interceptorpointcuts"expression = "execution (*com.bluesky.spring.dao.*.*(..))") ")") ")") ").
다섯 번째 방법 : 완전 주석
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.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.spramwork.org/schema/schema/schema/schema. xsi : schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.xsd http://www.springframework.org/schema/contet http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/sprring--5-2.5.xsd http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.spramework.org/schema/tx-tx-2.5.xsd "> context/> <context : component-scan base-package = "com.bluesky" /> <tx : 주석 중심 트랜잭션-매너 = "TransactionManager" /> <bean id = "sessionFactory"> <property name = "configlocation"value = "classpath : hibernate.cfg.xml" /> value = "org.hibernate.cfg.annotationconfiguration"/> </bean> <!-트랜잭션 관리자 정의 (선언 트랜잭션)-> <bean id = "TransactionManager"> <속성 이름 = "sessionFactory"ref = "sessionActory"/> </bean> </beans>
이 시점에서 다음과 같이 @transactional 주석을 DAO에 추가해야합니다.
패키지 com.mktao.spring.dao; Java.util.list 가져 오기; import org.hibernate.sessionfactory; org.springframework.beans.factory.annotation.autowired; org.springframework.orm.hibernate3.support.hibernatedaosupport; org.springframework.stereotyp.component import; com.bluesky.spring.domain.user import; @transactional@component ( "userDao") public class userDaoimpl은 hibernatedaoSupport 확장 userDao {public list <user> listUsers () {return this.getSession (). createquery ( "from user"). list (); } ...}위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.