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 ...
随机推荐
- cmake -help
{ Usage cmake [options] <path-to-source> cmake [options] <path-to-existing-build> Speci ...
- Android NDK 环境变量配置
NDK_ROOT = C:\__S_D_K__\AndroidNDK\android-ndk-r20 在path 中加入 %NDK_ROOT% 我的路径在C盘 //个别的程序可能需要 NDK_ROO ...
- FFmpeg Download
https://ffmpeg.zeranoe.com/builds/
- Dart编程循环
有时,某些指令需要重复执行.循环是一种理想的方法.循环表示必须重复的一组指令.在循环的上下文中,重复被称为迭代 . 下图说明了循环的分类 让我们开始讨论确定循环.迭代次数是确定/固定的循环称为确定循环 ...
- [Python]PDF合成小程序PDF合成小程序
运行平台:Python3.5 用刀了PyPDF2这个库,需要提前下载好. 源码如下: import PyPDF2, os #建立一个装pdf文件的数组pdfFiles = [] for fileNam ...
- Api:目录
ylbtech-Api:目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://ylbtech.c ...
- Unity NGUI 粒子的排序
Unity NGUI系统中是没有对粒子进行排序的,如: 怎么解决这个问题呢? 思路是把粒子的渲染层级,相对于UI组件的层级进行一个偏移. 解决后的效果如下: 代码如下: using System.Co ...
- 10.2 External interrupt/event controller (EXTI)
EXTI控制器的主要特点如下: 每个中断/事件线上的独立触发器和掩码 每个中断行的专用状态位 生成最多20个软件事件/中断请求 脉冲宽度小于APB2时钟周期的外部信号检测. 每条中断线路的专用状态位生 ...
- Harbor任意管理员注册漏洞复现CVE-2019-16097
注册时抓包 添加poc "has_admin_role":true 管理员权限 POC POST /api/users HTTP/1.1 Host: 127.0.0.1 Conte ...
- 2018湘潭大学程序设计竞赛【H】
题目链接:https://www.nowcoder.com/acm/contest/105/H 题意:两个操作,一个在[l,r]区间放颜色为c的球,一个统计在[l,r]里有多少不同颜色的球. 题解:哎 ...