Spring的PropertyPlaceholderConfigurer
在项目中我们一般将配置信息(如数据库的配置信息)配置在一个properties文件中,如下:
jdbcUrl=jdbc:mysql://localhost:3306/flowable?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE&useSSL=false&serverTimezone=UTC
userName=root
userPassword=
jdbcDriver=com.mysql.cj.jdbc.Driver
接着在Spring的配置文件中读取,有两种方式:
方式一:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- 对于读取一个配置文件采取的方案 -->
<property name="location" value="classpath:config.properties"></property>
<!-- 对于读取两个以上配置文件采取的处理方案 -->
<!--
<property name="locations">
<list>
<value>classpath:config.properties</value>
<value>classpath:config2.properties</value>
</list>
</property>
-->
</bean>
方式二:
<!--采用这种方式简化配置文件-->
<context:property-placeholder location="classpath:config.properties"/>
我们知道,不论是使用PropertyPlaceholderConfigurer还是通过context:property-placeholder这种方式进行实现,都需要记住,Spring框架不仅仅会读取我们的配置文件中的键值对,而且还会读取Jvm 初始化的一下系统的信息。有时候,我们需要将配置Key定一套命名规则 ,例如
项目名称.组名.功能名=配置值
org.team.tfunction=0001
同时,我们也可以使用下面这种配置方式进行配置,这里我配NEVER的意思是不读取系统配置信息。如:
<context:property-placeholder location="classpath:config.properties" system-properties-mode="NEVER"/>
测试用例:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-core.xml")
public class SpringContextTest { @Value("${userName}")
private String uname; @Value("${userPassword}")
private String pwd; @Test
public void test() {
System.out.println("username:" + uname);
System.out.println("password:" + pwd);
} }
username:root
password:123456
配置文件中使用就比较简单了:
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<!-- 数据库基本信息配置 -->
<property name="url"
value="${jdbcUrl}" />
<property name="username" value="${userName}" />
<property name="password" value="${userPassword}" />
<property name="driverClassName" value="${jdbcDriver}" />
</bean>
Spring的PropertyPlaceholderConfigurer的更多相关文章
- Spring中PropertyPlaceholderConfigurer的使用
Spring中PropertyPlaceholderConfigurer的使用 在使用Spring配置获取properties文件时,在网上查到相关的资料,分享哈!! (1)获取一个配置文件 ...
- Spring的PropertyPlaceholderConfigurer应用
Spring 利用PropertyPlaceholderConfigurer占位符 1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是BeanFa ...
- Spring 利用PropertyPlaceholderConfigurer占位符
Hey Girl 博客园 首页 博问 闪存 新随笔 订阅 管理 posts - 42, comments - 3, trackbacks - 0 Sp ...
- Spring的PropertyPlaceholderConfigurer应用(转)
转自:http://www.cnblogs.com/yl2755/archive/2012/05/06/2486752.html Spring 利用PropertyPlaceholderConfigu ...
- Spring里PropertyPlaceholderConfigurer类的使用
1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现.PropertyPlaceho ...
- spring中propertyplaceholderconfigurer简介
Spring的框架中为您提供了一个 BeanFactoryPostProcessor 的实作类别: org.springframework.beans.factory.config.PropertyP ...
- spring 的 PropertyPlaceholderConfigurer读取的属性怎么访问 (java访问方式,不是xml中的占位符哦)及此类的应用
一.1.占位符的应用:(@Autowired注解方式,不需要建立set与get方法了,xml注入也不需要写了) http://www.cnblogs.com/susuyu/archive/2012/0 ...
- Spring的PropertyPlaceholderConfigurer强制使用默认值的坑
1.问题 dubbo client配置: <dubbo:reference id="channelCustomerClient" interface="com.gt ...
- Spring的PropertyPlaceholderConfigurer事例应用
在开发的过程中,经常发现一些类似:${log4j.level}之类的内容,后来才知道原因.下面解释一下: 1.PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现 ...
随机推荐
- Matlab-2:二分法工具箱
function g=dichotomy(f,tol) %this routine uses bisection to find a zero of user-supplied %continuous ...
- input事件在ie9以下不兼容问题完美解决
上周四好不容易加了几天班把刚接手的一个pc页面做完,周五同事说要兼容ie7~ie9,结果在上面一跑,输入都没法输入. 我的需求是用6个span作为虚拟的密码输入框,实际上是用一个藏在页面里的input ...
- 安卓——AlertDialog多样按钮
在xml 设计页面添加标签 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmln ...
- hdu-4180-exgcd
RealPhobia Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 函数使用八:BP_EVENT_RAISE
此函数是关联触发一个已经定义的事件,这个事件可以放到SM36里设置JOB,这样就做成了一个事件触发JOB的东西. Import EVENTID 事件ID ,对应S ...
- 使用SpringBoot集成ActiveMQ
SpringBoot是个好东西,好多java常用的东西都被集成进去了 JMS 在 Spring Boot 中的使用 使用Spring/Spring Boot集成JMS的陷阱 Spring-boot J ...
- IDEA Activiti Designer插件---actiBPM汉字乱码问题
1.找到IDEA的安装目录 2.用编辑器打开,在文件末尾添加 -Dfile.encoding=UTF-8
- 一、持久层框架(Hibernate)
一.Hibernate 使用JDBC做数据库相关功能开发会做很多重复性的工作,创建连接,关闭连接,把字段逐一映射到属性中等.Hibernate把这些进行封装起来,使得数据库访问变得轻松简单. 1.创建 ...
- C++ leetcode::Reverse Integer
第一天上课,数据库老师说对于计算机系的学生,凡是在课本上学到的专业知识都是过时的.深以为然,感觉大学两年半真的不知道学了什么,为未来感到担忧,C++也不敢说是精通,入门还差不多.最近丧的不行,不管怎么 ...
- weblogic控制台用户名密码修改
1.记得用户名密码但想修改密码修改方法 保存后立即生效,即你退出后即能以新密码登录:但由于启动的用户名密码和登录的用户名密码是同一个,所以我们需要去修改DOMAIN_HOME/servers/serv ...