spring加载属性配置文件内容
在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加载属性配置文件内容的更多相关文章
- 使用Spring加载properties配置文件.md
背景 类似于datasource.properties之类的配置文件,最初通过Java的Properties类进行处理.这种方式有许多弊端,如每次都需要读取配置文件:若将Properties作为成员变 ...
- spring 加载属性(properties)文件
在开发的过程中,配置文件往往就是那些属性(properties)文件,比如使用properties文件配置数据库文件,又如database-config.properties 代码清单:databas ...
- spring学习 十六 spring加载属性文件
第一步:创建一个properties文件,以数据库链接作为实例db.properties jdbc.url=jdbc:mysql://192.168.153.128:3306/mybaties?cha ...
- Spring加载Properties配置文件的三种方式
一.通过 context:property-placeholder 标签实现配置文件加载 1) 用法: 1.在spring.xml配置文件中添加标签 <context:property-plac ...
- Spring加载xml配置文件的方式 ApplicationContext
大家都知道Java读普通文件是通过Basic I/O 中的InputStream.OutStream.Reader.Writer 等实现的.在spring 框架中,它是怎样识别xml这个配置文件的呢? ...
- Spring加载XML配置文件
原创链接:http://www.cnblogs.com/yanqin/p/5282929.html(允许转载,但请注明原创链接) BeanFactory加载单个文件 当使用beanfactory去获取 ...
- Spring加载xml配置文件的方式(BeanFactory和ApplicationContext区别)
描述 大家都知道Java读普通文件是通过Basic I/O 中的InputStream.OutStream.Reader.Writer 等实现的.在spring 框架中,它是怎样识别xml这个配置文件 ...
- Spring加载xml配置文件的方式
梳理Spring的流程 xml是最常见的spring 应用系统配置源.Spring中的几种容器都支持使用xml装配bean,包括: XmlBeanFactory,ClassPathXmlApplica ...
- spring加载属性(properties)文件
一.注解方式加载 jdbc.driver=org.mariadb.jdbc.Driver jdbc.url=jdbc:mariadb://localhost:3306/kt jdbc.user=roo ...
随机推荐
- Batch - FINDSTR
总结 Searches for strings in files. 在文件中寻找特定的字符串 官方文档 C:\Users\cuixunxu>FINDSTR /? Searches for str ...
- c++ TCP 获取客户端IP
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> #i ...
- 微信小程序自定义导航栏组件,完美适配所有手机,可实现各种功能和情况
背景 在做小程序时,关于默认导航栏,我们遇到了以下的问题: Android.IOS 手机对于页面 title 的展示不一致,安卓 title 的显示不居中 页面的 title 只支持纯文本级别的样式控 ...
- NX二次开发-UFUN按类选择对话框UF_UI_select_with_class_dialog
#include <uf.h> #include <uf_ui.h> UF_initialize(); //按类选择对话框 char sCue[] = "按类选择对话 ...
- Shader实现新手指引挖空圆形和矩形
这个是参考这个做的:https://blog.csdn.net/tracyzly/article/details/80279692 Shader "UI/ImageWithHole" ...
- Python 爬虫-爬取京东手机页面的图片
具体代码如下: __author__ = 'Fred Zhao' import requests from bs4 import BeautifulSoup import os from urllib ...
- OCP—051试题
FROM: http://blog.itpub.net/26736162/viewspace-1252569/?page=2 http://blog.csdn.net/elearnings/artic ...
- Apache Shiro RememberMe 1.2.4 反序列化漏洞
拉取镜像 docker pull medicean/vulapps:s_shiro_1 启动环境 docker run -d -p 80:8080 medicean/vulapps:s_shiro_1 ...
- Unity中销毁游戏对象的方式
销毁方式 销毁物体的方式有两种:Destroy和DestroyImmediate两种,那两者有什么区别呢?细听分说. 两种方式都能达到销毁物体的目的,有以下区别: Destroy销毁场景中的物体但是内 ...
- Read-Write lock 看可以,不过看的时候不能写
当线程“读取”实例的状态时,实例的状态不会改变,只有线程对实例“写入”操作时才会改变.read-write lock 模式将读取和写入分开来处理,在读取数据前获取读锁定,而写入之前,必须获取写锁定. ...