Skip to content

Commit 0dc29cb

Browse files
committed
Added a test to prove that @qualifier works in conjunction with @bean methods after some confusion by users that it may not.
1 parent 5801af9 commit 0dc29cb

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.springframework.context.annotation.configuration;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.junit.Assert.assertThat;
5+
6+
import org.junit.Test;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.beans.factory.annotation.Qualifier;
9+
import org.springframework.context.ApplicationContext;
10+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.context.annotation.Configuration;
13+
import org.springframework.stereotype.Component;
14+
15+
import test.beans.TestBean;
16+
17+
/**
18+
* Tests proving that @Qualifier annotations work when used
19+
* with @Configuration classes on @Bean methods.
20+
*
21+
* @author Chris Beams
22+
*/
23+
public class BeanMethodQualificationTests {
24+
25+
@Test
26+
public void test() {
27+
ApplicationContext ctx =
28+
new AnnotationConfigApplicationContext(Config.class, Pojo.class);
29+
Pojo pojo = ctx.getBean(Pojo.class);
30+
assertThat(pojo.testBean.getName(), equalTo("interesting"));
31+
}
32+
33+
@Configuration
34+
static class Config {
35+
@Bean
36+
@Qualifier("interesting")
37+
public TestBean testBean1() {
38+
return new TestBean("interesting");
39+
}
40+
41+
@Bean
42+
@Qualifier("boring")
43+
public TestBean testBean2() {
44+
return new TestBean("boring");
45+
}
46+
}
47+
48+
@Component
49+
static class Pojo {
50+
@Autowired @Qualifier("interesting") TestBean testBean;
51+
}
52+
}

0 commit comments

Comments
 (0)