In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the ways of Spring injection?". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Three injection modes of Spring
Attribute (filed) injection
This method of injection is dependency injection using annotations on the variables of bean. In essence, it is injected directly into field by reflection. This is one of the most familiar ways I usually see in development.
@ Autowired UserDao userDao; constructor injection
All the necessary dependencies are placed in the parameters with annotated constructors, and the corresponding variables are initialized in the constructor, which is based on the injection of the constructor. For example:
FinalUserDao userDao;@Autowiredpublic UserServiceImpl (UserDao userDao) {this.userDao = userDao;} set method injection
Dependency injection is accomplished through the setXXX () method of the corresponding variable and the use of annotations on the method. For example:
Possible problems in private UserDao userDao;@Autowiredpublic void setUserDao (UserDao userDao) {this.userDao = userDao;} attribute injection
Question one
Field-based injection may cause some hidden problems. Let's give an example:
@ Autowiredprivate User user;private String company;public UserDaoImpl () {this.company = user.getCompany ();}
The compilation process will not report an error, but report to NullPointerException after running
Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [...]: Constructor threw exception; nested exception is java.lang.NullPointerException
Java initializes a class 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 user object has not been injected, and its value is still null.
Question two
Cannot effectively indicate dependence. I believe many people have come across a bug, and the object of dependency injection is null. The problem encountered when starting the dependency container is that the configuration of dependency injection is missing an annotation or something. This approach is too dependent on the injection container, and when the entire dependency container is not started, the class does not work and cannot provide the dependencies that the class needs during reflection.
Question three
One of the core ideas of dependency injection is that container-managed classes should not rely on container-managed dependencies. In vernacular, if this class uses dependency injection classes, then this class must work properly without these dependencies. However, the use of variable injection does not guarantee this.
Spring recommendation
Since you can mix constructor-based and setter-based DI, it is a good rule of thumb to use constructors for mandatory dependencies and setter methods or configuration methods for optional dependencies.
The translation is:
Forced dependency is in the form of a constructor
Optional, variable dependencies are injected with setter
Use @ Resource instead of @ Autowired
@ Resource has two properties, name and type. In spring, the name attribute is defined as the name of bean, type, which is the type of bean. If you annotate the attribute with @ Resource, then his injection process is
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 is found in the context for assembly, and an exception is thrown if it is not found.
If type is specified, a unique bean that matches the type is found in the context to assemble, and an exception is thrown if no or more than one is found.
If neither name nor type is specified, the default is to assemble as byName; if there is no match, assemble as byType.
@ Autowired only injects based on type and does not match name. If it comes to type's inability to recognize the injected object, you need to rely on the @ Qualifier or @ Primary annotations to decorate it.
Use @ RequiredArgsConstructor constructor to inject
This form is the constructor injection recommended by Spring, which is the annotation under the lombok package. If you use this method, you need to introduce lombok into the project, for example:
@ RequiredArgsConstructorpublic class UserDaoImpl {private final User user;} "what are the ways to inject Spring"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.