[Spring] annotation-driven development - automatic assembly - @ Autowired & @ Qualifier & @ Primary

This blog demo source address
https://github.com/suchahaerkang/spring-annotation.git

In this article we start learning automatic assembly spring, that DI (dependency injection): The IOC container interdependent components are automatically assigned
for automatic assembly spring provides some relevant comments, we first look at @Autowired this comment

1 @Autowired

@Autowired注解的作用是去容器中找它标注的那个组件,如果找到了就将其赋值
First we create three components are BookController, BookService, BookDao, and BookController dependent BookService, BookService dependent BookDao, then we pass the dependency injection @Autowired manner to the corresponding component to

/**
 * @description:
 * @author: sukang
 * @date: 2020-03-04 15:09
 */
@Controller
public class BookController {

    @Autowired
    private BookService bookService;
}

/**
 * @description:
 * @author: sukang
 * @date: 2020-03-04 15:09
 */
@ToString
@Service
public class BookService {

    @Autowired
    private BookDao bookDao;
}

/**
 * @description:
 * @author: sukang
 * @date: 2020-03-04 15:09
 */
@Repository
public class BookDao {
}

We then use this @ComponentScan three components registered to the vessel to

/**
 * @description:
 * @author: sukang
 * @date: 2020-03-07 14:05
 */
@ComponentScan({"com.wolfx.controller","com.wolfx.service", "com.wolfx.dao"})
@Configuration
public class MainConfigOfAutowired {

}

Write test cases

//测试@Autowired
@Test
public void test01(){
    //创建容器
    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAutowired.class);
    //从容器中获取BookService组件
    BookService bookService = applicationContext.getBean(BookService.class);
    System.out.println(bookService);
}

Operating results
Here Insert Picture Description
@Autowired自动注入组件的时候,首先是去容器找那个组件对应的类型,如果容器中只有一个对应组件的类型,那么就注入这个组件
then the question is, if the same types of components exist multiple containers, @ Autowired how will automatically assemble it. Here we registered a BookDao component into the container, id is bookDao2, and attributes assigned to it lable 2

@Bean("bookDao2")
public BookDao bookDao(){
    BookDao bookDao = new BookDao();
    bookDao.setLable(2);
    return bookDao;
}

Operating results
Here Insert Picture Description
from the results that we know, @ Autowired injection is the property lable = 1, type components BookDao, the reason for the automatic injection assembly lable = 1 component rather than lable = 2, because @Autowired marked components BookDao in BookService component property named bookDao. 所以我们得出结论,如果容器中有多个相同类型的组件,那么@Autowired将自动注入容器中id为属性名的那个实例
ps:如果@Autowired标注属性,自动动注入的时候在容器中没有找到属性相关类型的组件,将会报在容器中找不到那个组件的错误We put BookDao components registered to the two methods are commented out of the container, the results are as follows:
Here Insert Picture Description
solution will @Autowired changed to false, default is true

@Autowired(required = false)
private BookDao bookDao;

operation result
Here Insert Picture Description

2 @Qualifier

@Qualifier的作用是:如果容器中有多个类型相同的实例,配合注解@Autowired自动注入想要的那个实例
Let us learn this annotation
For example, we want to automatically injected id in the BookService for instance bookDao2

@Qualifier("bookDao2")
@Autowired
private BookDao bookDao;

operation result
Here Insert Picture Description

3 @Primary

@Primary的作用:容器中注册了多个相同类型的组件情况下,如果在自动转配的时候想要某个实例优先被装配,那么你可以在注册组件的时候标注上@Primary这个注解
Here we learn about. For instance, we want the instance id priority automatic assembly bookDao2, then we have to bookDao2 registered to the container when the label on @Primary

@Primary
@Bean("bookDao2")
public BookDao bookDao(){
    BookDao bookDao = new BookDao();
    bookDao.setLable(2);
    return bookDao;
}

For fear of affecting the test results, we tested the above comment out @Qualifier annotation
results are as follows
Here Insert Picture Description

Published 78 original articles · won praise 32 · views 90000 +

Guess you like

Origin blog.csdn.net/suchahaerkang/article/details/104713898