74
74
* @since 3.1
75
75
*/
76
76
@ SuppressWarnings ("resource" )
77
- public class ComponentScanAnnotationIntegrationTests {
77
+ class ComponentScanAnnotationIntegrationTests {
78
78
79
79
@ Test
80
- public void controlScan () {
80
+ void controlScan () {
81
81
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
82
82
ctx .scan (example .scannable .PackageMarker .class .getPackage ().getName ());
83
83
ctx .refresh ();
84
- assertThat (ctx .containsBean ("fooServiceImpl" )).as (
85
- "control scan for example.scannable package failed to register FooServiceImpl bean" ).isTrue ();
84
+ assertContextContainsBean (ctx , "fooServiceImpl" );
86
85
}
87
86
88
87
@ Test
89
- public void viaContextRegistration () {
90
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
91
- ctx .register (ComponentScanAnnotatedConfig .class );
92
- ctx .refresh ();
88
+ void viaContextRegistration () {
89
+ AnnotationConfigApplicationContext ctx =
90
+ new AnnotationConfigApplicationContext (ComponentScanAnnotatedConfig .class );
93
91
ctx .getBean (ComponentScanAnnotatedConfig .class );
94
92
ctx .getBean (TestBean .class );
95
- assertThat (ctx .containsBeanDefinition ("componentScanAnnotatedConfig" )).as ("config class bean not found" )
96
- .isTrue ();
93
+ assertThat (ctx .containsBeanDefinition ("componentScanAnnotatedConfig" )).as ("config class bean not found" ).isTrue ();
97
94
assertThat (ctx .containsBean ("fooServiceImpl" )).as ("@ComponentScan annotated @Configuration class registered directly against " +
98
- "AnnotationConfigApplicationContext did not trigger component scanning as expected" )
99
- .isTrue ();
95
+ "AnnotationConfigApplicationContext did not trigger component scanning as expected" ).isTrue ();
100
96
}
101
97
102
98
@ Test
103
- public void viaContextRegistration_WithValueAttribute () {
104
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
105
- ctx .register (ComponentScanAnnotatedConfig_WithValueAttribute .class );
106
- ctx .refresh ();
99
+ void viaContextRegistration_WithValueAttribute () {
100
+ AnnotationConfigApplicationContext ctx =
101
+ new AnnotationConfigApplicationContext (ComponentScanAnnotatedConfig_WithValueAttribute .class );
107
102
ctx .getBean (ComponentScanAnnotatedConfig_WithValueAttribute .class );
108
103
ctx .getBean (TestBean .class );
109
- assertThat (ctx .containsBeanDefinition ("componentScanAnnotatedConfig_WithValueAttribute" )).as ("config class bean not found" )
110
- .isTrue ();
104
+ assertThat (ctx .containsBeanDefinition ("componentScanAnnotatedConfig_WithValueAttribute" )).as ("config class bean not found" ).isTrue ();
111
105
assertThat (ctx .containsBean ("fooServiceImpl" )).as ("@ComponentScan annotated @Configuration class registered directly against " +
112
- "AnnotationConfigApplicationContext did not trigger component scanning as expected" )
113
- .isTrue ();
106
+ "AnnotationConfigApplicationContext did not trigger component scanning as expected" ).isTrue ();
114
107
}
115
108
116
109
@ Test
117
- public void viaContextRegistration_FromPackageOfConfigClass () {
118
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
119
- ctx .register (ComponentScanAnnotatedConfigWithImplicitBasePackage .class );
120
- ctx .refresh ();
110
+ void viaContextRegistration_FromPackageOfConfigClass () {
111
+ AnnotationConfigApplicationContext ctx =
112
+ new AnnotationConfigApplicationContext (ComponentScanAnnotatedConfigWithImplicitBasePackage .class );
121
113
ctx .getBean (ComponentScanAnnotatedConfigWithImplicitBasePackage .class );
122
- assertThat (ctx .containsBeanDefinition ("componentScanAnnotatedConfigWithImplicitBasePackage" )).as ("config class bean not found" )
123
- .isTrue ();
114
+ assertThat (ctx .containsBeanDefinition ("componentScanAnnotatedConfigWithImplicitBasePackage" )).as ("config class bean not found" ).isTrue ();
124
115
assertThat (ctx .containsBean ("scannedComponent" )).as ("@ComponentScan annotated @Configuration class registered directly against " +
125
- "AnnotationConfigApplicationContext did not trigger component scanning as expected" )
126
- .isTrue ();
127
- assertThat (ctx .getBean (ConfigurableComponent .class ).isFlag ()).as ("@Bean method overrides scanned class" )
128
- .isTrue ();
116
+ "AnnotationConfigApplicationContext did not trigger component scanning as expected" ).isTrue ();
117
+ assertThat (ctx .getBean (ConfigurableComponent .class ).isFlag ()).as ("@Bean method overrides scanned class" ).isTrue ();
129
118
}
130
119
131
120
@ Test
132
- public void viaContextRegistration_WithComposedAnnotation () {
133
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
134
- ctx .register (ComposedAnnotationConfig .class );
135
- ctx .refresh ();
121
+ void viaContextRegistration_WithComposedAnnotation () {
122
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ComposedAnnotationConfig .class );
136
123
ctx .getBean (ComposedAnnotationConfig .class );
137
124
ctx .getBean (SimpleComponent .class );
138
125
ctx .getBean (ClassWithNestedComponents .NestedComponent .class );
139
126
ctx .getBean (ClassWithNestedComponents .OtherNestedComponent .class );
140
- assertThat (ctx .containsBeanDefinition ("componentScanAnnotationIntegrationTests.ComposedAnnotationConfig" )).as ("config class bean not found" )
141
- .isTrue ();
127
+ assertThat (ctx .containsBeanDefinition ("componentScanAnnotationIntegrationTests.ComposedAnnotationConfig" )).as ("config class bean not found" ).isTrue ();
142
128
assertThat (ctx .containsBean ("simpleComponent" )).as ("@ComponentScan annotated @Configuration class registered directly against " +
143
- "AnnotationConfigApplicationContext did not trigger component scanning as expected" )
144
- .isTrue ();
129
+ "AnnotationConfigApplicationContext did not trigger component scanning as expected" ).isTrue ();
145
130
}
146
131
147
132
@ Test
@@ -154,7 +139,7 @@ void multipleComposedComponentScanAnnotations() { // gh-30941
154
139
}
155
140
156
141
@ Test
157
- public void viaBeanRegistration () {
142
+ void viaBeanRegistration () {
158
143
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
159
144
bf .registerBeanDefinition ("componentScanAnnotatedConfig" ,
160
145
genericBeanDefinition (ComponentScanAnnotatedConfig .class ).getBeanDefinition ());
@@ -164,54 +149,50 @@ public void viaBeanRegistration() {
164
149
ctx .refresh ();
165
150
ctx .getBean (ComponentScanAnnotatedConfig .class );
166
151
ctx .getBean (TestBean .class );
167
- assertThat (ctx .containsBeanDefinition ("componentScanAnnotatedConfig" )).as ("config class bean not found" )
168
- .isTrue ();
152
+ assertThat (ctx .containsBeanDefinition ("componentScanAnnotatedConfig" )).as ("config class bean not found" ).isTrue ();
169
153
assertThat (ctx .containsBean ("fooServiceImpl" )).as ("@ComponentScan annotated @Configuration class registered as bean " +
170
- "definition did not trigger component scanning as expected" )
171
- .isTrue ();
154
+ "definition did not trigger component scanning as expected" ).isTrue ();
172
155
}
173
156
174
157
@ Test
175
- public void withCustomBeanNameGenerator () {
176
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
177
- ctx .register (ComponentScanWithBeanNameGenerator .class );
178
- ctx .refresh ();
179
- assertThat (ctx .containsBean ("custom_fooServiceImpl" )).isTrue ();
180
- assertThat (ctx .containsBean ("fooServiceImpl" )).isFalse ();
158
+ void withCustomBeanNameGenerator () {
159
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ComponentScanWithBeanNameGenerator .class );
160
+ assertContextContainsBean (ctx , "custom_fooServiceImpl" );
161
+ assertContextDoesNotContainBean (ctx , "fooServiceImpl" );
181
162
}
182
163
183
164
@ Test
184
- public void withScopeResolver () {
165
+ void withScopeResolver () {
185
166
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ComponentScanWithScopeResolver .class );
186
167
// custom scope annotation makes the bean prototype scoped. subsequent calls
187
168
// to getBean should return distinct instances.
188
169
assertThat (ctx .getBean (CustomScopeAnnotationBean .class )).isNotSameAs (ctx .getBean (CustomScopeAnnotationBean .class ));
189
- assertThat (ctx . containsBean ( "scannedComponent" )). isFalse ( );
170
+ assertContextDoesNotContainBean (ctx , "scannedComponent" );
190
171
}
191
172
192
173
@ Test
193
- public void multiComponentScan () {
174
+ void multiComponentScan () {
194
175
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (MultiComponentScan .class );
195
176
assertThat (ctx .getBean (CustomScopeAnnotationBean .class )).isNotSameAs (ctx .getBean (CustomScopeAnnotationBean .class ));
196
- assertThat (ctx . containsBean ( "scannedComponent" )). isTrue ( );
177
+ assertContextContainsBean (ctx , "scannedComponent" );
197
178
}
198
179
199
180
@ Test
200
- public void withCustomTypeFilter () {
181
+ void withCustomTypeFilter () {
201
182
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ComponentScanWithCustomTypeFilter .class );
202
183
assertThat (ctx .getDefaultListableBeanFactory ().containsSingleton ("componentScanParserTests.KustomAnnotationAutowiredBean" )).isFalse ();
203
184
KustomAnnotationAutowiredBean testBean = ctx .getBean ("componentScanParserTests.KustomAnnotationAutowiredBean" , KustomAnnotationAutowiredBean .class );
204
185
assertThat (testBean .getDependency ()).isNotNull ();
205
186
}
206
187
207
188
@ Test
208
- public void withAwareTypeFilter () {
189
+ void withAwareTypeFilter () {
209
190
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ComponentScanWithAwareTypeFilter .class );
210
191
assertThat (ctx .getEnvironment ().matchesProfiles ("the-filter-ran" )).isTrue ();
211
192
}
212
193
213
194
@ Test
214
- public void withScopedProxy () throws IOException , ClassNotFoundException {
195
+ void withScopedProxy () throws Exception {
215
196
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
216
197
ctx .register (ComponentScanWithScopedProxy .class );
217
198
ctx .getBeanFactory ().registerScope ("myScope" , new SimpleMapScope ());
@@ -228,7 +209,7 @@ public void withScopedProxy() throws IOException, ClassNotFoundException {
228
209
}
229
210
230
211
@ Test
231
- public void withScopedProxyThroughRegex () throws IOException , ClassNotFoundException {
212
+ void withScopedProxyThroughRegex () throws IOException , ClassNotFoundException {
232
213
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
233
214
ctx .register (ComponentScanWithScopedProxyThroughRegex .class );
234
215
ctx .getBeanFactory ().registerScope ("myScope" , new SimpleMapScope ());
@@ -240,7 +221,7 @@ public void withScopedProxyThroughRegex() throws IOException, ClassNotFoundExcep
240
221
}
241
222
242
223
@ Test
243
- public void withScopedProxyThroughAspectJPattern () throws IOException , ClassNotFoundException {
224
+ void withScopedProxyThroughAspectJPattern () throws IOException , ClassNotFoundException {
244
225
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
245
226
ctx .register (ComponentScanWithScopedProxyThroughAspectJPattern .class );
246
227
ctx .getBeanFactory ().registerScope ("myScope" , new SimpleMapScope ());
@@ -252,41 +233,41 @@ public void withScopedProxyThroughAspectJPattern() throws IOException, ClassNotF
252
233
}
253
234
254
235
@ Test
255
- public void withMultipleAnnotationIncludeFilters1 () throws IOException , ClassNotFoundException {
256
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
257
- ctx .register (ComponentScanWithMultipleAnnotationIncludeFilters1 .class );
258
- ctx .refresh ();
236
+ void withMultipleAnnotationIncludeFilters1 () throws IOException , ClassNotFoundException {
237
+ AnnotationConfigApplicationContext ctx =
238
+ new AnnotationConfigApplicationContext (ComponentScanWithMultipleAnnotationIncludeFilters1 .class );
259
239
ctx .getBean (DefaultNamedComponent .class ); // @CustomStereotype-annotated
260
240
ctx .getBean (MessageBean .class ); // @CustomComponent-annotated
261
241
}
262
242
263
243
@ Test
264
- public void withMultipleAnnotationIncludeFilters2 () throws IOException , ClassNotFoundException {
265
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
266
- ctx .register (ComponentScanWithMultipleAnnotationIncludeFilters2 .class );
267
- ctx .refresh ();
244
+ void withMultipleAnnotationIncludeFilters2 () throws IOException , ClassNotFoundException {
245
+ AnnotationConfigApplicationContext ctx =
246
+ new AnnotationConfigApplicationContext (ComponentScanWithMultipleAnnotationIncludeFilters2 .class );
268
247
ctx .getBean (DefaultNamedComponent .class ); // @CustomStereotype-annotated
269
248
ctx .getBean (MessageBean .class ); // @CustomComponent-annotated
270
249
}
271
250
272
251
@ Test
273
- public void withBasePackagesAndValueAlias () {
274
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
275
- ctx .register (ComponentScanWithBasePackagesAndValueAlias .class );
276
- ctx .refresh ();
277
- assertThat (ctx .containsBean ("fooServiceImpl" )).isTrue ();
252
+ void withBasePackagesAndValueAlias () {
253
+ AnnotationConfigApplicationContext ctx =
254
+ new AnnotationConfigApplicationContext (ComponentScanWithBasePackagesAndValueAlias .class );
255
+ assertContextContainsBean (ctx , "fooServiceImpl" );
278
256
}
279
257
280
258
private static void assertContextContainsBean (ApplicationContext ctx , String beanName ) {
281
- assertThat (ctx .containsBean (beanName )).as ("context contains bean " + beanName ).isTrue ();
259
+ assertThat (ctx .containsBean (beanName )).as ("context should contain bean " + beanName ).isTrue ();
260
+ }
261
+ private static void assertContextDoesNotContainBean (ApplicationContext ctx , String beanName ) {
262
+ assertThat (ctx .containsBean (beanName )).as ("context should not contain bean " + beanName ).isFalse ();
282
263
}
283
264
284
265
285
266
@ Configuration
286
267
@ ComponentScan
287
268
@ Retention (RetentionPolicy .RUNTIME )
288
269
@ Target (ElementType .TYPE )
289
- public @interface ComposedConfiguration {
270
+ @interface ComposedConfiguration {
290
271
291
272
@ AliasFor (annotation = ComponentScan .class )
292
273
String [] basePackages () default {};
@@ -296,22 +277,22 @@ private static void assertContextContainsBean(ApplicationContext ctx, String bea
296
277
@ ComponentScan
297
278
@ Retention (RetentionPolicy .RUNTIME )
298
279
@ Target (ElementType .TYPE )
299
- public @interface ComposedConfiguration2 {
280
+ @interface ComposedConfiguration2 {
300
281
301
282
@ AliasFor (annotation = ComponentScan .class )
302
283
String [] basePackages () default {};
303
284
}
304
285
305
286
@ ComposedConfiguration (basePackages = "org.springframework.context.annotation.componentscan.simple" )
306
- public static class ComposedAnnotationConfig {
287
+ static class ComposedAnnotationConfig {
307
288
}
308
289
309
290
@ ComposedConfiguration (basePackages = "org.springframework.context.annotation.componentscan.simple" )
310
291
@ ComposedConfiguration2 (basePackages = "example.scannable.sub" )
311
292
static class MultipleComposedAnnotationsConfig {
312
293
}
313
294
314
- public static class AwareTypeFilter implements TypeFilter , EnvironmentAware ,
295
+ static class AwareTypeFilter implements TypeFilter , EnvironmentAware ,
315
296
ResourceLoaderAware , BeanClassLoaderAware , BeanFactoryAware {
316
297
317
298
private BeanFactory beanFactory ;
0 commit comments