在spring中提供了一个专门加载文件的类PropertyPlaceholderConfigurer,通过这个类我们只需要给定需要加载文件的路径就可以

通过该类加载到项目,但是为了后面在程序中需要使用到属性文件内容,在这里将加载到的配置文件全部保存进一个map对象中,后面可以直接

键值对去用:

首先创建一个继承PropertyPlaceholderConfigurer的类PropertyConfigurer,加载文件,并保存进对象:

public class PropertyConfigurer extends PropertyPlaceholderConfigurer{

	private static Map<String, Object> ctxPropertiesMap; 

    @Override
protected void processProperties( ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
//spring载入配置文件(具体原理还在研究中)
super.processProperties(beanFactoryToProcess, props);
ctxPropertiesMap = new HashMap<String, Object>();
//便利加载的配置文件的键值,并根据键值获取到value值,然后一同保存进map对象
for (Object key : props.keySet()){
String keyStr = key.toString();
String value = props.getProperty(keyStr);
ctxPropertiesMap.put(keyStr, value);
}
} //此方法根据map对象的键获取其value的值
public static Object getContextProperty(String name) {
return ctxPropertiesMap.get(name);
}
}

  然后在spring的配置文件中配置需要载入的属性文件

 <!--读入配置文件,在后面的代码中需要用到属性文件的内容时 -->
<bean id="propertyConfigurer" class="cn.com.owg.util.PropertyConfigurer">
<property name="locations">
<value>classpath*:jdbc.properties</value>
</property>
</bean>

<!--读入配置文件,后面的代码不需要用到属性文件的内容时,直接通过PropertyPlaceholderConfigurer来载入属性文件 -->
        <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
             <property name="locations">
                     <value>classpath*:jdbc.properties</value>
             </property>
        </bean>

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

  后面的代码中需要使用属性配置文件时:

String    driverManager=PropertyConfigurer.getContextProperty("jdbc:driverClassManager")).trim();//获取属性文件的某一个内容

  

spring加载属性配置文件内容的更多相关文章

  1. 使用Spring加载properties配置文件.md

    背景 类似于datasource.properties之类的配置文件,最初通过Java的Properties类进行处理.这种方式有许多弊端,如每次都需要读取配置文件:若将Properties作为成员变 ...

  2. spring 加载属性(properties)文件

    在开发的过程中,配置文件往往就是那些属性(properties)文件,比如使用properties文件配置数据库文件,又如database-config.properties 代码清单:databas ...

  3. spring学习 十六 spring加载属性文件

    第一步:创建一个properties文件,以数据库链接作为实例db.properties jdbc.url=jdbc:mysql://192.168.153.128:3306/mybaties?cha ...

  4. Spring加载Properties配置文件的三种方式

    一.通过 context:property-placeholder 标签实现配置文件加载 1) 用法: 1.在spring.xml配置文件中添加标签 <context:property-plac ...

  5. Spring加载xml配置文件的方式 ApplicationContext

    大家都知道Java读普通文件是通过Basic I/O 中的InputStream.OutStream.Reader.Writer 等实现的.在spring 框架中,它是怎样识别xml这个配置文件的呢? ...

  6. Spring加载XML配置文件

    原创链接:http://www.cnblogs.com/yanqin/p/5282929.html(允许转载,但请注明原创链接) BeanFactory加载单个文件 当使用beanfactory去获取 ...

  7. Spring加载xml配置文件的方式(BeanFactory和ApplicationContext区别)

    描述 大家都知道Java读普通文件是通过Basic I/O 中的InputStream.OutStream.Reader.Writer 等实现的.在spring 框架中,它是怎样识别xml这个配置文件 ...

  8. Spring加载xml配置文件的方式

    梳理Spring的流程 xml是最常见的spring 应用系统配置源.Spring中的几种容器都支持使用xml装配bean,包括: XmlBeanFactory,ClassPathXmlApplica ...

  9. spring加载属性(properties)文件

    一.注解方式加载 jdbc.driver=org.mariadb.jdbc.Driver jdbc.url=jdbc:mariadb://localhost:3306/kt jdbc.user=roo ...

随机推荐

  1. windows 计算器

    calc sin 弧度与角度

  2. lua执行的两种方式

    一.交互模式 二.脚本式 创建一个以lua结尾的文件,例如hello.lua文件内容 println("hello world")

  3. linux中hadoop组件启动日志存放问题

    如果是nohup xxxx &的话会在当前目录下生成一个nohup文件存放当前出现所有的日志,&表示后台启动: 当然你也可以将日志存放在>/dev/null表示当前日志存放的位置 ...

  4. NX二次开发-UFUN工程图表格注释检索默认单元格首选项UF_TABNOT_ask_default_cell_prefs

    NX9+VS2012 #include <uf.h> #include <uf_tabnot.h> #include <NXOpen/Part.hxx> #incl ...

  5. CocoaPods更新2018年11月06日16:06:48

    https://gems.ruby-china.org点进去就知道了…… CocoaPods命令 更新 sudo gem install -n /usr/local/bin cocoapods --p ...

  6. webstorm快捷键、支持vue文件等部分使用技巧

    转载:https://www.cnblogs.com/seven077/p/9771474.html 1.常用快捷键 shift+↑ 向上选取代码块shift+↓ 向下选取代码块ctrl+/ 注释/取 ...

  7. 牛客练习赛26 A 平面(结论题)

    题目链接:https://ac.nowcoder.com/acm/contest/907/A 思路:结论题,当做2n条直线,参考资料https://blog.csdn.net/qq_34131212/ ...

  8. Maven IntelliJ IDEA设置

    参考:博客地址: https://blog.csdn.net/huo920/article/details/82082403 Maven常用配置 在配置之前请将JDK安装好. 1. 环境变量配置 添加 ...

  9. 9款很棒的网页绘制图表JavaScript框架脚本

    推荐9款很棒的可在网页中绘制图表的JavaScript脚本,这些有趣的JS脚本可以帮助你快速方便的绘制图表(线.面.饼.条…),其中包括jQuery.MooTools.Prototype和一些其它的J ...

  10. 21-5-split

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...