static 和 @postconstruct区别

static 和 @postconstruct区别:

static blocks are invoked when the class is being initialized, after it is loaded. The dependencies of your component haven't been initialized yet. That is why you get a NullPointerException (Your dependencies are null) .

Move your code to a method annotated with @PostConstruct. This will ensure that your code will run when all the dependencies of your component are initialized

翻译:static模块会被引入,当class加载后。你的component组件的依赖还没有初始化。这就是为什么你的代码块会报空指针异常。(你的依赖都是null)

把你的代码移到一个@PostConstruct声明的方法块中,这样可以确保你的代码执行在所有组件都加载以后。

你可能感兴趣的:(static 和 @postconstruct区别)