文章目录
@Primary和@Qualifier
一、代码
- 下面代码中,我声明了2个TestService类型的bean,一个是@Component方式,一个是@Bean的方式,我们通过其内部的成员变量不一样来区分到底打印的是哪一个bean实例。既然有2个bean,那么问题就来了,我们知道bean有一个beanid,前面的方式是类名首字母小写,后面的方式是方法名,按照2个bean的id的是否相同,问题又分为几种情况,结合后面要引入的@Primaey和@Qualifier注解,我们按照情况的分类一个一个来看。
- TestController 代码
@Component
public class TestController {
//@Qualifier("testService2")
@Autowired
TestService testService;
public void printService() {
testService.printFlag();
}
}
- TestService 代码
@Component
public class TestService {
int flag = 1;
public void printFlag() {
System.out.println("flag:" + flag);
}
public void setFlag(int flag) {
this.flag = flag;
}
}
- Cap9MainConfig 配置类代码
@Configuration
@ComponentScan("com.intellif.ch09")
public class Cap9MainConfig {
@Bean
public TestService testService() {
TestService testService = new TestService();
testService.setFlag(2);
return testService;
}
}
- Ch9Test 测试代码
public class Ch9Test {
@Test
public void test01() {
ApplicationContext app = new AnnotationConfigApplicationContext(Cap9MainConfig.class);
System.out.println("The beans in the IOC container are listed as follows: ");
//1.打印IOC容器中所有的 实例对象名称
String[] names = app.getBeanDefinitionNames();
for ( String name : names ) {
System.out.println( name );
}
System.out.println("Get Controller bean and check the service bean:");
//2.通过类型获取
TestController testController = (TestController) app.getBean(TestController.class);
testController.printService();
//3.通过类型获取
System.out.println("Get the service bean by type:");
TestService testService = (TestService) app.getBean(TestService.class);
testService.printFlag();
//4.通过Id获取
System.out.println("Get the service bean by beanId :");
TestService testService1 = (TestService) app.getBean("testService");
testService1.printFlag();
((AnnotationConfigApplicationContext) app).close();
}
}
情况1: bean Id一致
- bean Id一致,就是我们上面的代码,我们打印的情况如下,我们看到容器中只有一个testService的bean,也就是说在beanid一样的情况下,@Bean将@Component的bean覆盖了。
到此我们得到结论1。 - 结论1:在beanid一样的情况下,@Bean注入的bean将@Component注入的bean覆盖了
The beans in the IOC container are listed as follows:
cap9MainConfig
testController
testService
Get Controller bean and check the service bean:
flag:2
Get the service bean by type:
flag:2
Get the service bean by beanId :
flag:2
情况2: bean Id不一致
-
这里我们将@Bean的名字修改一下:@Bean(“testService2”),然后测试结果如下,我们看到容器中包含testService和testService2两个bean,Controller中注入的是@Compnent的那个,但是通过类型获取bean报错了,提示TestService类型的bean有 testService,testService2这2个,到这里我们可以得出两个结论:
-
结论2:@Autowire注入的bean,是通过变量名字去寻找bean的,变量名就是bean id。(这里我们修改TestController中@Autowire TestService的变量名字为testService2,我们会发现打印的就是2)。
-
结论3:容器中存在多个同类型bean时,通过类型获取会报错,只能通过id获取。(这里我们将测试代码中的通过id获取放在前面就发现,通过id获取是ok的)
The beans in the IOC container are listed as follows:
cap9MainConfig
testController
testService
testService2
Get Controller bean and check the service bean:
flag:1
Get the service bean by type:
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.intellif.ch09.service.TestService' available: expected single matching bean but found 2: testService,testService2
情况3: bean Id不一致,声明@Primary
-
如果出现了情况2,通过类型获取会失败,那是不是说容器内同类型bean有多个的时候,不能通过类型获取呢?也还是可以的,我们可以指定一个老大,比如同类型存在bean1,bean2,bean3…,我们指定一个老大,通过类型获取的时候就获取这个老大,在其他的地方注入的时候,也会注入这个老大。测试的方法就是在TestService类上加上@Primary,打印如下,我们发现还是有2个bean,但是Controller中注入的,通过类型获取的都是@Primary标注的那个,这就是@Primary注解的作用,到此我们得出结论。
-
结论4:同类型存在多个bean时,申明了@Primary的bean在被使用时会被优先使用。
比如app.getBean(TestService.class)通过类型从容器获取bean,或者@Autowire注入(这里注入即使改变变量名指定id也没用了,我们将变量名字改为testService2,发现打印的还是1),仿佛只要@Primary这个bean存在,你就只能看到这个bean,看不到其他的了。 -
结论5:@Primary指定了bean时,@Autowire的变量名代表注入bean的bean id无效了,会直接注入@Primary标注的bean
The beans in the IOC container are listed as follows:
cap9MainConfig
testController
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@d70c109: startup date [Sun May 05 15:30:27 CST 2019]; root of context hierarchy
testService
testService2
Get Controller bean and check the service bean:
flag:1
Get the service bean by type:
flag:1
Get the service bean by beanId :
flag:1
情况4: bean Id不一致,声明@Primary和@Qualifier
-
沿着情况3,感觉有点简单粗暴啊,如果有3个同类型的bean,bean1申明了@Primary,但是我想注入bean2或其他的怎么办?那么就使用@Qualifier,在第三步的基础上,我们在Controller中使用该注解,打印如下,
我们发现,通过类型获取到的还是bean1,但是Controller中注入的却是bean2,因为我们通过@Qualifier(“testService2”)指定了bean id,到此我们有结论: -
结论6:@Qualifier注解可以指定注入bean的beanid,此时只会注入指定beanid的bean,Primary的限制此时不生效了,注意如果指定的beanid有误,那么会报错的。对于没有使用@Qualifier来获取指定bean的地方,依赖注入的还是@Primary标注的bean
@Component
public class TestController {
@Qualifier("testService2")
@Autowired
TestService testService2;
public void printService() {
testService2.printFlag();
}
}
打印:
The beans in the IOC container are listed as follows:
cap9MainConfig
testController
testService
testService2
Get Controller bean and check the service bean:
flag:2
Get the service bean by type:
flag:1
Get the service bean by beanId :
flag:1
情况5: bean Id不一致,只声明@Qualifier
- 在情况4中我们的分析可以看出,@Qualifier指定了beanid的情况下,Primary是对我这个类是无效的,不管众多bean中哪一个有@Primary哪一个没有,我只要我自己@Qualifier指定的那个。因此在没有@Primary的情况下也是一样的,只会去找@Qualifier指定的bean,和Primary没关系。
二、总结
- 1.如果2种方式注入bean且bean id一样时,@Bean注册的方式会覆盖@Component扫描注册的bean。
- 2.容器中存在多个同类型bean时,但是注入的beanid不一样,那么通过@Autowire注入会去找id和自己相符的那一个,变量名就是bean id,
- 3.容器中存在多个同类型bean时,通过类型获取会报错,只能通过id获取。
- 4.容器中存在多个同类型bean时,声明了@Primary的bean在被使用时会被优先使用。比如app.getBean(TestService.class)通过类型从容器获取bean,或者@Autowire注入(这里注入即使改变变量名指定id也没用了),仿佛只要@Primary这个bean存在,你就只能看到这个bean,看不到其他的了。
- 5.容器中存在多个同类型bean时,@Qualifier注解可以指定注入bean的beanid,此时只会注入指定beanid的bean,@Primary的限制此时不生效了,注意如果指定的beanid有误,那么会报错的。
三、个人理解
- @Primary意思是,在A类的多个bean里面,我的优先级最高,相当于看不到其他A类型的bean了,@Primary更像是很多bean候选者里面的一个选择准则,当有多个的时候,优先选我。@Qualifier意思是,我依赖注入A类的bean,我只要我指定id的那一个,其他的我不认,@Qualifier则是选择者自己知道自己要哪一个,不管是1个还是多个,我只要我指定的哪一个。前者是提供方提供时候的策略,后者是使用方的选择策略。