通过spring配置properties文件
|
1
2
3
4
5
6
7
8
9
|
<bean id="propertyConfigurer" class="com.hapishop.util.ProjectDBinfoConfigurer"> <property name="ignoreResourceNotFound" value="true" /> <property name="locations"> <list> <value>/WEB-INF/config/dbinfo.properties</value> </list> </property></bean> |
其中class为自己定义的类
自定义类ProjectDBinfoConfigurer
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
import java.util.HashMap;import java.util.Map;import java.util.Properties;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;/** * 自定义ProjectDBinfoConfigurer返回properties内容 * @ClassName: ProjectDBinfoConfigurer * @Description: TODO (请用一句话描述该类做什么) * @author ZhangYQ iitshare@itblood.com * @date: 2012-11-20 下午11:48:32 */public class ProjectDBinfoConfigurer extends PropertyPlaceholderConfigurer { private static Map ctxPropertiesMap; @Override protected void processProperties( ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { super.processProperties(beanFactoryToProcess, props); ctxPropertiesMap = new HashMap(); for (Object key : props.keySet()) { String keyStr = key.toString(); String value = props.getProperty(keyStr); ctxPropertiesMap.put(keyStr, value); } } public static Object getContextProperty(String name) { return ctxPropertiesMap.get(name); }} |
这样就可以通过ProjectDBinfoConfigurer类来获取properties属性文件中的内容了
如何获取属性文件的内容
String url= (String) ProjectDBinfoConfigurer.getContextProperty(“jdbc.url”);
摘自 : http://blog.itblood.com/java-to-get-the-value-of-the-spring-configuration-file.html