1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
18
18
19
19
import javax .annotation .PreDestroy ;
20
20
21
+ import org .junit .jupiter .api .BeforeEach ;
21
22
import org .junit .jupiter .api .Test ;
22
23
23
24
import org .springframework .aop .framework .autoproxy .BeanNameAutoProxyCreator ;
24
25
import org .springframework .aop .framework .autoproxy .target .LazyInitTargetSourceCreator ;
25
26
import org .springframework .aop .target .AbstractBeanFactoryBasedTargetSource ;
26
- import org .springframework .context .ApplicationContext ;
27
27
import org .springframework .context .ApplicationListener ;
28
+ import org .springframework .context .ConfigurableApplicationContext ;
28
29
import org .springframework .context .event .ApplicationContextEvent ;
29
30
30
31
import static org .assertj .core .api .Assertions .assertThat ;
31
32
32
33
/**
34
+ * Integration tests for {@link BeanNameAutoProxyCreator} and
35
+ * {@link LazyInitTargetSourceCreator}.
36
+ *
33
37
* @author Juergen Hoeller
34
38
* @author Arrault Fabien
39
+ * @author Sam Brannen
35
40
*/
36
- public class AutoProxyLazyInitTests {
41
+ class AutoProxyLazyInitTests {
37
42
38
- @ Test
39
- public void withStaticBeanMethod () {
43
+ @ BeforeEach
44
+ void resetBeans () {
40
45
MyBeanImpl .initialized = false ;
46
+ }
41
47
42
- ApplicationContext ctx = new AnnotationConfigApplicationContext (ConfigWithStatic .class );
43
- MyBean bean = ctx .getBean ("myBean" , MyBean .class );
48
+ @ Test
49
+ void withStaticBeanMethod () {
50
+ ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext (ConfigWithStatic .class );
51
+ MyBean bean = ctx .getBean (MyBean .class );
44
52
45
53
assertThat (MyBeanImpl .initialized ).isFalse ();
46
54
bean .doIt ();
47
55
assertThat (MyBeanImpl .initialized ).isTrue ();
56
+
57
+ ctx .close ();
48
58
}
49
59
50
60
@ Test
51
- public void withStaticBeanMethodAndInterface () {
52
- MyBeanImpl .initialized = false ;
53
-
54
- ApplicationContext ctx = new AnnotationConfigApplicationContext (ConfigWithStaticAndInterface .class );
55
- MyBean bean = ctx .getBean ("myBean" , MyBean .class );
61
+ void withStaticBeanMethodAndInterface () {
62
+ ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext (ConfigWithStaticAndInterface .class );
63
+ MyBean bean = ctx .getBean (MyBean .class );
56
64
57
65
assertThat (MyBeanImpl .initialized ).isFalse ();
58
66
bean .doIt ();
59
67
assertThat (MyBeanImpl .initialized ).isTrue ();
68
+
69
+ ctx .close ();
60
70
}
61
71
62
72
@ Test
63
- public void withNonStaticBeanMethod () {
64
- MyBeanImpl .initialized = false ;
65
-
66
- ApplicationContext ctx = new AnnotationConfigApplicationContext (ConfigWithNonStatic .class );
67
- MyBean bean = ctx .getBean ("myBean" , MyBean .class );
73
+ void withNonStaticBeanMethod () {
74
+ ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext (ConfigWithNonStatic .class );
75
+ MyBean bean = ctx .getBean (MyBean .class );
68
76
69
77
assertThat (MyBeanImpl .initialized ).isFalse ();
70
78
bean .doIt ();
71
79
assertThat (MyBeanImpl .initialized ).isTrue ();
80
+
81
+ ctx .close ();
72
82
}
73
83
74
84
@ Test
75
- public void withNonStaticBeanMethodAndInterface () {
76
- MyBeanImpl .initialized = false ;
77
-
78
- ApplicationContext ctx = new AnnotationConfigApplicationContext (ConfigWithNonStaticAndInterface .class );
79
- MyBean bean = ctx .getBean ("myBean" , MyBean .class );
85
+ void withNonStaticBeanMethodAndInterface () {
86
+ ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext (ConfigWithNonStaticAndInterface .class );
87
+ MyBean bean = ctx .getBean (MyBean .class );
80
88
81
89
assertThat (MyBeanImpl .initialized ).isFalse ();
82
90
bean .doIt ();
83
91
assertThat (MyBeanImpl .initialized ).isTrue ();
92
+
93
+ ctx .close ();
84
94
}
85
95
86
96
87
- public static interface MyBean {
97
+ interface MyBean {
88
98
89
- public String doIt ();
99
+ String doIt ();
90
100
}
91
101
92
102
93
- public static class MyBeanImpl implements MyBean {
103
+ static class MyBeanImpl implements MyBean {
94
104
95
- public static boolean initialized = false ;
105
+ static boolean initialized = false ;
96
106
97
- public MyBeanImpl () {
107
+ MyBeanImpl () {
98
108
initialized = true ;
99
109
}
100
110
@@ -110,46 +120,48 @@ public void destroy() {
110
120
111
121
112
122
@ Configuration
113
- public static class ConfigWithStatic {
123
+ static class ConfigWithStatic {
114
124
115
125
@ Bean
116
- public BeanNameAutoProxyCreator lazyInitAutoProxyCreator () {
126
+ BeanNameAutoProxyCreator lazyInitAutoProxyCreator () {
117
127
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator ();
128
+ autoProxyCreator .setBeanNames ("*" );
118
129
autoProxyCreator .setCustomTargetSourceCreators (lazyInitTargetSourceCreator ());
119
130
return autoProxyCreator ;
120
131
}
121
132
122
133
@ Bean
123
- public LazyInitTargetSourceCreator lazyInitTargetSourceCreator () {
134
+ LazyInitTargetSourceCreator lazyInitTargetSourceCreator () {
124
135
return new StrictLazyInitTargetSourceCreator ();
125
136
}
126
137
127
138
@ Bean
128
139
@ Lazy
129
- public static MyBean myBean () {
140
+ static MyBean myBean () {
130
141
return new MyBeanImpl ();
131
142
}
132
143
}
133
144
134
145
135
146
@ Configuration
136
- public static class ConfigWithStaticAndInterface implements ApplicationListener <ApplicationContextEvent > {
147
+ static class ConfigWithStaticAndInterface implements ApplicationListener <ApplicationContextEvent > {
137
148
138
149
@ Bean
139
- public BeanNameAutoProxyCreator lazyInitAutoProxyCreator () {
150
+ BeanNameAutoProxyCreator lazyInitAutoProxyCreator () {
140
151
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator ();
152
+ autoProxyCreator .setBeanNames ("*" );
141
153
autoProxyCreator .setCustomTargetSourceCreators (lazyInitTargetSourceCreator ());
142
154
return autoProxyCreator ;
143
155
}
144
156
145
157
@ Bean
146
- public LazyInitTargetSourceCreator lazyInitTargetSourceCreator () {
158
+ LazyInitTargetSourceCreator lazyInitTargetSourceCreator () {
147
159
return new StrictLazyInitTargetSourceCreator ();
148
160
}
149
161
150
162
@ Bean
151
163
@ Lazy
152
- public static MyBean myBean () {
164
+ static MyBean myBean () {
153
165
return new MyBeanImpl ();
154
166
}
155
167
@@ -160,46 +172,48 @@ public void onApplicationEvent(ApplicationContextEvent event) {
160
172
161
173
162
174
@ Configuration
163
- public static class ConfigWithNonStatic {
175
+ static class ConfigWithNonStatic {
164
176
165
177
@ Bean
166
- public BeanNameAutoProxyCreator lazyInitAutoProxyCreator () {
178
+ BeanNameAutoProxyCreator lazyInitAutoProxyCreator () {
167
179
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator ();
180
+ autoProxyCreator .setBeanNames ("*" );
168
181
autoProxyCreator .setCustomTargetSourceCreators (lazyInitTargetSourceCreator ());
169
182
return autoProxyCreator ;
170
183
}
171
184
172
185
@ Bean
173
- public LazyInitTargetSourceCreator lazyInitTargetSourceCreator () {
186
+ LazyInitTargetSourceCreator lazyInitTargetSourceCreator () {
174
187
return new StrictLazyInitTargetSourceCreator ();
175
188
}
176
189
177
190
@ Bean
178
191
@ Lazy
179
- public MyBean myBean () {
192
+ MyBean myBean () {
180
193
return new MyBeanImpl ();
181
194
}
182
195
}
183
196
184
197
185
198
@ Configuration
186
- public static class ConfigWithNonStaticAndInterface implements ApplicationListener <ApplicationContextEvent > {
199
+ static class ConfigWithNonStaticAndInterface implements ApplicationListener <ApplicationContextEvent > {
187
200
188
201
@ Bean
189
- public BeanNameAutoProxyCreator lazyInitAutoProxyCreator () {
202
+ BeanNameAutoProxyCreator lazyInitAutoProxyCreator () {
190
203
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator ();
204
+ autoProxyCreator .setBeanNames ("*" );
191
205
autoProxyCreator .setCustomTargetSourceCreators (lazyInitTargetSourceCreator ());
192
206
return autoProxyCreator ;
193
207
}
194
208
195
209
@ Bean
196
- public LazyInitTargetSourceCreator lazyInitTargetSourceCreator () {
210
+ LazyInitTargetSourceCreator lazyInitTargetSourceCreator () {
197
211
return new StrictLazyInitTargetSourceCreator ();
198
212
}
199
213
200
214
@ Bean
201
215
@ Lazy
202
- public MyBean myBean () {
216
+ MyBean myBean () {
203
217
return new MyBeanImpl ();
204
218
}
205
219
0 commit comments