Springmvc bean dependency injection is empty

The case where the bean injection is empty may be due to the following reasons: Reference link: https://blog.csdn.net/jacksonzhou88/article/details/78482826

  1. The annotation method is wrong, @Controller(”) @Service @Repository @Resource These annotations are written in parentheses if they have aliases, and then use 
    @Autowired 
    private Controller controller (the default is the class name starts with lowercase, if there is an alias, it is based on the alias, Aliases are names in parentheses using @Controller());
  2. The class that injects the bean is also managed by spring as a bean, that is to say, if you want to inject dependencies, you must first be in the spring repository.
  3. Do not use new to create objects in objects using dependency injection, as it is impossible to inject objects. For example, if you want to call Bcontroller in Acontroller, and BService and CService are injected into Bcontroller, please pay attention not to use Bcontroller bcontroller = new Bcontroller() to create objects in Acontroller, so that BService, CService cannot be injected successfully. Because Bcontroller is new, not injected through spring, so the beans in it can't be injected naturally. So if you want to inject successfully, you need to use 
    @Autowired 
    private Bcontroller bcontroller;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324522318&siteId=291194637