spring注解-自动装配
Spring利用依赖注入(DI)完成对IOC容器中中各个组件的依赖关系赋值
一、@Autowired
- 默认优先按照类型去容器中找对应的组件(applicationContext.getBean(BookDao.class))
- 如果找到多个相同类型的组件,再将属性的名称作为组件的id去容器中查找(applicationContext.getBean("bookDao"))
- 自动装配默认一定要将属性赋值好,没有就会报错;可以使用@Autowired(required=false);
标注的位置
构造器,参数,方法,属性;
- 标注在方法位置:构建bean的时候可以不在参数处写@Autowired,参数的值自动从容器中获取;而setXXX需要加上@Autowired
@Autowired
public void setCar(Car car) {
this.car = car;
}
@Bean
public Color color(Car car){
Color color = new Color();
color.setCar(car);
return color;
}
- 标注在构造器上:如果组件只有一个有参构造器,这个有参构造器的@Autowired可以省略,参数位置的组件自动从容器中获取
public Boss(Car car){
this.car = car;
System.out.println("Boss...有参构造器");
}
- 标注在参数位置
public Boss(@AutoWired Car car){
this.car = car;
System.out.println("Boss...有参构造器");
}
@Qualifier
@Qualifier("xxxDao")指定需要装配的组件的id,而不是使用属性名
@Primary
让Spring进行自动装配的时候,默认使用首选的bean;也可以继续使用@Qualifier指定需要装配的bean的名字
二、@Resource
- 默认是按照组件名称进行装配的,也可以通过name属性指定
- 不支持@Primary功能、reqiured=false
三、@Inject
- 需要导入javax.inject的包,和Autowired的功能大致一样。没有required=false的功能
四、实现xxxAware使用Spring容器底层的组件
实现原理就是在创建bean后它的一系列后置处理器(XXXBeanPostProcessor)会进行前置处理,如果满足条件则会调用接口规定的方法注入相关组件
@Component public class Red implements ApplicationContextAware,BeanNameAware,EmbeddedValueResolverAware { private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { // TODO Auto-generated method stub System.out.println("传入的ioc:"+applicationContext); this.applicationContext = applicationContext; } @Override public void setBeanName(String name) { // TODO Auto-generated method stub System.out.println("当前bean的名字:"+name); } @Override public void setEmbeddedValueResolver(StringValueResolver resolver) { // TODO Auto-generated method stub String resolveStringValue = resolver.resolveStringValue("你好 ${os.name} 我是 #{20*18}"); System.out.println("解析的字符串:"+resolveStringValue); }
五、@Profile
根据当前环境,动态的激活和切换一系列组件的功能
- 加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中。默认是default环境
- 写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能开始生效
- 有标注环境标识的bean在,任何环境下都是加载的(配置类生效的情况下)
//1、使用命令行动态参数: 在虚拟机参数位置加载 -Dspring.profiles.active=test //2、代码的方式激活某种环境 @Test public void test01(){ AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); //1、创建一个applicationContext //2、设置需要激活的环境 applicationContext.getEnvironment().setActiveProfiles("dev"); //3、注册主配置类 applicationContext.register(MainConfigOfProfile.class); //4、启动刷新容器 applicationContext.refresh(); String[] namesForType = applicationContext.getBeanNamesForType(DataSource.class); for (String string : namesForType) { System.out.println(string); } Yellow bean = applicationContext.getBean(Yellow.class); System.out.println(bean); applicationContext.close(); }
总结
@Autowired:Spring定义的;@Resource、@Inject都是java规范。它们都是通过AutowiredAnnotationBeanPostProcessor解析完成自动装配功能
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了