【发布时间】:2016-03-16 10:40:00
【问题描述】:
用@ConfigurationProperties定义属性时,我可以定义特定字段的前缀而不是整个类吗?
例如,假设我们有一个 Properties 类
@ConfigurationProperties(prefix = "com.example")
public class MyProperties {
private String host;
private String port;
// Geters and setters...
}
这会将字段host 和port 绑定到com.example.host 和com.example.port。假设我想将port 绑定到com.example.something.port。这样做的方法是定义一个内部类Something 并在那里添加属性port。但是如果我需要更多前缀,它会变得太麻烦。我尝试在setter上添加@ConfigurationProperties,因为注解的目标是ElementType.TYPE和ElementType.METHOD:
@ConfigurationProperties(prefix = "com.example.something.port")
public void setPort(int port) {...}
但它最终没有奏效。除了通过内部类之外,还有其他方法可以自定义前缀吗?
【问题讨论】:
标签: java properties spring-boot