Three common injection methods in Spring

1 Three commonly used dependency injection methods

  • Constructor-based dependency injection
  • Setter-based dependency injection
  • Dependency injection based on field variables

(Of course there are other less commonly used injection methods: static factory method injection, dynamic factory method injection)

2 Implementation methods of each dependency injection

2.1 Constructor injection

    private final InventoryMapper inventoryMapper;
 
    public InventoryController(InventoryMapper inventoryMapper) {
    
    
        this.inventoryMapper = inventoryMapper;
    }

2.2 Setter injection

	private InventoryMapper inventoryMapper;
	 
	public void setInventoryMapper(InventoryMapper inventoryMapper) {
    
    
	    this.inventoryMapper = inventoryMapper;
	}

2.3 Field variable injection

Field variable injection is implemented based on annotations, namely @Resource or @Autowired,

    @Autowired
    private InventoryMapper inventoryMapper;

@Autowired : Indicates automatic injection, automatically finds the appropriate bean from the spring context to inject

@Resource : Indicates injection according to the specified name. @Resource can be injected through byName and byType. By default, it will be matched by byName first, and if it cannot be matched, it will be matched by byType. Of course, you can also add the name attribute to @Service and @Resource to distinguish different implementations.

@Component : Indicates a general term. The marked class is a component. When Spring scans the annotation configuration, it will mark these classes to generate corresponding beans.

@Qualifier and @Autowired are used together. When an interface has multiple implementations, the value of @Qualifier determines which class implementation to call (need to represent each different bean through @Service in the implementation class), that is Says the name of the injected bean is specified.

Controller, Service, and Repository are the control layer class, business layer class, and data access layer class respectively. When Spring scans the annotation configuration, it will mark these classes to generate corresponding beans.

@Autowired and @Resource are used to modify fields, constructors, or methods, and their role is to inject beans. @Service, @Controller, @Repository, and @Component are used to modify classes and mark these classes to generate beans.

2.4 Dependency Injection Based on Field Variables

Advantages : The dependency injection method based on field variables is very concise, without any redundant code, which effectively improves the simplicity of Java.

Disadvantages : cannot specify specific dependencies. It may be encountered that the object of dependency injection is null, so this method is too dependent on the injection container. When the entire dependency container is not started, this class cannot run, and the dependencies required by this class cannot be provided during reflection.

If you use the Setter-based dependency injection method, it is a selective injection, dispensable, even if this dependency is not injected, it will not affect the operation of the entire project.

If constructor-based dependency injection is used, it is a mandatory, explicit injection. Ensure the operation of the entire project by explicitly specifying dependency injection.

3 Understanding of dependency injection

First understand the two concepts of IOC (Inversion of Control) and DI (Dependency Injection) about injection. Simply put, IoC is a kind of thought, and DI is a kind of behavior. It can also be said that IoC is the purpose and DI is the means. IoC means that the method of generating classes is reversed from the traditional method (new). When developing a program, there is no need to call new. When a certain class needs to be used, it can be injected (DI) by the framework.

Guess you like

Origin blog.csdn.net/weixin_42039228/article/details/131204254