Solution to the problem of injection failure when using @Component and then using @Resource or @Autowired in Java

question


Under the @Component annotated class, the @Resource or @Autowired annotation is used. Doing so will cause dependency injection to fail.

This is because the order in which spring loads them is different. When using the @Component annotation to instantiate a bean into the spring container, because @Autowired is in this bean, @Autowired has not completed automatic loading at this time, so it causes dependencies. The injected service is null

@Component and @Autowired or @Resource


In Spring applications, the @Component annotation is used to mark a class as a component that can be automatically scanned. When the Spring container starts, it scans classes annotated with @Component and instantiates them into beans. These beans will be added to the Spring container's bean factory for use in the application.

@Autowired annotation is used for dependency injection in Spring applications. When the Spring container creates a bean with an @Autowired annotation, it will automatically find a matching type for injection. If more than one matching type is found, an exception is thrown.

The @Resource annotation can also be used for dependency injection in Spring applications. When the Spring container creates a bean annotated with @Resource, it will give priority to name matching for injection. If a matching name is not found, type matching is used for injection.

Therefore, in a Spring application, @Component annotated classes will be loaded before @Autowired or @Resource annotated classes. The @Autowired annotation will give priority to type matching for dependency injection, while the @Resource annotation will give priority to name matching for dependency injection.

When using @Component, @Autowired or @Resource annotations for dependency injection, you also need to pay attention to the following points:

If you want to use the @Autowired annotation to inject multiple matching types, you can use the @Qualifier annotation to specify a specific bean name.
If you want to use @Autowired or @Resource annotation to inject non-required dependencies, you can use @Autowired(required=false) or @Resource(required=false).
If the dependency you want to inject using the @Autowired or @Resource annotation does not exist, you can use @Autowired(required=false) or @Resource(required=false) and handle it accordingly in the code.
If you want the dependency injected using the @Autowired or @Resource annotation to be null, you can use @Autowired(required=false) or @Resource(required=false) and handle it accordingly in the code.
If you want the dependency injected using the @Autowired or @Resource annotation to be null, you can use @Autowired(required=false) or @Resource(required=false) and handle it 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;
    }    
}


Placing @Autowiredthe annotation on the method will automatically inject the parameters of this method after the class is loaded , and execute the method once.

Guess you like

Origin blog.csdn.net/Angel_asp/article/details/132405392