About the problem that @Autowired is NULL in the use of @Component in Java

  1.  The code that found the above problem is as follows: Direct use of @Autowired in @Component causes the code to report a null pointer
     
    @Component
    public class ThreasTestDemo extends Thread {
        private String personalId;
        private HttpSession httpSession;
        @Autowired
        private McPersonalInfoService mcPersonalInfoService;
    }
  2. The processing code is as follows:
     
    
    @Component
    public class ThreasTestDemo extends Thread{
        private String personalId;
        private HttpSession httpSession;
    
        private static McPersonalInfoService mcPersonalInfoService;
    
        @Autowired
        public void setMcPersonalInfoService (McPersonalInfoService mcPersonalInfoService){
            ThreasTestDemo.mcPersonalInfoService= mcPersonalInfoService;
        }
    }

    3. The reason is roughly as follows:
              When using the @Componet annotation to instantiate a bean into the Spring container, @Autowired is in the bean, and @Autowired has not completed the automatic loading, so the service is null

Guess you like

Origin blog.csdn.net/zf199805/article/details/126800070