【发布时间】:2018-07-10 10:23:33
【问题描述】:
我是java和spring框架的新手。我的问题是如何将 OS(ubuntu) 环境变量注入 Spring Boot bean。我尝试了什么:
@Configuration
@EnableWebMvc
public class CorsConfig implements WebMvcConfigurer {
@Value("${COMPONENT_PARAM_CORS}")
private String COMPONENT_PARAM_CORS;
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/"+COMPONENT_PARAM_CORS);
}
}
导出 COMPONENT_PARAM_CORS=**
打印环境
说它存在,但是当我尝试 mvn clean install 时:发生错误
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'corsConfig': Injection of autowired dependencies
failed; nested exception is java.lang.IllegalArgumentException: Could not
resolve placeholder 'COMPONENT_PARAM_CORS' in value "${COMPONENT_PARAM_CORS}"
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder
'COMPONENT_PARAM_CORS' in value "${COMPONENT_PARAM_CORS}"
然后我的单元测试也下降了 (我正在尝试搜索此错误,但所有主题都是旧的并且使用来自 application.properties 的参数,但我需要使用 env var 而不是 application.properties)
【问题讨论】:
标签: java spring environment-variables