美文网首页
Spring注解08 自动装配 @Autowired @Qual

Spring注解08 自动装配 @Autowired @Qual

作者: 運河的縴夫 | 来源:发表于2019-01-06 11:48 被阅读0次

    自动装配:

    Spring利用依赖注入(DI),完成对IOC容器中中各个组件的依赖关系赋值;

    自动装配用到三个注解:

    • @Autowired 自动装配
    • @Qualifier 指定自动装配bean的name
    • @Primary 指定装配的优先级,这个指定的优先级最高。

    @Autowired自动注入:

    • 1)、默认优先按照类型去容器中找对应的组件:applicationContext.getBean(BookDao.class);找到就赋值。
    • 2)、如果找到多个相同类型的组件,再将属性的名称作为组件的id去容器中查找applicationContext.getBean("bookDao")
    • 3)、@Qualifier("bookDao"):使用@Qualifier指定需要装配的组件的id,而不是使用属性名
    • 4)、自动装配默认一定要将属性赋值好,没有就会报错;可以使用@Autowired(required=false);
    • 5)、@Primary:让Spring进行自动装配的时候,默认使用首选的bean;也可以继续使用@Qualifier指定需要装配的bean的名字

    Spring还支持使用@Resource(JSR250)和@Inject(JSR330)[java规范的注解]

    • @Resource:
      可以和@Autowired一样实现自动装配功能;默认是按照组件名称进行装配的;
      没有能支持@Primary功能没有支持@Autowired(reqiured=false);
    • @Inject:
      需要导入javax.inject的包,和Autowired的功能一样。没有required=false的功能;
    • @Autowired:Spring定义的: @Resource、@Inject都是java规范
      AutowiredAnnotationBeanPostProcessor:解析完成自动装配功能;

    @Autowired:构造器,参数,方法,属性;都是从容器中获取参数组件的值

    • 1)、[标注在方法位置]:@Bean+方法参数;参数从容器中获取;默认不写@Autowired效果是一样的;都能自动装配
    • 2)、[标在构造器上]:如果组件只有一个有参构造器,这个有参构造器的@Autowired可以省略,参数位置的组件还是可以自动从容器中获取
    • 3)、放在参数位置:

    自定义组件想要使用Spring容器底层的一些组件(ApplicationContext,BeanFactory,xxx);

    自定义组件实现xxxAware;在创建对象的时候,会调用接口规定的方法注入相关组件;Aware;
    把Spring底层一些组件注入到自定义的Bean中;
    xxxAware:功能使用xxxProcessor;
    ApplicationContextAware==》ApplicationContextAwareProcessor;

    直接鲁代码

    • 配置类
    @Configuration
    @ComponentScan(value = {"com.tommy.service", "com.tommy.controller", "com.tommy.dao"})
    public class AutowaireConfig {
        @Bean
        @Primary
        public PersonDao personDao(){
            final PersonDao personDao = new PersonDao();
            personDao.setLabel("2");
            return personDao;
        }
    }
    
    //名字默认是类名首字母小写
    @Repository
    @Data
    @Primary
    public class PersonDao {
        public PersonDao() {
        }
        private String label = "1";
        public Person getPerson(){
            return new Person("jm",16,"tommy");
        }
    }
    
    @Service
    public class PersonService {
        // @Autowired
        //@Resource
        @Inject
        private PersonDao personDao;
        public Person getPerson() {
            return personDao.getPerson();
        }
    }
    

    测试类

    public class IOCTest_Autowaire {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AutowaireConfig.class);
        @Test
        public void testAutowaire(){
            final String[] beanNamesForType = applicationContext.getBeanDefinitionNames();
            for (String name :beanNamesForType) {
                System.out.println(name);
            }
            final Object personService = applicationContext.getBean("personService");
            System.out.println("personService "+personService);
            final PersonDao personDao = (PersonDao) applicationContext.getBean("personDao");
            System.out.println("lable "+personDao.getLabel());
        }
    }
    

    相关文章

      网友评论

          本文标题:Spring注解08 自动装配 @Autowired @Qual

          本文链接:https://www.haomeiwen.com/subject/bhqsrqtx.html