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解析完成自动装配功能

 

原文地址:https://www.cnblogs.com/edda/p/13449902.html