「Spring開発」PropertyPlaceholderConfigurerでプロパティファイルを読み込むコード
1.PropertyPlaceholderConfigurerの設定
<bean id="propertyConfigurer" class="com.lija.changfapj.util.PropertyConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
2.PropertyConfigurerクラスの定義
package com.lija.changfapj.util;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import java.util.Properties;
public class PropertyConfigurer extends PropertyPlaceholderConfigurer {
private Properties props;
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
throws BeansException {
super.processProperties(beanFactoryToProcess, props);
this.props = props;
}
public String getProperty(String key){
return this.props.getProperty(key);
}
public String getProperty(String key, String defaultValue) {
return this.props.getProperty(key, defaultValue);
}
public Object setProperty(String key, String value) {
return this.props.setProperty(key, value);
}
}
Beanクラスをインジェクション
@Autowired
private SampPropertyBean cftBean;