Java中如何获取spring中配置的properties属性文件内容

4 篇文章 0 订阅

现在很多项目都是ssh框架,其中applicationContext.xml必定会用引用外部properties文件,

怎样即可供自己调用,又可提供给外部调用呢,下介绍个方法:

直接上代码吧!哈哈

1. jdbc.properties 内容如下:

jdbc.driverClass=oracle.jdbc.driver.OracleDriver



2. 自己调用 applicationContext.xml 内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
						http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
						http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
						http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" >
	<!-- 动态读取配置文件进行数据加载 -->
	<bean id="propertyConfigurer" 
    class="com.sse.common.util.CustomizedPropertyPlaceholderConfigurer"> 
    <property name="ignoreResourceNotFound" value="true" /> 
	    <property name="locations"> 
	        <list> 
	            <!-- 这种是用于工程当中读取<value>classpath*:jdbc.properties</value> -->
	            <!-- 下边这种文法用于jar包,访问jar包外部的文件配置方式 -->
	            <value>file:./jdbc.properties</value>
	        </list> 
	    </property> 
	</bean>
	<!-- 动态读取配置文件进行数据加载
	<bean id="propertyConfigurer" 
	class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="jdbc.properties" />
	</bean>  -->
	
	<!-- 定义数据源Bean,使用C3P0数据源实现 -->
	<bean id="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<!-- 指定连接数据库的驱动 -->
		<property name="driverClass" value="${jdbc.driverClass}" />
	</bean>
</beans>


3. 处部调用实现类CustomizedPropertyPlaceholderConfigurer.java 继承PropertyPlaceholderConfigurer.java 内容如下:

package com.sse.common.util;

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;

/**
 * 自定义PropertyPlaceholderConfigurer返回properties内容
 * Created on 2013-12-12
 * <p>Title:       XXXX系统_[模块名]/p>
 * <p>Description: [描述该类概要功能介绍]</p>
  * <p>Copyright:   Copyright (c) 2011</p>
 * <p>Company:     </p>
 * <p>Department:  网站运维部</p>
 * @author         zhl
 */
public class CustomizedPropertyPlaceholderConfigurer extends
		PropertyPlaceholderConfigurer {

	private static Map<String, Object> ctxPropertiesMap;

	@Override 
	protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,Properties props) throws BeansException {
		super.processProperties(beanFactoryToProcess, props); 
		ctxPropertiesMap = new HashMap<String, Object>();
		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);
	}

}

4. 外部类调用  获取属性文件的内容

String host =  (String) CustomizedPropertyPlaceholderConfigurer.getContextProperty("jdbc.driverClass");


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值