Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example analysis of @ Autowired auto-assembly, @ Bean injection @ Primary, @ Qualifier priority

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about the sample analysis of @ Autowired autoassembly, @ Bean injection @ Primary, and @ Qualifier priority. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Autowired automatic assembly

Spring uses dependency injection (DI) to complete the dependency assignment to each component in the IOC container.

For the same Dao class, there are both @ Bean annotation declaration and Autowired automatic assembly. Analyze several situations:

The first case

1. If @ Repository is declared in the Dao class and @ ComponentScan adds a dao scan, a testDao is created in the IOC container by default.

2. If the Bean annotation is specified in config, at this point:

A. If the method name of the Bean annotation is also testDao, the default object will be overwritten

1. New maven project

Com.MySpring demo

The new TestController.java TestService.java TestDao; is built in the specified package.

The pom.xml file is as follows:

4.0.0 com.MySpring demo 1.0-SNAPSHOT org.springframework spring-context 5.0.6.RELEASE junit junit 4.12 test

1.TestController class:

Package com.MySpring.demo.controller;import com.MySpring.demo.service.TestService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;@Controllerpublic class TestController {@ Autowired private TestService testService;}

2.TestDao class:

Package com.MySpring.demo.dao;import org.springframework.stereotype.Repository;@Repositorypublic class TestDao {public String getMessage () {return message;} public void setMessage (String message) {this.message = message;} private String message= "1"; @ Override public String toString () {return "TestDao {" + "message='" + message +'\'+'}' }}

3.TestService

Package com.MySpring.demo.service;import com.MySpring.demo.dao.TestDao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class TestService {@ Autowired private TestDao testDao; public void print () {System.out.printf ("[% s] from testService.\ n", testDao);}}

4.AppConfig

Package com.MySpring.demo;import com.MySpring.demo.dao.TestDao;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan ({"com.MySpring.demo.controller", "com.MySpring.demo.service", "com.MySpring.demo.dao",}) public class AppConfig {@ Bean public TestDao testDao () {TestDao dao = new TestDao (); dao.setMessage ("2") Return dao;}}

5. Create a new test class:

Package com.MySpring.demo;import com.MySpring.demo.dao.TestDao;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan ({"com.MySpring.demo.controller", "com.MySpring.demo.service", "com.MySpring.demo.dao",}) public class AppConfig {@ Bean public TestDao testDao () {TestDao dao = new TestDao (); dao.setMessage ("2") Return dao;}}

Output result:

The second case

If there are two Dao instances in the IOC container, @ Autowired automatic assembly will look in the IOC container according to the variable name, and the corresponding instance will be loaded. If it cannot be found, an error will be reported. By default, it must exist. The Autowired source code is as follows:

Of course, you can also set required to false

Public @ interface Autowired {/ * Declares whether the annotated dependency is required. *

Defaults to {@ code true}. * / boolean required () default true;}

1. The method to change the Bean annotation in the AppConfig class is named testDao2

two。 At the same time, change the name of the testDao variable in TestService to testDao2, while others remain the same:

Execute the test case AppTest and get the result. We can find that there are two instance names in the IOC container, the object automatically assembled by Autowired in Service becomes 2, and the result obtained by getBean ("testDao") is the original result of Dao 1:

The third situation

In the second case, if @ Qualifier ("testDao") is declared in the variable annotated by @ Autowired, the Bean object will be loaded first according to the name specified by Qualifier, and the TestService code will be modified as follows:

Run the AppTest test case and the results are as follows:

The fourth situation

@ Qualifier and @ Primary annotations exist at the same time on the basis of the third case, add @ Primary annotations to @ Bean annotations

Change the way to get TestDao in AppTest to app.getBean (TestDao.class) as follows:

Run the AppTest test case and the results are as follows:

Proof:

@ Qualifier is the testDao specified by bean id and is not affected by @ Primary. But where there is no Qualifier, the Bean with the @ Primary tag takes precedence.

It can be proved by TestDao {message='2'} that if Primary Bean is not marked, an error will be reported here because there are two identical instances and Spring does not know which one to choose.

Summary

1. If @ Repository is declared in the Dao class and @ ComponentScan adds a dao scan, a testDao is created in the IOC container by default.

2. If the Bean annotation is specified in config, at this point:

A. If the method name of the Bean annotation is also testDao, the default object will be overwritten

B. if the method name or declared alias of the Bean annotation is not testDao, a new object is created in the IOC container.

@ Autowired value

If the declared variable is testDao, the testDao object is obtained from the IOC container by default.

If the declared variable is an alias or method name specified by the Bean annotation, the Bean annotation alias or method name is obtained from the container.

If the declared variable is not both of the above, an error will be reported

App.getBean ()

If there are two TestDao instances in the IOC container, the bean instance can only be obtained by a string name, such as app.getBean ("testDao"), otherwise the object cannot be obtained, unless a @ Primary declaration is specified.

If there is only one TestDao instance in the IOC container, you can get the bean object through app.getBean (TestDao.class).

@ Primary priority

Whenever Primary appears, regardless of the value of Autowired or getBean, the container object instance declared by @ Primary will be given priority, but

If @ Qualifier ("testDao") is declared in Autowired, the value on Autowired will take precedence over the Qualifiler object, and the Primary object will still be fetched elsewhere

Thank you for reading! This is the end of the article on @ Autowired auto-assembly, @ Bean injection @ Primary, @ Qualifier priority sample analysis. I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report