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 ... 
随机推荐
- SurfaceFlinger(  226): Permission Denial: can't access SurfaceFlinger
			MODIFY_PHONE_STATE permission is granted to system apps only. For your information, there are 2 type ... 
- log4j修改SMTPAppender支持ssl
- Lua游戏开发参考资料收集
			table {border-collapse:collapse;} List of game engines that use lua: 2D Agen (Lua; Windows) Blitwiza ... 
- 详细解读Android中的搜索框(一)—— 简单小例子
			这次开的是一个讲解SearchView的栏目,第一篇主要是给一个小例子,让大家对这个搜索视图有一个了解,之后再分布细化来说. 目标: 我们先来定个目标,我们通过搜索框来输入要搜索的联系人名字,输入的时 ... 
- [转]Linux常用命令大全
			From : http://www.php100.com/html/webkaifa/Linux/2009/1106/3485.html 系统信息 arch 显示机器的处理器架构(1) uname - ... 
- Springmvc的handler method参数绑定常用的注解
			转自:http://blog.longjiazuo.com/archives/1149 1. 简介: handler method参数绑定常用的注解,我们根据他们处理的Request的不同内容部分 ... 
- MVC详解(转)
			原文链接:MVC详解 MVC与模板概念的理解 MVC(Model View Controller)模型-视图-控制器 MVC本来是存在于Deskt op程序中的,M是指数据模型,V是指用户界面,C ... 
- ubuntu 安装 android studio
			总共分三个步骤: 安装JDK 这里引用下别人的安装过程http://blog.csdn.net/bhq2010/article/details/6839762 注:android studio 版本为 ... 
- 硬链接(hard link)和符号连接(symbolic link)
			inode ====== 在Linux系统中,内核为每一个新创建的文件分配一个inode,每个文件都有一个惟一的inode号,我们可以将inode简单理解成一个指针,它永远指向本文件的具体存储位置.文 ... 
- 超酷实用的jQuery焦点图赏析及源码
			焦点图应用在现代网页中使用十分广泛,有注重简单小巧的,也有注重华丽美观的,大部分jQuery焦点图都可以滑动和切换图片,并且在切换图片时产生不同的动画特效.今天我们要分享一些超酷而且实用的jQuery ... 
