Location>code7788 >text

Spring's three ways of dependency injection

Popularity:483 ℃/2024-07-20 23:31:23

1, what is dependency injection

Dependency Injection (DI), an alias for IOC, is used to reduce dependencies between objects.
When you mention dependency injection, you can't beat IOC.
IOC (Inversion of Control, Inversion of Control) is a design idea, it would have been in the program manually create objects in the control of the Spring Framework to manage.

IOC and DI, are different perspective descriptions of the same concept. (IOC is an idea and DI is a specific technical realization of that idea.)
It can also be interpreted that way:
IOC is the end (the purpose is to create the object) and DI is the means (the means by which the external object is obtained).

2, the common realization of dependency injection

  • Constructor Injection
  • setter method injection
  • Property Injection

2.1 Constructor Injection

The way to put all the necessary dependencies in the parameters with annotated constructor methods and initialize the corresponding variables in the constructor methods is constructor-based injection.

@RestController
public class UserController {
    // Constructor Injection
    private UserService userService;

    @Autowired
    public UserController(UserService userService) {
         = userService;
    }

    @RequestMapping("/add")
    public UserInfo add(String username, String password) {
        return (username, password);
    }
}

Advantages of the approach:

  • Injectable immutable objects
  • The injected object will not be modified
  • The injected object is fully initialized
  • Better versatility

Disadvantages of the approach:

  • Constructor methods are bloated when there are too many objects to rely on

2.2 Setter method injection

In JavaBean, the corresponding properties are usually accessed through the setXXX() and getXXX() methods.
These setXXX() methods are collectively referred to as setter methods and getXXX() methods are collectively referred to as getter methods.
With the setter method, you can change the corresponding object property, and with the getter method, you can get the state of the corresponding property.

Therefore, the current object can set the corresponding dependent object to the injected object through the setter method as long as the setter method is added for the property corresponding to its dependent object.

@Service
public class UserService {
	
    private SmsService smsService;
    
    @Autowired //pass (a bill or inspection etc)settermethod implements the injection
    public void setWolf3Bean(SmsService smsService) {
         = smsService;
    }
}

Advantages of the approach:

  • Fully compliant with the single-responsibility design principle, since each Setter targets only one object

Disadvantages of the approach:

  • Cannot inject immutable objects (final-modified objects)
  • Injected objects can be modified

2.3 Property Injection

Attribute injection, which is dependency injection using annotations on the bean's variables.

Attribute injection is the most familiar and most used type of injection in daily development, which is implemented in the following code:

@RestController
public class UserController {
    // property object
    @Autowired
    private UserService userService;

    @RequestMapping("/add")
    public UserInfo add(String username, String password) {
        return (username, password);
    }
}

Advantages of the approach:

  • Simple to use

Disadvantages of the approach:

  • Cannot inject immutable objects (final-modified objects)
  • Injected objects can be modified
  • Can only be adapted to IoC containers

3. Summary

In actual development, different injection methods are chosen according to different scenarios.
In a nutshell.

  • Forcing a dependency is the constructor approach
  • Optional, mutable dependencies are injected with a setter

But, daily development should still be more attribute injection~