zoukankan      html  css  js  c++  java
  • 20、自动装配-@Autowired&@Qualifier&@Primary

    20、自动装配-@Autowired&@Qualifier&@Primary

    • 自动装配:Spring 利用依赖注入(DI),完成对IOC容器中各个依赖关系赋值

    20.1 @Autowired :自动注入

    • 默认优先按照类型去容器中找对应的组件,applicationContext.getBean(BookRepository.class),找到就赋值。
    • 如果找到多个相同类型的组件,再将属性名称作为组件的id 去容器中查找applicationContext.getBean("bookRepository")
    • 使用 @Qualifier("bookRepository") 指定装配组件
    • 自动装配默认一定要将属性赋值好,没有就会报错。可以使用@Autowired(required = false)来配置非必须的注入,有则注入,没有就算了。
    • @Primary 让Spring进行自动装配的时候,默认选择需要装配的bean,也可以继续使用@Qualifier 指定需要装配的bean的名称
    • @Qualifier 的权重大于 @Primary,如果指定了@Qualifier@Primary失效

    20.2 代码实例

    • MainConfigOfAutowired.java
    package com.hw.springannotation.config;
    
    import com.hw.springannotation.dao.BookRepository;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    
    import javax.management.relation.RelationType;
    
    /**
     * @Description 自动装配:
     * <p>
     * Spring 利用依赖注入(DI),完成对IOC容器中各个依赖关系赋值
     * 1. @Autowire
     * @Author hw
     * @Date 2018/11/29 16:04
     * @Version 1.0
     */
    @Configuration
    @ComponentScan({"com.hw.springannotation.service", "com.hw.springannotation.controller", "com.hw.springannotation.dao"})
    public class MainConfigOfAutowired {
    
        @Primary // 首选装配bean
        @Bean("bookRepository2")
        public BookRepository bookRepository() {
            return new BookRepository("2");
        }
    }
    
    
    • BookService.java
    @Service
    public class BookService {
    
        @Autowired(required = false)
        // @Qualifier("bookRepository")
        private BookRepository bookRepository2;
    
        public void print() {
            System.out.println("bookRepository2。。。");
        }
    
        @Override
        public String toString() {
            return "BookService{" +
                    "bookRepository=" + bookRepository2 +
                    '}';
        }
    }
    
    
    • 测试用例
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAutowired.class);
    
    @Test
    public void test1() {
        BookService bookService = applicationContext.getBean(BookService.class);
        System.out.println(bookService);
    
        applicationContext.close();
    }
    

  • 相关阅读:
    Leetcode: Find Median from Data Stream
    Leetcode: Flip Game II
    Leetcode: Flip Game
    Leetcode: Nim Game
    Leetcode: Word Pattern II
    Leetcode: Word Pattern
    Leetcode: Game of Life
    Leetcode: Alien Dictionary && Summary: Topological Sort
    Leetcode: Unique Word Abbreviation
    Leetcode: Find the Duplicate Number
  • 原文地址:https://www.cnblogs.com/Grand-Jon/p/10039340.html
Copyright © 2011-2022 走看看