Autowired自动装配
spring利用依赖注入(DI),完成对IOC容器中的各个组件的依赖关系赋值
对同一个Dao类,既有 @Bean 注解声明,又有Autowired 自动装配,分析一下几种情况:
第一种情况
1、如果Dao类中声明了@Repository,且@ComponentScan 添加了dao扫描,则默认会创建一个testDao在IOC容器中。
2、如果在config中指定了Bean注解,此时:
a、如果Bean注解的方法名也是testDao ,则会覆盖默认对象
1.新建maven工程
1 | < groupId >com.MySpring</ groupId > < artifactId >demo</ artifactId > |
新建TestController.java TestService.java TestDao; 分别建在指定的包内
![](https://img-blog.csdnimg.cn/img_convert/cf6fb252e91524c72f967777b7efce62.png)
pom.xml 文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <? xml version = "1.0" encoding = "UTF-8" ?> < project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion >4.0.0</ modelVersion > < groupId >com.MySpring</ groupId > < artifactId >demo</ artifactId > < version >1.0-SNAPSHOT</ version > < dependencies > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-context</ artifactId > < version >5.0.6.RELEASE</ version > </ dependency > < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >4.12</ version > < scope >test</ scope > </ dependency > </ dependencies > </ project > |
1.TestController 类:
1 2 3 4 5 6 7 8 9 | package com.MySpring.demo.controller; import com.MySpring.demo.service.TestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @Controller public class TestController { @Autowired private TestService testService; } |
2.TestDao 类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package com.MySpring.demo.dao; import org.springframework.stereotype.Repository; @Repository public class TestDao { public String getMessage( ) { return message; } public void setMessage( String message ) { this .message = message; } private String message = "1" ; @Override public String toString( ) { return "TestDao{" + "message='" + message + '\ '' + '}' ; } } |
3.TestService
1 2 3 4 5 6 7 8 9 10 11 12 | package com.MySpring.demo.service; import com.MySpring.demo.dao.TestDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class TestService { @Autowired private TestDao testDao; public void print(){ System.out.printf( "[%s] from testService.\n" ,testDao); } } |
4.AppConfig
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.MySpring.demo; import com.MySpring.demo.dao.TestDao; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan ({ "com.MySpring.demo.controller" , "com.MySpring.demo.service" , "com.MySpring.demo.dao" ,}) public class AppConfig { @Bean public TestDao testDao(){ TestDao dao = new TestDao(); dao.setMessage( "2" ); return dao; } } |
5.新建测试类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.MySpring.demo; import com.MySpring.demo.dao.TestDao; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan ({ "com.MySpring.demo.controller" , "com.MySpring.demo.service" , "com.MySpring.demo.dao" ,}) public class AppConfig { @Bean public TestDao testDao(){ TestDao dao = new TestDao(); dao.setMessage( "2" ); return dao; } } |
输出结果:
![](https://img-blog.csdnimg.cn/img_convert/331e85b89b98b144412a8974699a51f4.png)
第二种情况
如果IOC容器中有两个Dao实例,则@Autowired 自动装配会根据变量名称去IOC容器中寻找,对应的实例装载,若找不到则报错,默认是必须存在,Autowired 源码如下:
当然,也可以设置required为false
1 2 3 4 5 6 7 | public @interface Autowired { /** * Declares whether the annotated dependency is required. * <p>Defaults to {@code true}. */ boolean required() default true ; } |
1. 更改AppConfig类中 Bean注解的方法名为testDao2
![](https://img-blog.csdnimg.cn/img_convert/2f9fe3a62f714ffe4aeaaa586d15e3dd.png)
2. 同时更改TestService中的 testDao变量名为 testDao2,其他保持不变:
![](https://img-blog.csdnimg.cn/img_convert/db1bf4fc53eaf7a9f1a1771546b313d3.png)
执行测试用例AppTest,得到结果,我们可以发现IOC容器中,有两个实例名称,Service中Autowired 自动装配的对象变成了2, 而通过getBean(“testDao") 得到的结果是Dao原始的结果1:
![](https://img-blog.csdnimg.cn/img_convert/d802d246c1e155bb53e91a4a9d76a15f.png)
第三种情况
在第二中情况下,如果在@Autowired 注解的变量中声明@Qualifier(“testDao”),则会优先根据Qualifier指定的名称去装载Bean对象, 修改TestService代码如下:
![](https://img-blog.csdnimg.cn/img_convert/e2923171090c93b372adf25e092903d5.png)
运行AppTest测试用例,结果如下:
![](https://img-blog.csdnimg.cn/img_convert/7bde138a09187a9df42d9abe1b1a724e.png)
第四种情况
@Qualifier与@Primary注解同时存在 在第三种情况基础上,@Bean注解中追加 @Primary注解
![](https://img-blog.csdnimg.cn/img_convert/1fafb247b817cf2d017977056484b818.png)
更改 AppTest 中获取 TestDao的方式为 app.getBean(TestDao.class) 如下:
![](https://img-blog.csdnimg.cn/img_convert/9376c2fb705d752d0ff65a2fc64384a5.png)
运行AppTest 测试用例,结果如下:
![](https://img-blog.csdnimg.cn/img_convert/57650f33df9412f5bceb7f5d89b9f808.png)
证明:
@Qualifier是根据bean id指定获取testDao, 不受@Primary影响. 但是在没有Qualifier 的地方,通过@Primary标记的Bean优先被使用。
通过TestDao{message=‘2'} 可以证明,如果不标记Primary Bean,则此处会报错,因为有两个相同实例,Spring不知道该选择哪个。
总结
1、如果Dao类中声明了@Repository,且@ComponentScan 添加了dao扫描,则默认会创建一个testDao在IOC容器中。
2、如果在config中指定了Bean注解,此时:
a、如果Bean注解的方法名也是testDao ,则会覆盖默认对象
b、如果Bean注解的方法名或者声明的别名不是testDao ,则会新创建一个对象在IOC容器中。
@Autowired 取值
如果声明的变量是testDao ,则默认会去IOC容器中获取testDao对象。
如果声明的变量是Bean注解指定的别名或者方法名,则会去容器中获取Bean注解别名或者方法名。
如果声明 的变量不是以上两者,则会报错
app.getBean()
如果在IOC容器中存在两个TestDao实例,则只能通过字符串名称来获取bean实例,如:app.getBean("testDao"),否则无法获取到对象, 有指定@Primary声明除外。
如果在IOC容器中只存在一个TestDao实例,则可以通过app.getBean(TestDao.class)获取bean对象。
@Primary 优先
只要Primary出现,不管是Autowired 还是getBean取值,都会优先取到 @Primary 声明的那个容器对象实例,但是,
如果在Autowired中声明了@Qualifier("testDao"),则在Autowired 上的取值会优先Qualifiler对象,其他地方还是会去取Primary对象