![](/img/trans.png)
[英]How do I inject a value from Properties file to a field of an existing instance (not managed by Spring) using Spring annotations?
[英]How can I reload properties file in Spring 4 using annotations?
我有一個簡單的應用程序,其中我使用幾個屬性文件來獲取其他用戶編輯的內容(指向網站的鏈接等)。
我加載屬性的類如下所示:
@Configuration
@PropertySource("classpath:salestipsWhitelist.properties")
public class SalestipsWhitelist {
@Autowired
Environment env;
public Environment getEnv() {
return env;
}
public void setEnv(Environment env) {
this.env = env;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
一些屬性文件:
UPS_MPP_M_L=True
UPS_MPP_M_M=True
UPS_MPP_M_MP=True
UPS_MPP_M_S=True
這可以正常工作,但是如果我對properties-file進行更改,則必須重新加載應用程序以可視化所做的任何更改。
如果我將位置移動到磁盤而不是類路徑,是否可以定期或手動重新加載? 我不希望更改時自動完成此操作,因為我想控制何時完成/更新。
這可以治療。 需要Java 7,Apache Commons日志記錄,Apache Commons lang(v2.6)和Apache Commons Configuration:
package corejava.reloadTest;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
public class MyApplicationProperties {
private static PropertiesConfiguration configuration = null;
static {
try {
configuration = new PropertiesConfiguration("test.properties");
} catch (ConfigurationException e) {
e.printStackTrace();
}
configuration.setReloadingStrategy(new FileChangedReloadingStrategy());
}
public static synchronized String getProperty(final String key) {
return (String) configuration.getProperty(key);
}
}
並使用以下命令進行測試:
package corejava.reloadTest;
public class TestReloading {
public static void main(String[] args) {
while (true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(MyApplicationProperties.getProperty("key"));
}
}
}
更改test.properties時的輸出是這樣的(test.props的原始內容是key = value,后來在程序執行中更改為key = value1):
value
value
value
value
value
Jan 17, 2015 2:05:26 PM org.apache.commons.configuration.PropertiesConfiguration reload
INFO: Reloading configuration. URL is file:/D:/tools/workspace /AutoReloadConfigUsingApacheCommons/resources/test.properties
value1
value1
value1
您也可以考慮使用類似Groovy的DSL來考慮《 Spring Framework官方參考文檔》可 刷新bean 。
必須重寫PropertyPlaceholderConfigurer才能重新加載新的Properties
您需要重寫processProperties
方法,以使包含屬性的StringValueResolver
變為可加載。 這是我的代碼
import java.io.IOException;
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.util.PropertyPlaceholderHelper;
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
import org.springframework.util.StringValueResolver;
public class ReloadablePropertyPlaceholderConfigurer
extends PropertyPlaceholderConfigurer {
private ReloadablePlaceholderResolvingStringValueResolver reloadableValueResolver;
public void reloadProperties() throws IOException {
Properties props = mergeProperties();
this.reloadableValueResolver.refreshProperties(props);
}
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
throws BeansException {
this.reloadableValueResolver = new ReloadablePlaceholderResolvingStringValueResolver(props);
StringValueResolver valueResolver = this.reloadableValueResolver;
this.doProcessProperties(beanFactoryToProcess, valueResolver);
}
private class ReloadablePlaceholderResolvingStringValueResolver
implements StringValueResolver {
private final PropertyPlaceholderHelper helper;
private final ReloadablePropertyPlaceholderConfigurerResolver resolver;
public ReloadablePlaceholderResolvingStringValueResolver(Properties props) {
this.helper = new PropertyPlaceholderHelper(placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders);
this.resolver = new ReloadablePropertyPlaceholderConfigurerResolver(props);
}
@Override
public String resolveStringValue(String strVal) throws BeansException {
String value = this.helper.replacePlaceholders(strVal, this.resolver);
return (value.equals(nullValue) ? null : value);
}
private void refreshProperties(Properties props){
this.resolver.setProps(props);
}
}
private class ReloadablePropertyPlaceholderConfigurerResolver
implements PlaceholderResolver {
private Properties props;
private ReloadablePropertyPlaceholderConfigurerResolver(Properties props) {
this.props = props;
}
@Override
public String resolvePlaceholder(String placeholderName) {
return ReloadablePropertyPlaceholderConfigurer.this.resolvePlaceholder(placeholderName, props, SYSTEM_PROPERTIES_MODE_FALLBACK);
}
public void setProps(Properties props) {
this.props = props;
}
}
}
這是properties-config.xml
的配置。所有這些屬性都可以在運行時作為原型Bean重新加載。
<bean id="propertyConfigurer" class="com.cn21.mail189.analysis.commons.expand.ReloadablePropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<!-- database config -->
<value>classpath:spring/dbconfig.properties</value>
<!-- app config -->
<value>classpath:spring/app.properties</value>
<!-- some other config -->
<value>classpath:xxxx.properties</value>
</list>
</property>
</bean>`
內部applicationContext.xml
<bean id="beanId" class="org.apache.commons.configuration.reloading.FileChangedReloadingStrategy">
<property name="refreshDelay" value="30000" /> <!-- 30 seconds -->
</bean>
<bean id="reloadableProperties" class="org.apache.commons.configuration.PropertiesConfiguration">
<constructor-arg value="file:/web/${weblogic.Domain}/${weblogic.Name}/${app.Name}/reloadable_cfg/Reloadable.properties"/>
<property name="reloadingStrategy" ref="propertiesReloadingStrategy"/>
</bean>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.