【发布时间】:2015-08-26 20:31:50
【问题描述】:
我正在使用 @SpringBootApplication 注释通过 Spring Boot (1.2.0.RELEASE) 运行我的应用程序。
我试图实现的是在每个 @Value 注释中不使用长前缀的情况下具有以下内容:
application.properties
prefix.key1=value1
prefix.key2=value2
DefaultService.java
@Service
@ConfigurationProperties("prefix")
public class DefaultService implements Service {
private final String key1;
private final String key2;
@Autowired
public DefaultService(@Value("${key1}") final String key1, @Value("${key2}") final String key2) {
this.key1 = key1;
this.key2 = key2;
}
}
我知道这可以在不使用 @Value 并且需要设置器 (@ConfigurationProperties prefix not working) 或使用 http://docs.spring.io/spring-boot/docs/1.2.0.RELEASE/reference/htmlsingle/#boot-features-external-config-typesafe-configuration-properties 的情况下完成,但我尝试在构造函数中实现它。
【问题讨论】:
-
我猜长前缀是您方案中的唯一选择。
-
这将是一个不错的 Spring 功能请求。
标签: spring spring-boot