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