SpringBoot-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

class被加载后,当class被初始化的时候static代码块会被调用。你的component组件的依赖还没有初始化。这就是为什么你的代码块会报空指针异常。(你的依赖都是null) 把你的代码移到一个@PostConstruct声明的方法块中,这样可以确保你的代码执行在所有组件都加载以后。