2828import org .springframework .context .annotation .AdviceMode ;
2929import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
3030import org .springframework .context .annotation .Bean ;
31+ import org .springframework .context .annotation .ConditionContext ;
32+ import org .springframework .context .annotation .Conditional ;
3133import org .springframework .context .annotation .Configuration ;
34+ import org .springframework .context .annotation .ConfigurationCondition ;
35+ import org .springframework .core .type .AnnotatedTypeMetadata ;
3236import org .springframework .stereotype .Service ;
3337import org .springframework .tests .transaction .CallCountingTransactionManager ;
3438import org .springframework .transaction .PlatformTransactionManager ;
@@ -51,7 +55,8 @@ public class EnableTransactionManagementTests {
5155
5256 @ Test
5357 public void transactionProxyIsCreated () {
54- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (EnableTxConfig .class , TxManagerConfig .class );
58+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (
59+ EnableTxConfig .class , TxManagerConfig .class );
5560 TransactionalTestBean bean = ctx .getBean (TransactionalTestBean .class );
5661 assertTrue ("testBean is not a proxy" , AopUtils .isAopProxy (bean ));
5762 Map <?,?> services = ctx .getBeansWithAnnotation (Service .class );
@@ -61,7 +66,19 @@ public void transactionProxyIsCreated() {
6166
6267 @ Test
6368 public void transactionProxyIsCreatedWithEnableOnSuperclass () {
64- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (InheritedEnableTxConfig .class , TxManagerConfig .class );
69+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (
70+ InheritedEnableTxConfig .class , TxManagerConfig .class );
71+ TransactionalTestBean bean = ctx .getBean (TransactionalTestBean .class );
72+ assertTrue ("testBean is not a proxy" , AopUtils .isAopProxy (bean ));
73+ Map <?,?> services = ctx .getBeansWithAnnotation (Service .class );
74+ assertTrue ("Stereotype annotation not visible" , services .containsKey ("testBean" ));
75+ ctx .close ();
76+ }
77+
78+ @ Test
79+ public void transactionProxyIsCreatedWithEnableOnExcludedSuperclass () {
80+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (
81+ ParentEnableTxConfig .class , ChildEnableTxConfig .class , TxManagerConfig .class );
6582 TransactionalTestBean bean = ctx .getBean (TransactionalTestBean .class );
6683 assertTrue ("testBean is not a proxy" , AopUtils .isAopProxy (bean ));
6784 Map <?,?> services = ctx .getBeansWithAnnotation (Service .class );
@@ -71,7 +88,8 @@ public void transactionProxyIsCreatedWithEnableOnSuperclass() {
7188
7289 @ Test
7390 public void txManagerIsResolvedOnInvocationOfTransactionalMethod () {
74- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (EnableTxConfig .class , TxManagerConfig .class );
91+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (
92+ EnableTxConfig .class , TxManagerConfig .class );
7593 TransactionalTestBean bean = ctx .getBean (TransactionalTestBean .class );
7694
7795 // invoke a transactional method, causing the PlatformTransactionManager bean to be resolved.
@@ -81,7 +99,8 @@ public void txManagerIsResolvedOnInvocationOfTransactionalMethod() {
8199
82100 @ Test
83101 public void txManagerIsResolvedCorrectlyWhenMultipleManagersArePresent () {
84- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (EnableTxConfig .class , MultiTxManagerConfig .class );
102+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (
103+ EnableTxConfig .class , MultiTxManagerConfig .class );
85104 TransactionalTestBean bean = ctx .getBean (TransactionalTestBean .class );
86105
87106 // invoke a transactional method, causing the PlatformTransactionManager bean to be resolved.
@@ -97,7 +116,7 @@ public void txManagerIsResolvedCorrectlyWhenMultipleManagersArePresent() {
97116 @ SuppressWarnings ("resource" )
98117 public void proxyTypeAspectJCausesRegistrationOfAnnotationTransactionAspect () {
99118 try {
100- new AnnotationConfigApplicationContext (EnableAspectJTxConfig .class , TxManagerConfig .class );
119+ new AnnotationConfigApplicationContext (EnableAspectjTxConfig .class , TxManagerConfig .class );
101120 fail ("should have thrown CNFE when trying to load AnnotationTransactionAspect. " +
102121 "Do you actually have org.springframework.aspects on the classpath?" );
103122 }
@@ -139,15 +158,54 @@ public void spr11915() {
139158 static class EnableTxConfig {
140159 }
141160
161+
142162 @ Configuration
143163 static class InheritedEnableTxConfig extends EnableTxConfig {
144164 }
145165
166+
167+ @ Configuration
168+ @ EnableTransactionManagement
169+ @ Conditional (NeverCondition .class )
170+ static class ParentEnableTxConfig {
171+
172+ @ Bean
173+ Object someBean () {
174+ return new Object ();
175+ }
176+ }
177+
178+
179+ @ Configuration
180+ static class ChildEnableTxConfig extends ParentEnableTxConfig {
181+
182+ @ Override
183+ Object someBean () {
184+ return "X" ;
185+ }
186+ }
187+
188+
189+ private static class NeverCondition implements ConfigurationCondition {
190+
191+ @ Override
192+ public boolean matches (ConditionContext context , AnnotatedTypeMetadata metadata ) {
193+ return false ;
194+ }
195+
196+ @ Override
197+ public ConfigurationPhase getConfigurationPhase () {
198+ return ConfigurationPhase .REGISTER_BEAN ;
199+ }
200+ }
201+
202+
146203 @ Configuration
147- @ EnableTransactionManagement (mode = AdviceMode .ASPECTJ )
148- static class EnableAspectJTxConfig {
204+ @ EnableTransactionManagement (mode = AdviceMode .ASPECTJ )
205+ static class EnableAspectjTxConfig {
149206 }
150207
208+
151209 @ Configuration
152210 @ EnableTransactionManagement
153211 static class Spr11915Config {
@@ -167,6 +225,7 @@ public TransactionalTestBean testBean() {
167225 }
168226 }
169227
228+
170229 @ Configuration
171230 static class TxManagerConfig {
172231
@@ -179,9 +238,9 @@ public TransactionalTestBean testBean() {
179238 public PlatformTransactionManager txManager () {
180239 return new CallCountingTransactionManager ();
181240 }
182-
183241 }
184242
243+
185244 @ Configuration
186245 static class MultiTxManagerConfig extends TxManagerConfig implements TransactionManagementConfigurer {
187246
0 commit comments