【发布时间】:2020-03-15 19:19:58
【问题描述】:
我有一个 Spring Boot 应用程序,我在主类中配置了多个 bean,并且我还为不同的环境设置了不同的配置文件。请看下面的代码:
private static String propertiesFilePath;
public static void main(String[] args) {
LOGGER.info("Starting app..!!");
SpringApplication.run(Application.class, args);
}
@Bean
@Profile("local")
public String localBean() {
propertiesFilePath = "application-local.properties";
return propertiesFilePath;
}
@Bean
public static CommandLineRunner init() {
return (args) -> {
try {
LOGGER.info("Starting ..");
CommonUtil.initializeCacheManager(propertiesFilePath);
} catch (Exception e) {
LOGGER.info("CommonUtil.initializeCacheManager :" + e.getMessage());
}
};
}
@Bean
public BroadcastHandler createBroadcastHandler() {
return new BroadcastHandler();
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
在这段代码中,首先调用其他 bean,然后调用带有 @Profile 注释的 bean,我可以在下面的日志中看到:
以下个人资料是
active: local
由于 Spring 容器正在尝试初始化其他 bean,因此在没有先初始化属性的情况下,应用程序无法启动。
带有@Profile 的bean 没有被调用有什么原因,我该如何解决?
【问题讨论】:
标签: spring-boot properties-file