Spring的PropertyPlaceholderConfigurer事例应用
在开发的过程中,经常发现一些类似:${log4j.level}之类的内容,后来才知道原因。下面解释一下:
1、PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是BeanFactoryPostProcessor接口的一个实现。PropertyPlaceholderConfigurer可以将上下文(配置文件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进行修改,而不用对xml配置文件进行修改。
2、在Spring中,使用PropertyPlaceholderConfigurer可以在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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.***.data.***" />
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:project.properties</value>
</property>
</bean>
<import resource="classpath*:/db/*.xml" />
<import resource="classpath*:/common/*.xml" />
</beans>
2、文件antx.properties:
session.mode=test
session.userId=99758820
session.testDate=2013-07-17
3、在其他的xml文件里可以这么写:
<bean id="sessionConfig" class="com.***.data.***.acl.SessionConfig">
<property name="mode">
<value>${session.mode}</value>
</property>
<property name="userId">
<value>${session.userId}</value>
</property>
<property name="testDate">
<value>${session.testDate}</value>
</property>
</bean>
4、这样,一个简单的数据源就设置完毕了。可以看出:PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义的工具。
5、当然也可以在JAVA代码中使用
private static final Properties sysConfig = new Properties();
static {
try {
InputStream iStream = new FileInputStream(new File("config", "antx.properties"));
sysConfig.load(iStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getPropertyValue(String key){
return sysConfig.getProperty(key);
}
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
在项目中我们一般将配置信息(如数据库的配置信息)配置在一个properties文件中,如下: jdbcUrl=jdbc:mysql://localhost:3306/flowable?useUnico ...
随机推荐
- C#编程(六十八)----------LINQ小结
LINQ小结 一.LINQ是什么 LINQ也就是Language Interrated Query的缩写,怎么一个缩写法我也不明白,即语言集成查询,是微软在.NET3.5中提出的一项新技术,LINQ主 ...
- ibatis.net:惯用法
使用<![CDATA[]]>保持SQL格式 IN 查询
- 女子监狱第一季/全集Orange Is the New Black迅雷下载
本季第一季 Orange Is the New Black 1 (2013) 看点:该剧描述主人公Piper Chapman(Taylor Schilling)在大学里结识了毒贩Alex(Laura ...
- mysql 内存表
show variables like 'max_%'; max_heap_table_size 16777216 max_tmp_tables 32 show variables l ...
- Material Designer的低版本兼容实现(十二)—— Slider or SeekBar
Slider,我更喜欢叫他SeekBar,其实是一个东西啦,就是拖动条.5.0的拖动条和4.x上的HOLO风格完全不同,平添了一些精致.此外还加入了数值指示器,让用户在滑动的时候就能知道现在到了什么位 ...
- [转]PHP 汉字转拼音
转自: https://git.oschina.net/wapznw/php-pinyin <?php /** * @package default * @copyright php-pinyi ...
- Java学习笔记——File类文件管理及IO读写、复制操作
File类的总结: 1.文件和文件夹的创建 2.文件的读取 3.文件的写入 4.文件的复制(字符流.字节流.处理流) 5.以图片地址下载图片 文件和文件夹 相关函数 (boolean) mkdir( ...
- Controller向View传递数据
1. 使用ViewData传递数据 我们在Controller中定义如下: ViewData[“Message”] = “Hello word!”; 然后在View中读取Controlle ...
- 仿qq底部的提示标记
看到一个比較不错的开源项目,分享给大家: <?xml version="1.0" encoding="utf-8"?> <RelativeLa ...
- 设置让php能够以root权限来执行exec() 或者 shell_exec()
一.查看启动你php的进程的用户是谁. 可以通过在命令行执行:ps -ef | grep php来看.或者在php中执行 echo exec('whoami') 来查看.centos下默认会是nobo ...