Preface
In normal Java development, programmers need to rely on other classes in a certain class, so they usually use new to new class and then call class instances. The problem with this development is that new class instances are not easy to manage. Spring proposes the idea of dependency injection, that is, dependency classes are not instantiated by programmers, but help us new instances through spring containers and inject instances into the class that needs the object. Another term for dependency injection is "control inversion". The common understanding is: Usually we new an instance, and the control of this instance is our programmer, and control inversion means that the work of the new instance is not done by us programmers but is handed over to the spring container.
There are four ways to inject dependency in Spring
1. Set injection (usually also called attribute injection)
2. Constructor injection
3. Interface injection (this is basically not used now)
4. Annotation injection (@Autowire)
Below is an example of the usage of set method injection, constructor injection, and annotation injection.
1. Set method injection (attribute injection)
UserDao.java
public class UserDao{ public void insertUser(User user){ //Specific logic omitted}}UserService.java
public Interface UserService{ void insertUser(User user);}UserServiceImpl.java
public class UserServiceImpl implements UserService{ private UserDao userDao; public void setUserDao(UserDao userDao){ this.userDao = userDao; } public void insertUser(User user){ userDao.insert(user); }}Spring configuration file
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "> <!--Omit some unrelated configuration writing--> <bean id="userDao"></bean> <bean id="userService"> <property name="userDao" ref="userDao"> </bean> </beans>
The above can inject userDao into UserServiceImpl
2. Constructor injection
User.java
public class User{ //For simplicity, write two attributes private String name; private Integer age; //Omit the getter/setter method about name,age public User(String name,int age){ this.name = name; this.age = age; }}Now use the Spring configuration file to inject the User object
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "> <!--Omit some unrelated configuration writing --> <bean id="user"> <!--Subscript of constructor parameters starts from 0 (avoid multiple constructors having the same number of parameters)--> <constructor-arg type="java.lang.String" index="0" value="zhangsan"/> <constructor-arg type="java.lang.Integer" index="1" value="20> </bean> </beans>
The above can inject property values into the User object. Of course, if you want to inject the example using the set method, it is actually the same. It is necessary to have a constructor in UserServiceImpl , and the formal parameters are userDao, so there is no need for setter method.
Then the writing of the Spring configuration file should be changed to this
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "> <!--Omit some unrelated configuration writing --> <bean id="userDao"></bean> <bean id="userService> <!--Subscript of constructor parameters starts from 0 (avoid multiple constructors having the same number of parameters)--> <constructor-arg index="0" ref="userDao"> </bean> </beans>
This can also achieve the effect achieved by the above set method injection
3. Annotation injection
I have never tried whether it can be successfully @Autowire without adding @Component,@Service,@Controller , etc. to these classes. However, it is recommended to add corresponding annotations to the corresponding level. In the following example, UserDao belongs to the Modul layer. In Spring, you can use @Component annotation.
UserDao.java
@Componentpublic class UserDao{ public void insertUser(User user){ //Specific logic omitted}}UserService.java
public Interface UserService{ void insertUser(User user);}UserServiceImpl.java
//Introduce the corresponding Spring package @Servicepublic class UserServiceImpl implements UserService{ @Autowire private UserDao userDao; public void insertUser(User user){ userDao.insert(user); }}Spring's corresponding configuration file
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "> <!--Omit some unrelated configuration writing--> <!--* means to scan all packages. In actual development, you can scan based on packages involving annotation components--> <context:component-scan base-package="*"><!-- Turn on component scanning--></context:component-scan><context:annotation-config><!-- Turn on annotation processor--></context:annotation-config> <bean id="userDao"></bean> <bean id="userService"/> </beans>
However, during the development process, set method injection is enabled and annotation injection is enabled, Spring will first choose set injection, so it does not forget to provide the corresponding set method, otherwise it will fail.
Through comparison, annotation injection is much more convenient than the other injections, and less code and configuration files are written. It is recommended to use annotation injection during the development process.
Summarize
The above is a complete introduction to several methods of dependency injection in Spring. I hope it can help you in your study or work. If you have any questions, you can also leave a message to communicate. Thank you for your support to Wulin.com.