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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理