Spring Dependency Injection
Let's talk about the conclusion first
- When actually developing, try to choose constructor injection instead of field injection.
- Assume that value injection and method injection are applicable to fewer and more special scenarios.
reason
- Officially recommended!
- The code is simpler:In conjunction with Lombok's @RequiredArgsConstructor, a constructor containing all final fields can be automatically generated.
- Safer:Constructor injection ensures that all required dependencies are injected at the time of object creation, avoiding the issue of forgetting to inject a dependency at runtime that causes NullPointerException.
- Easier to test:When using constructor injection, unit testing can be performed directly through the constructor transitive dependencies without the need to simulate the injection mechanism of the framework.
As for @Resource, it is an annotation for Java, I personally recommend not to use it in Spring projects.
The differences between the two are as follows:
@Autowired
- Annotations provided by Spring: @Autowired is an annotation provided by Spring containers, specifically used for dependency injection.
- By type injection by default: @Autowired By default, by type injection.
- Injection by name can be specified: Injection by name can be specified through the @Qualifier annotation.
@Resource
- J2EE standard annotation: @Resource is part of the J2EE standard and is usually used for injection of EJB and other J2EE resources.
- By name injection by default: @Resource By default, it is injected by name. If a matching name cannot be found, it is injected by type.
- No @Qualifier is required: @Resource can specify a name through the name attribute without the need for an additional @Qualifier annotation.
1. Constructor Injection
Constructor injection passes dependencies through the constructor method of the class.
For example:
@RestController @RequiredArgsConstructor public class WarningTaskController { // Note that using final tags is immutable private final WarningTaskService taskService; // Multiple dependencies can}
2. Setter Injection
Let value injection inject dependencies through the setter method.
For example:
Using Spring Security, the default memory user authentication information can be configured=customUserName
It is through Setter injection.
The source code is as follows:
@ConfigurationProperties(prefix = "") public class SecurityProperties { // Omit some non-essential code private final User user = new User(); public User getUser() { return ; } public static class User { private String name = "user"; private String password = ().toString(); private List<String> roles = new ArrayList<>(); private boolean passwordGenerated = true; public String getName() { return ; } public void setName(String name) { = name; } //Omit other getters/setters } }
3. Field Injection
Field injection directly injects dependencies through fields.
For example:
@RestController public class WarningTaskController { // Not recommended // If you depend on a lot, the code will be bloated @Autowired private final WarningTaskService taskService; }
4. Method Injection
Method injection injects dependencies through method parameters.
For example:
@RestController public class WarningTaskController { private WarningTaskService taskService; // Not recommended if the object is mutable @Autowired public void configure(WarningTaskService taskService) { = taskService; } // Other business logic...}
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.