Loading...
Spring Framework Reference Documentation 7.0.2의 Configuring Different Transactional Semantics for Different Beans의 한국어 번역본입니다.
아래의 경우에 피드백에서 신고해주신다면 반영하겠습니다.
감사합니다 :)
service layer 객체가 여러 개 있고, 각각에 완전히 다른 트랜잭션 설정을 적용하고자 하는 상황을 고려해 보십시오. 서로 다른 pointcut 및 advice-ref attribute 값을 가진 개별적인 <aop:advisor/> element를 정의함으로써 그렇게 할 수 있습니다.
비교를 위해, 먼저 모든 service layer 클래스가 root x.y.service 패키지에 정의되어 있다고 가정합니다. 해당 패키지(또는 서브패키지)에 정의된 클래스의 인스턴스이며 이름이 Service로 끝나는 모든 빈에 기본 트랜잭션 설정을 적용하려면 다음과 같이 작성할 수 있습니다:
1<?xml version="1.0" encoding="UTF-8"?> 2<beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans 8 https://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/tx 10 https://www.springframework.org/schema/tx/spring-tx.xsd 11 http://www.springframework.org/schema/aop 12 https://www.springframework.org/schema/aop/spring-aop.xsd"> 13 14 <aop:config> 15 16 <aop:pointcut id="serviceOperation" 17 expression="execution(* x.y.service..*Service.*(..))"/> 18 19 <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/> 20 21 </aop:config> 22 23 <!-- these two beans will be transactional... --> 24 <bean id="fooService" class="x.y.service.DefaultFooService"/> 25 <bean id="barService" class="x.y.service.extras.SimpleBarService"/> 26 27 <!-- ... and these two beans won't --> 28 <bean id="anotherService" class="org.xyz.SomeService"/> <!-- (not in the right package) --> 29 <bean id="barManager" class="x.y.service.SimpleBarManager"/> <!-- (doesn't end in 'Service') --> 30 31 <tx:advice id="txAdvice"> 32 <tx:attributes> 33 <tx:method name="get*" read-only="true"/> 34 <tx:method name="*"/> 35 </tx:attributes> 36 </tx:advice> 37 38 <!-- other transaction infrastructure beans such as a TransactionManager omitted... --> 39 40</beans>
다음 예제는 완전히 다른 트랜잭션 설정으로 두 개의 서로 다른 빈을 구성하는 방법을 보여 줍니다:
1<?xml version="1.0" encoding="UTF-8"?> 2<beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans 8 https://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/tx 10 https://www.springframework.org/schema/tx/spring-tx.xsd 11 http://www.springframework.org/schema/aop 12 https://www.springframework.org/schema/aop/spring-aop.xsd"> 13 14 <aop:config> 15 16 <aop:pointcut id="defaultServiceOperation" 17 expression="execution(* x.y.service.*Service.*(..))"/> 18 19 <aop:pointcut id="noTxServiceOperation" 20 expression="execution(* x.y.service.ddl.DefaultDdlManager.*(..))"/> 21 22 <aop:advisor pointcut-ref="defaultServiceOperation" advice-ref="defaultTxAdvice"/> 23 24 <aop:advisor pointcut-ref="noTxServiceOperation" advice-ref="noTxAdvice"/> 25 26 </aop:config> 27 28 <!-- this bean will be transactional (see the 'defaultServiceOperation' pointcut) --> 29 <bean id="fooService" class="x.y.service.DefaultFooService"/> 30 31 <!-- this bean will also be transactional, but with totally different transactional settings --> 32 <bean id="anotherFooService" class="x.y.service.ddl.DefaultDdlManager"/> 33 34 <tx:advice id="defaultTxAdvice"> 35 <tx:attributes> 36 <tx:method name="get*" read-only="true"/> 37 <tx:method name="*"/> 38 </tx:attributes> 39 </tx:advice> 40 41 <tx:advice id="noTxAdvice"> 42 <tx:attributes> 43 <tx:method name="*" propagation="NEVER"/> 44 </tx:attributes> 45 </tx:advice> 46 47 <!-- other transaction infrastructure beans such as a TransactionManager omitted... --> 48 49</beans>
Rolling Back a Declarative Transaction
<tx:advice/> Settings