由于我们相当复杂的properties
- 文件设置,我们无法简单地使用@PropertySource
。
这是属性文件:
connection.http.connectTimeout=15000
#connection.http.readTimeout=${connection.http.connectTimeout}
connection.http.readTimeout=#{30*1000}
第二行仍然有效,并将readTimeout
设置为15000,但第三行的值为0。
bean类:
@Component
@ConfigurationProperties("connection")
public class ConnectionConfig {
@NestedConfigurationProperty
private ConnectionSourceConfig http;
public ConnectionSourceConfig getHttp() {
return http;
}
public void setHttp(ConnectionSourceConfig http) {
this.http = http;
}
}
public class ConnectionSourceConfig {
private long connectTimeout;
private long readTimeout;
public long getConnectTimeout() {
return connectTimeout;
}
public void setConnectTimeout(long connectTimeout) {
this.connectTimeout = connectTimeout;
}
public long getReadTimeout() {
return readTimeout;
}
public void setReadTimeout(long readTimeout) {
this.readTimeout = readTimeout;
}
}
使用PropertySourcesPlaceholderConfigurer
似乎阻止SpEL工作:
@Configuration
public class BaseAppConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer properties(Environment environment) throws IOException {
String env = getEnvProperty(environment);
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setLocations(getPropertiesFiles(env));
configurer.setIgnoreResourceNotFound(true);
return configurer;
}
我尝试了一位鸽友PropertySourcesPlaceholderConfigurer
,但convertPropertyValue()
从未被调用过:
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer() {
@Override
protected String convertPropertyValue(String originalValue) {
System.out.println("Parse " + originalValue);
return super.convertPropertyValue(originalValue);
}
};
我试着研究一下Spring是如何工作的,它似乎与PropertyResolver
一起工作。但是,我不知道如何将其编织成一个。
但是,也许我在Spring的处理生命周期中犯了错误......
答案 0 :(得分:1)
属性文件,#被视为注释块。这样做,您的属性文件最终会将属性设置为空。
connection.http.readTimeout=
呈现为#
,因为<table class="(old_class)">
之后的所有内容都会被忽略。