关于java:static-和-postconstruct区别

39次阅读

共计 478 个字符,预计需要花费 2 分钟才能阅读完成。

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 申明的办法块中,这样能够确保你的代码执行在所有组件都加载当前。

正文完
 0