Skip to content

Commit 1c2b1d2

Browse files
committed
Demonstrate use of @configuration as meta-annotation
Issue: SPR-9090
1 parent 58e270f commit 1c2b1d2

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.context.annotation.configuration;
18+
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
22+
import org.junit.Test;
23+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.context.annotation.Configuration;
26+
27+
import test.beans.TestBean;
28+
29+
import static org.hamcrest.CoreMatchers.*;
30+
import static org.junit.Assert.*;
31+
32+
/**
33+
* Ensures that @Configuration is supported properly as a meta-annotation.
34+
*
35+
* @author Chris Beams
36+
*/
37+
public class ConfigurationMetaAnnotationTests {
38+
39+
@Test
40+
public void customConfigurationStereotype() {
41+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
42+
ctx.register(Config.class);
43+
ctx.refresh();
44+
assertThat(ctx.containsBean("customName"), is(true));
45+
TestBean a = ctx.getBean("a", TestBean.class);
46+
TestBean b = ctx.getBean("b", TestBean.class);
47+
assertThat(b, sameInstance(a.getSpouse()));
48+
}
49+
50+
51+
@TestConfiguration("customName")
52+
static class Config {
53+
@Bean
54+
public TestBean a() {
55+
TestBean a = new TestBean();
56+
a.setSpouse(b());
57+
return a;
58+
}
59+
60+
@Bean
61+
public TestBean b() {
62+
return new TestBean();
63+
}
64+
}
65+
66+
67+
@Configuration
68+
@Retention(RetentionPolicy.RUNTIME)
69+
public @interface TestConfiguration {
70+
String value() default "";
71+
}
72+
}

0 commit comments

Comments
 (0)