In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the way to inject Spring into Bean". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the way to inject Bean into Spring".
One of the core functions of the Spring framework is to manage the dependency relationship between Bean through dependency injection.
Dependency injection
Each application-based java has several objects that work together to present the working application that the end user sees. When writing a complex Java application, the application class should be as independent of other Java classes as possible to increase the possibility of reuse of these classes, and when doing unit tests, the test should be independent of other classes. Dependency injection (or sometimes called cabling) helps to glue these classes together while keeping them independent.
Spring implements IOC (inversion of Control) through DI (dependency injection). There are three common injection methods:
Construction method injection
Setter injection
Annotation-based injection
1. Construction method injection
Question 1: if there are multiple constructors with parameters and each constructor has attributes to be injected in the parameter list, where will userDaoJdbc be injected?
Public class UserService implements IUserService {private IUserDao userDao; private User user; public UserService (IUserDao userDao) {System.out.println ("this is the construction method with one parameter"); this.userDao = userDao } public UserService (IUserDao userDao, User user) {System.out.println ("this is the construction method with two parameters"); this.userDao = userDao; this.user = user;} public void loginUser () {userDao.loginUser () }} result: will be injected into a constructor with only one parameter, and which constructor will be injected after testing is independent of the order of the constructor
Question 2: if there is only one constructor, but there are two parameters, one is the parameter to be injected, and the other is another type of parameter, will the injection be successful this time?
Public class UserService implements IUserService {private IUserDao userDao; private User user; public UserService (IUserDao userDao, User user) {this.userDao = userDao; this.user = user;} public void loginUser () {userDao.loginUser () Result: failed, even if you specify the parameter name userDao to be injected through the name attribute in the costract-arg tag.
Question 3: how do we write values in the configuration file if we want to inject values into a constructor with multiple parameters?
Public class UserService implements IUserService {private IUserDao userDao; private User user; public UserService (IUserDao userDao, User user) {this.userDao = userDao; this.user = user;} public void loginUser () {userDao.loginUser () }} reference Writing: specify the value to be injected through the name attribute, regardless of the order of the constructor parameter list parameters.
Question 4: if there are multiple constructors and each constructor only has a different order of parameters, which one will be injected by injecting multiple parameters through the constructor?
Public class UserService implements IUserService {private IUserDao userDao; private User user; public UserService (IUserDao userDao, User user) {System.out.println ("this is the second construction method"); this.userDao = userDao; this.user = user } public UserService (User user, IUserDao userDao) {System.out.println ("this is the first construction method"); this.userDao = userDao; this.user = user;} public void loginUser () {userDao.loginUser () Result: the constructor is injected first, in which case it is related to the order of the constructor. 2. Setter injection Note: both of the above methods are OK. Spring converts the first letter of each word of the name value to uppercase, then concatenates "set" in front of it to form a method name, and then looks for the method in the corresponding class to achieve injection through reflection calls. Public class UserService implements IUserService {private IUserDao userDao1; public void setUserDao (IUserDao userDao1) {this.userDao1 = userDao1;} public void loginUser () {userDao1.loginUser () }} Note: if you inject attributes through the set method, spring will instantiate the object through the default empty parameter constructor, so if you write a constructor with parameters in the class, be sure to write the constructor with null parameters, otherwise spring has no way to instantiate the object, resulting in an error. 3. Annotation-based injection
Before introducing the method of annotation injection, let's briefly understand that autowire,autowire, an attribute of bean, has three main attribute values: constructor,byName,byType.
Constructor: automatic injection through the construction method, and spring will match the bean with the same parameter type as the construction method for injection. If there is a multi-parameter construction method, a construction method with only one parameter, and multiple bean matching multi-parameter construction methods are found in the container, then spring will first inject bean into the multi-parameter construction method.
ByName: the id name injected into the bean must match the second half of the set method, and the first letter of the first word of the id name must be lowercase, which is a little different from manual set injection.
ByType: find all the set methods and inject bean that matches the parameter type.
There are four main annotations that can be registered with bean, each of which can be used at will, but semantically different:
@ Component: can be used to register all bean
@ Repository: bean that is mainly used to register the dao tier
@ Controller: bean mainly used to register the control layer
@ Service: bean mainly used to register the service layer
There are two main ways to describe dependencies:
The annotation of @ Resource:java, by default, matches the id of the bean with the same attribute name as byName. If it is not found, it will be searched in the way of byType. If the byType finds more than one, use the @ Qualifier annotation (spring annotation) to specify the bean of a specific name.
@ Resource@Qualifier ("userDaoMyBatis") private IUserDao userDao;public UserService () {}
@ Autowired:spring annotation. By default, byType is used to match bean of the same type. If there is only one match, then the bean is directly injected, regardless of the name of the bean to be injected. If there is more than one match, the determineAutowireCandidate method of DefaultListableBeanFactory is called to determine which bean is to be injected. The determineAutowireCandidate method is as follows:
@ Autowired@Qualifier ("userDaoJdbc") private IUserDao userDao; can be simply understood as matching in the way of ByType first. If there are multiple matches, then match in the way of ByName. If you find the corresponding bean, inject it, and throw an exception if it is not found. Thank you for your reading, the above is the content of "what is the way to inject Spring into Bean?" after the study of this article, I believe you have a deeper understanding of what the way of Spring injection into Bean is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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: 236
*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.