【发布时间】:2017-05-23 03:11:20
【问题描述】:
我正在使用 web Spring Boot 1.4.3 并创建一个自定义 @AutoConfigure 来设置一堆属性。事实证明,我设置的许多属性都依赖于一个内置的 Spring 属性:server.port。问题:让我的 AutoConfigurers 使用此属性(如果存在)的最佳方法是什么,否则默认为 9999?
这是我使用属性文件的方法:
myapp.port = ${server.port:9999}
这是我在 AutoConfiguration 方面取得的成就:
@Configuration(prefix="myapp")
@EnableConfigurationProperties(MyAppProperties.class)
public class MyAppProperties {
@Autowired
ServerProperties serverProperties;
Integer port = serverProperties.getPort() otherwise 9999?
}
我曾考虑使用 @PostConstruct 来执行逻辑,但查看 Spring-Boot 的自动配置源代码示例,我没有看到他们这样做,所以感觉就像代码异味。
【问题讨论】:
-
不幸的是,您没有像 Java 编译器的注释传递那样获得迭代自动配置。您是否尝试过使用
@AutoConfigureAfter? -
Integer port = serverProperties.getPort() != null?serverProperties.getPort():9999
标签: java spring-boot