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
曾经很高兴在这样的困难时刻提供帮助: ) –