在我的 SpringBoot 应用程序中,我在实现 EnvironmentPostProcessor
的类中 Autowiring 了一个 configObject
。
注入(inject)的类在启动时从不同的来源读取数据,因为这是应用运行所必需的。
但是在启动应用程序时,configObject
变为 Null。
@SpringBootApplication
@EnableEncryptableProperties
@EnableConfigurationProperties
@EnableCaching
@Slf4j
public class SBApplication {
public static void main(String[] args) {
SpringApplication.run(SBApplication.class, args);
}
}
以及调用 Autowired 对象的 AppEnvironmentPostProcessor 类。这个类在spring.factories中配置为org.springframework.boot.env.EnvironmentPostProcessor
。该类在启动时被调用。
@Slf4j
public class AppEnvironmentPostProcessor implements
EnvironmentPostProcessor, Ordered {
@Autowired
KeysConfig keysConfig;
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
// keysConfig is null
String key = keysConfig.getSecretKeyMap().get("key12");
}
}
在 KeysConfig 类中
@Component
public final class KeysConfig {
public Map getSecretKeyMap() {
//Returns key map
}
}
我正在使用 Intellij Ultimate。我该如何调试和解决这个问题?
最佳答案
EnvironmentPostProcessor
在创建应用程序上下文之前创建,因此在依赖注入(inject)成为可能之前创建。这意味着 @Autowired
将不起作用。
您必须更新您的实现以创建 KeysConfig
本身的实例,或者使用模仿任何 KeysConfig
当前所做的不同方法。
关于java - 启动时 EnvironmentPostProcessor 实现类中的 Autowired 属性为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68034440/