通过@Autowired通过Spring实例化的类-引用时会生成空指针豁免

时间:2019-03-03 17:06:22

标签: java spring spring-boot

获得以下课程

       @Component
       @DependsOn({"classB"}
       public class ClassA{
       @Autowired ClassB classB;
        public ClassA(){
         log.info("Instantiating Class A") // 
        }

            executeA(){
             classB.executeB();  //null pointer
            }
        }

    @Configuration
    @Endpoint
    public class EndPointClass {
    @Autowired
    ClassA classA;
       webserviceEndpoint1() {
          classA.executeA(); // this line works fine without any errors
       }

    }

    @Service
    public class ClassB {

       public ClassB() {
         log.info("Instantiating Class B") // This line is executed before the Class A 
       }

      public void executeB() {
      }

    }

当我运行上面的代码时,我可以看到ClassB在spring之前被实例化,并且在ClassA被实例化之前,但是一旦它到达classB.executeB,我就会得到一个空指针异常。这些都在Springboot中运行。

请问对此有何想法?

0 个答案:

没有答案