0
由於我們相當複雜的properties
-文件設置,我們不能簡單地使用@PropertySource
。PropertySourcesPlaceholderConfigurer和SpEL?
這裏的屬性文件:
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
似乎停止工作規劃環境地政司:
@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是如何完成它的工作,似乎它與秒。然而,我不明白我怎樣才能編織一個。
但是,也許我犯了一個錯誤關於Spring的處理生命週期...
哦,男孩,這樣一個簡單的答案!我完全「忘記」了這一點。雖然說只是看看我的屬性 - 例子....它一直在那裏.......偉大的金髮時刻.......... – sjngm
曾經很高興在這樣的困難時刻提供幫助: ) –