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.
@@ -97,7 +116,7 @@ public void txManagerIsResolvedCorrectlyWhenMultipleManagersArePresent() {
97
116
@ SuppressWarnings ("resource" )
98
117
public void proxyTypeAspectJCausesRegistrationOfAnnotationTransactionAspect () {
99
118
try {
100
- new AnnotationConfigApplicationContext (EnableAspectJTxConfig .class , TxManagerConfig .class );
119
+ new AnnotationConfigApplicationContext (EnableAspectjTxConfig .class , TxManagerConfig .class );
101
120
fail ("should have thrown CNFE when trying to load AnnotationTransactionAspect. " +
102
121
"Do you actually have org.springframework.aspects on the classpath?" );
103
122
}
@@ -139,15 +158,54 @@ public void spr11915() {
139
158
static class EnableTxConfig {
140
159
}
141
160
161
+
142
162
@ Configuration
143
163
static class InheritedEnableTxConfig extends EnableTxConfig {
144
164
}
145
165
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
+
146
203
@ Configuration
147
- @ EnableTransactionManagement (mode = AdviceMode .ASPECTJ )
148
- static class EnableAspectJTxConfig {
204
+ @ EnableTransactionManagement (mode = AdviceMode .ASPECTJ )
205
+ static class EnableAspectjTxConfig {
149
206
}
150
207
208
+
151
209
@ Configuration
152
210
@ EnableTransactionManagement
153
211
static class Spr11915Config {
@@ -167,6 +225,7 @@ public TransactionalTestBean testBean() {
167
225
}
168
226
}
169
227
228
+
170
229
@ Configuration
171
230
static class TxManagerConfig {
172
231
@@ -179,9 +238,9 @@ public TransactionalTestBean testBean() {
179
238
public PlatformTransactionManager txManager () {
180
239
return new CallCountingTransactionManager ();
181
240
}
182
-
183
241
}
184
242
243
+
185
244
@ Configuration
186
245
static class MultiTxManagerConfig extends TxManagerConfig implements TransactionManagementConfigurer {
187
246
0 commit comments