Injection failure problem when using @Component and using @Resource or @Autowired
foreword
Under @Componentthe annotated class, @Resourcethe or @Autowiredannotation is used again. Doing so will cause dependency injection to fail.
This is because the order in which spring loads them is different. When using @Componentannotations to instantiate beans into the spring container, because @Autowiredit is in this bean, @Autowiredthe automatic loading has not been completed at this time, so the dependency injection service is null
@Component and @Autowired or @Resource
In Spring applications, @Componentannotations are used to mark classes as autoscannable components. When the Spring container starts, it scans for @Componentannotated classes and instantiates them as beans. These beans will be added to the bean factory of the Spring container for use in the application.
@AutowiredAnnotations are used for dependency injection in Spring applications. When the Spring container creates an @Autowiredannotated bean, it will automatically find the matching type for injection. If more than one matching type is found, an exception is thrown.
@ResourceAnnotations can also be used for dependency injection in Spring applications. When the Spring container creates an @Resourceannotated bean, it will preferentially use name matching for injection. If no matching name is found, type matching is used for injection.
Therefore, in a Spring application, @Componentthe annotated class will be loaded before @Autowiredor @Resourcebefore the annotated class. @AutowiredAnnotations will prefer type matching for dependency injection, while @Resourceannotations will prefer name matching for dependency injection.
When using @Component, @Autowiredor @Resourceannotations for dependency injection, you need to pay attention to the following points:
If you want to use @Autowiredannotations to inject multiple matching types, you can use @Qualifierannotations to specify specific bean names.
If you wish to inject non-essential dependencies using @Autowiredthe or annotation, you can use or .@Resource@Autowired(required=false)@Resource(required=false)
If the dependency that you want to use @Autowiredor @Resourceannotation injection does not exist, you can use @Autowired(required=false)or @Resource(required=false), and handle it in the code accordingly.
If you want to use @Autowiredor @Resourceannotation to inject dependencies that can be null, you can use @Autowired(required=false)or @Resource(required=false)and handle them accordingly in the code.
If you want to use @Autowiredor @Resourceannotation to inject dependencies that can be null, you can use @Autowired(required=false)or @Resource(required=false)and handle them accordingly in the code.
Solution
@Component
public class Test {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
// @Autowired
// private UserServer userServer;
private static UserServer userServer;
@Autowired
public void setUserServer(UserServer userServer) {
Test.userServer = userServer;
}
}
Putting @Autowiredthe annotation on the method will automatically inject the parameters of this method after the class is loaded , and execute the method once.
0 Comments