16
16
17
17
package org .springframework .context .annotation ;
18
18
19
- import static org .hamcrest .CoreMatchers .is ;
20
- import static org .junit .Assert .assertEquals ;
21
- import static org .junit .Assert .assertFalse ;
22
- import static org .junit .Assert .assertThat ;
23
- import static org .junit .Assert .assertTrue ;
24
-
25
- import java .util .Iterator ;
19
+ import java .lang .annotation .Retention ;
20
+ import java .lang .annotation .RetentionPolicy ;
26
21
import java .util .Set ;
27
22
import java .util .regex .Pattern ;
28
23
24
+ import example .profilescan .DevComponent ;
25
+ import example .profilescan .ProfileAnnotatedComponent ;
26
+ import example .profilescan .ProfileMetaAnnotatedComponent ;
27
+ import example .scannable .FooDao ;
28
+ import example .scannable .FooService ;
29
+ import example .scannable .FooServiceImpl ;
30
+ import example .scannable .MessageBean ;
31
+ import example .scannable .NamedComponent ;
32
+ import example .scannable .NamedStubDao ;
33
+ import example .scannable .ServiceInvocationCounter ;
34
+ import example .scannable .StubFooDao ;
29
35
import org .aspectj .lang .annotation .Aspect ;
30
36
import org .junit .Test ;
37
+
31
38
import org .springframework .beans .factory .config .BeanDefinition ;
32
39
import org .springframework .core .env .ConfigurableEnvironment ;
33
40
import org .springframework .core .env .StandardEnvironment ;
39
46
import org .springframework .stereotype .Repository ;
40
47
import org .springframework .stereotype .Service ;
41
48
42
- import example .profilescan .DevComponent ;
43
- import example .profilescan .ProfileAnnotatedComponent ;
44
- import example .profilescan .ProfileMetaAnnotatedComponent ;
45
- import example .scannable .FooDao ;
46
- import example .scannable .FooService ;
47
- import example .scannable .FooServiceImpl ;
48
- import example .scannable .MessageBean ;
49
- import example .scannable .NamedComponent ;
50
- import example .scannable .NamedStubDao ;
51
- import example .scannable .ServiceInvocationCounter ;
52
- import example .scannable .StubFooDao ;
49
+ import static org .hamcrest .CoreMatchers .*;
50
+ import static org .junit .Assert .*;
53
51
54
52
/**
55
53
* @author Mark Fisher
@@ -276,9 +274,39 @@ public void testIntegrationWithAnnotationConfigApplicationContext_defaultAndDevP
276
274
}
277
275
}
278
276
277
+ @ Test
278
+ public void testIntegrationWithAnnotationConfigApplicationContext_metaProfile () {
279
+ Class <?> beanClass = MetaProfileAnnotatedComponent .class ;
280
+ String beanName = MetaProfileAnnotatedComponent .BEAN_NAME ;
281
+ {
282
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
283
+ ctx .getEnvironment ().setDefaultProfiles (TEST_DEFAULT_PROFILE_NAME );
284
+ // no active profiles are set
285
+ ctx .register (beanClass );
286
+ ctx .refresh ();
287
+ assertThat (ctx .containsBean (beanName ), is (true ));
288
+ }
289
+ {
290
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
291
+ ctx .getEnvironment ().setDefaultProfiles (TEST_DEFAULT_PROFILE_NAME );
292
+ ctx .getEnvironment ().setActiveProfiles ("dev" );
293
+ ctx .register (beanClass );
294
+ ctx .refresh ();
295
+ assertThat (ctx .containsBean (beanName ), is (true ));
296
+ }
297
+ {
298
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
299
+ ctx .getEnvironment ().setDefaultProfiles (TEST_DEFAULT_PROFILE_NAME );
300
+ ctx .getEnvironment ().setActiveProfiles ("other" );
301
+ ctx .register (beanClass );
302
+ ctx .refresh ();
303
+ assertThat (ctx .containsBean (beanName ), is (false ));
304
+ }
305
+ }
306
+
279
307
private boolean containsBeanClass (Set <BeanDefinition > candidates , Class <?> beanClass ) {
280
- for (Iterator < BeanDefinition > it = candidates . iterator (); it . hasNext (); ) {
281
- ScannedGenericBeanDefinition definition = (ScannedGenericBeanDefinition ) it . next () ;
308
+ for (BeanDefinition candidate : candidates ) {
309
+ ScannedGenericBeanDefinition definition = (ScannedGenericBeanDefinition ) candidate ;
282
310
if (beanClass .getName ().equals (definition .getBeanClassName ())) {
283
311
return true ;
284
312
}
@@ -293,9 +321,26 @@ private static class DefaultProfileAnnotatedComponent {
293
321
static final String BEAN_NAME = "defaultProfileAnnotatedComponent" ;
294
322
}
295
323
296
- @ Profile ({TEST_DEFAULT_PROFILE_NAME ,"dev" })
324
+ @ Profile ({TEST_DEFAULT_PROFILE_NAME , "dev" })
297
325
@ Component (DefaultAndDevProfileAnnotatedComponent .BEAN_NAME )
298
326
private static class DefaultAndDevProfileAnnotatedComponent {
299
327
static final String BEAN_NAME = "defaultAndDevProfileAnnotatedComponent" ;
300
328
}
329
+
330
+ @ DefaultProfile @ DevProfile
331
+ @ Component (MetaProfileAnnotatedComponent .BEAN_NAME )
332
+ private static class MetaProfileAnnotatedComponent {
333
+ static final String BEAN_NAME = "metaProfileAnnotatedComponent" ;
334
+ }
335
+
336
+ @ Profile (TEST_DEFAULT_PROFILE_NAME )
337
+ @ Retention (RetentionPolicy .RUNTIME )
338
+ public @interface DefaultProfile {
339
+ }
340
+
341
+ @ Profile ("dev" )
342
+ @ Retention (RetentionPolicy .RUNTIME )
343
+ public @interface DevProfile {
344
+ }
345
+
301
346
}
0 commit comments