[Spring] 4 dependency injection methods of Spring Bean

Preface

The so-called dependency injection is actually assigning values ​​​​to the properties in the object, because there are other objects in the object, so a dependency is formed. Spring has 4 ways to assign values ​​to properties:

  1. Constructor injection
  2. set method injection
  3. automatic assembly
  4. annotation

1. Constructor injection

Constructor injection refers to injecting attributes or objects into the constructor to implement dependency injection. As shown below, define a Bean with the id userDaoImpl in the tag, and inject the value with name username and value admin. After the injection is completed Get the value admin directly through this.username. Among them, the reference type uses the ref attribute, and the basic type uses the value attribute.

public class UserDaoImpl {
    
    

    private String username;

    public UserDaoImpl(String username) {
    
    
        this.username = username;
    }
}
  <bean id="userDaoImpl" class="com.example.UserDaoImpl">
      <constructor-arg name="username" value="admin"></constructor-arg>
  </bean>

2. set method injection

The set method injection is to implement the dependency injection of attributes or objects by implementing the get and set methods in the class. As shown below, define a Bean with the id userDaoImpl in the tag, and inject the value with name username and value admin. After the injection is completed, the value admin is obtained directly through getUsername().

public class UserDaoImpl {
    
    

    private String username;


    public String getUsername() {
    
    
        return username;
    }

    public void setUsername(String username) {
    
    
        this.username = username;
    }
}
<bean id="userDaoImpl" class="com.example.UserDaoImpl">
    <property name="username" value="admin"></property>
</bean>

3. Automatic assembly

Spring provides the function of automatic assembly, which simplifies our configuration. Automatic assembly is not enabled by default. There are two commonly used methods:

  • byName: Automatically assemble through the parameter name, as shown below. After the autowire with the id of userService is set to byName, the IOC container will automatically assemble through the name. It is found that there is an attribute called userDao in the UserService class, and then look at the IOC container. Is there any id userDao, if so, assemble it.
<bean id="userDao" class="com.example.UserDao"></bean>
<bean id="userService" class="com.example.UserService" autowire="byName"/>
  • byType: Automatic assembly through parameter type. When autowire is set to byType, the IOC container will check if there is a UserDao type in it, and assemble it if there is.
<bean id="userDao" class="com.example.UserDao"></bean>
<bean id="userService" class="com.example.UserService" autowire="byType"/>

4. Annotations

  • The @Autowired annotation can realize automatic assembly, as long as the annotation is marked on the corresponding property, but the @Autowired annotation is only injected according to byType.
public class UserController {
    
    

    @Autowired
    private IUserService userService;
}
  • The @Resource annotation can realize automatic assembly. It has two important attributes, name and type. The name attribute is resolved to the name of the bean, and the type attribute is resolved to the type of the bean. So if you use the name attribute, use the byName automatic injection strategy, and use the type attribute to use the byType automatic injection strategy. If neither the name nor the type attribute is specified, the byName automatic injection strategy will be used through the reflection mechanism.

  • The @Autowired annotation has the same effect as the @Resource annotation, except that @Autowired is injected according to byType. If @Autowired wants to use a name, it can be used in conjunction with the @Qualifier annotation.

Guess you like

Origin blog.csdn.net/u011397981/article/details/132751799
Recommended