28
28
import org .springframework .context .annotation .AdviceMode ;
29
29
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
30
30
import org .springframework .context .annotation .Bean ;
31
+ import org .springframework .context .annotation .ConditionContext ;
32
+ import org .springframework .context .annotation .Conditional ;
31
33
import org .springframework .context .annotation .Configuration ;
34
+ import org .springframework .context .annotation .ConfigurationCondition ;
35
+ import org .springframework .core .type .AnnotatedTypeMetadata ;
32
36
import org .springframework .stereotype .Service ;
33
37
import org .springframework .tests .transaction .CallCountingTransactionManager ;
34
38
import org .springframework .transaction .PlatformTransactionManager ;
@@ -51,7 +55,8 @@ public class EnableTransactionManagementTests {
51
55
52
56
@ Test
53
57
public void transactionProxyIsCreated () {
54
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (EnableTxConfig .class , TxManagerConfig .class );
58
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (
59
+ EnableTxConfig .class , TxManagerConfig .class );
55
60
TransactionalTestBean bean = ctx .getBean (TransactionalTestBean .class );
56
61
assertTrue ("testBean is not a proxy" , AopUtils .isAopProxy (bean ));
57
62
Map <?,?> services = ctx .getBeansWithAnnotation (Service .class );
@@ -61,7 +66,19 @@ public void transactionProxyIsCreated() {
61
66
62
67
@ Test
63
68
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 );
65
82
TransactionalTestBean bean = ctx .getBean (TransactionalTestBean .class );
66
83
assertTrue ("testBean is not a proxy" , AopUtils .isAopProxy (bean ));
67
84
Map <?,?> services = ctx .getBeansWithAnnotation (Service .class );
@@ -71,7 +88,8 @@ public void transactionProxyIsCreatedWithEnableOnSuperclass() {
71
88
72
89
@ Test
73
90
public void txManagerIsResolvedOnInvocationOfTransactionalMethod () {
74
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (EnableTxConfig .class , TxManagerConfig .class );
91
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (
92
+ EnableTxConfig .class , TxManagerConfig .class );
75
93
TransactionalTestBean bean = ctx .getBean (TransactionalTestBean .class );
76
94
77
95
// invoke a transactional method, causing the PlatformTransactionManager bean to be resolved.
@@ -81,7 +99,8 @@ public void txManagerIsResolvedOnInvocationOfTransactionalMethod() {
81
99
82
100
@ Test
83
101
public void txManagerIsResolvedCorrectlyWhenMultipleManagersArePresent () {
84
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (EnableTxConfig .class , MultiTxManagerConfig .class );
102
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (
103
+ EnableTxConfig .class , MultiTxManagerConfig .class );
85
104
TransactionalTestBean bean = ctx .getBean (TransactionalTestBean .class );
86
105
87
106
// invoke a transactional method, causing the PlatformTransactionManager bean to be resolved.
@@ -96,7 +115,7 @@ public void txManagerIsResolvedCorrectlyWhenMultipleManagersArePresent() {
96
115
@ Test
97
116
public void proxyTypeAspectJCausesRegistrationOfAnnotationTransactionAspect () {
98
117
try {
99
- new AnnotationConfigApplicationContext (EnableAspectJTxConfig .class , TxManagerConfig .class );
118
+ new AnnotationConfigApplicationContext (EnableAspectjTxConfig .class , TxManagerConfig .class );
100
119
fail ("should have thrown CNFE when trying to load AnnotationTransactionAspect. " +
101
120
"Do you actually have org.springframework.aspects on the classpath?" );
102
121
}
@@ -138,15 +157,54 @@ public void spr11915() {
138
157
static class EnableTxConfig {
139
158
}
140
159
160
+
141
161
@ Configuration
142
162
static class InheritedEnableTxConfig extends EnableTxConfig {
143
163
}
144
164
165
+
166
+ @ Configuration
167
+ @ EnableTransactionManagement
168
+ @ Conditional (NeverCondition .class )
169
+ static class ParentEnableTxConfig {
170
+
171
+ @ Bean
172
+ Object someBean () {
173
+ return new Object ();
174
+ }
175
+ }
176
+
177
+
178
+ @ Configuration
179
+ static class ChildEnableTxConfig extends ParentEnableTxConfig {
180
+
181
+ @ Override
182
+ Object someBean () {
183
+ return "X" ;
184
+ }
185
+ }
186
+
187
+
188
+ private static class NeverCondition implements ConfigurationCondition {
189
+
190
+ @ Override
191
+ public boolean matches (ConditionContext context , AnnotatedTypeMetadata metadata ) {
192
+ return false ;
193
+ }
194
+
195
+ @ Override
196
+ public ConfigurationPhase getConfigurationPhase () {
197
+ return ConfigurationPhase .REGISTER_BEAN ;
198
+ }
199
+ }
200
+
201
+
145
202
@ Configuration
146
- @ EnableTransactionManagement (mode = AdviceMode .ASPECTJ )
147
- static class EnableAspectJTxConfig {
203
+ @ EnableTransactionManagement (mode = AdviceMode .ASPECTJ )
204
+ static class EnableAspectjTxConfig {
148
205
}
149
206
207
+
150
208
@ Configuration
151
209
@ EnableTransactionManagement
152
210
static class Spr11915Config {
@@ -166,6 +224,7 @@ public TransactionalTestBean testBean() {
166
224
}
167
225
}
168
226
227
+
169
228
@ Configuration
170
229
static class TxManagerConfig {
171
230
@@ -178,9 +237,9 @@ public TransactionalTestBean testBean() {
178
237
public PlatformTransactionManager txManager () {
179
238
return new CallCountingTransactionManager ();
180
239
}
181
-
182
240
}
183
241
242
+
184
243
@ Configuration
185
244
static class MultiTxManagerConfig extends TxManagerConfig implements TransactionManagementConfigurer {
186
245
0 commit comments