In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you why Spring and IDEA do not recommend the use of @ Autowired annotations, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can learn something after reading this article, let's take a look at it.
Why is Spring not recommended to use @ Autowired to annotate background
Developers may find that idea adds a warning to the @ Autowired annotation that we often use.
The warning reads: Field injection is not recommended, attribute injection is not recommended
We click on the three dots on the right to view the description, and we can see the information as follows
Reason description: Inspection info: Spring Team recommends: "Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies".
The Spring team recommends that you always use constructor-based dependency injection in your bean. Always use assertions on forced dependencies
Reason
Why does Spring suggest that we use construction injection in Bean?
To answer this question, we need to understand the dependency injection (DI) approach of Spring
The common injection methods of Spring are: simple type injection, set type injection, automatic domain attribute injection, automatic injection of categories, null injection, and construction injection.
Can be simplified as: attribute injection, constructor injection, set method injection
Next, use the code to show the following three ways to inject
Attribute injection
As you can see, the most common thing we use in development is attribute injection.
@ RestControllerpublic class AppointmentNumberConfigurationController {@ Autowired private AppointmentNumberConfigurationService numberConfigurationService;}
Set method injection
The @ Autowired annotation is also used for set method injection, but it is used differently from attribute injection.
Property injection is used on member variables, while the set method is used on the Setter function of member variables.
@ RestControllerpublic class AppointmentNumberConfigurationController {private AppointmentNumberConfigurationService numberConfigurationService; @ Autowired public void setNumberConfigurationService (AppointmentNumberConfigurationService numberConfigurationService) {this.numberConfigurationService = numberConfigurationService;}
Construction method injection
Constructor Injection is constructor injection and is the most recommended way to use it.
However, it will be troublesome to construct the injection according to this process for each injection.
As for how to simplify this step, we can move on.
@ RestControllerpublic class AppointmentNumberConfigurationController {final AppointmentNumberConfigurationService numberConfigurationService; public AppointmentNumberConfigurationController (AppointmentNumberConfigurationService numberConfigurationService) {this.numberConfigurationService = numberConfigurationService;}}
The comparison of the three ways is as follows
Problems that may arise with attribute injection
Based on attribute injection, it violates the principle of single responsibility.
Because today's business generally uses a lot of dependencies, but having too many dependencies usually means taking on more responsibility, which clearly violates the principle of single responsibility.
And the class and the dependent container are strongly coupled and cannot be used outside the container. Based on attribute injection, it is easy to cause Spring initialization failure.
Because now in the use of Spring, especially Spring Boot, it often causes npe (null pointer error) because the property is referenced before it is injected during initialization.
This in turn causes the container initialization to fail (similar to the following code block). When Java initializes a class
It is in the order of static variables or static statement blocks-> instance variables or initialized statement blocks-> constructors-> @ Autowired.
So when the constructor of this class is executed, the person object has not been injected, and its value is still null. Through @ Autowired injection, and because it is ByType injection, it is possible to have two bean of the same type
If the following code is fast, two identical Bean will be generated, which will result in Spring assembly failure / / 2. Based on attribute injection, it is easy to cause Spring initialization to fail @ Autowiredprivate Person person;private String company;public UserServiceImpl () {this.company = person.getCompany ();} / / 3. Through @ Autowired injection, and because it is ByType injection, it is possible to have two identical types of beanpublic interface IUser {void say ();} @ Servicepublic class User1 implements IUser {@ Override public void say () {}} @ Servicepublic class User2 implements IUser {@ Override public void say () {}} @ Servicepublic class UserService {@ Autowired private IUser user;}
If you must use attribute injection, you can use @ Resource instead of the @ Autowired annotation
@ Resource is equivalent to @ Autowired, except that @ Autowired is automatically injected according to byType.
If we want to assemble by name byName, we can use it in conjunction with the @ Qualifier annotation.
If possible, try to use construction injection
Lombok provides an annotation @ RequiredArgsConstructor, which makes it easy for us to do construction injection quickly, such as:
@ RestController@RequiredArgsConstructorpublic class AppointmentNumberConfigurationController {final AppointmentNumberConfigurationService numberConfigurationService;}
At the same time, it should be noted that:
Using the @ RequiredArgsConstructor annotation requires importing the Lombok package or installing the lombok plug-in
Org.projectlombok lombok 1.18.16
The variable that must be declared is final
According to the constructor injection, the DI based on the constructor is completed when the container calls the class constructor with a set of parameters.
Each of these parameters represents a dependency on other classes. Based on the constructor, the attribute is assigned, and the container injects the dependency by calling the constructor of the class.
Thinking
Why is @ Resource recommended? @ Autowired is not recommended.
By combing question 1, we can know.
Because the @ Autowired annotation is based on ByType when Bean is injected, the assembly fails due to the injection of two Bean of the same type
@ Resource is equivalent to @ Autowired, except that @ Autowired is automatically injected according to byType.
If we want to assemble by name byName, we can use it in conjunction with the @ Qualifier annotation.
@ Resource assembly sequence:
① if both name and type are specified, a unique matching bean is found in the Spring context to assemble, and an exception is thrown if it is not found.
② if name is specified, a bean with a matching name (id) is found in the context for assembly, and an exception is thrown if it is not found.
③ if type is specified, a unique bean with a similar match is found in the context to assemble. If no or more than one is found, an exception will be thrown.
④ automatically assembles in byName mode if neither name nor type is specified; if there is no match, it falls back to an original type for matching, and if there is a match, it automatically assembles.
Therefore, if you must use attribute injection, you can use @ Resource instead of the @ Autowired annotation
@ Autowired, @ Qualifier, @ Resource, what's the difference between them?
@ Autowired: if you assemble by byType, if you cannot find or find more than one, an exception will be thrown.
@ Qualifier: if you want the Bean injected by @ Autowired to be byName assembled, you can specify it using @ Qualifier
@ Resource: the function is equivalent to @ Autowired, except that @ Resource is assembled in byName mode by default. If there is no match, it is returned to byType mode for assembly.
These are all the contents of the article "Why Spring and IDEA do not recommend @ Autowired annotations". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.