spring管理属性配置文件properties——使用PropertiesFactoryBean

对于属性配置,一般采用的是键值对的形式,如:key=value

属性配置文件一般使用的是XXX.properties,当然有时候为了避免eclipse把properties文件转码,放到服务器上认不出中文,可以采用XXX.conf的形式管理属性配置。
spring对于属性文件有自己的管理方式,通过spring的管理,可以直接使用@Value的方式直接得到属性值。
先使用org.springframework.beans.factory.config.PropertiesFactoryBean对属性配置文件进行管理。

1.新建一个Javaproject,命名spring_test;

2.导入jar包:

aopalliance-1.0.jar
commons-logging-1.1.1.jar
org.springframework.test-3.1.0.RELEASE.jar
spring-aop-3.1.1.RELEASE.jar
spring-asm-3.1.1.RELEASE.jar
spring-beans-3.1.1.RELEASE.jar
spring-context-3.1.1.RELEASE.jar
spring-context-support-3.1.1.RELEASE.jar
spring-core-3.1.1.RELEASE.jar
spring-expression-3.1.1.RELEASE.jar

3.在src下新建一个config.propertites:

author_name=lee

4.新建一个文件夹config;

5.新建一个app.conf:

project_info=项目

6.新建一个spring配置文件applicationContext.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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config />
<context:component-scan base-package="com.*"></context:component-scan> <!-- 使用注解注入properties中的值 -->
<bean id="setting"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:*.properties</value>
<value>file:config/*.conf</value>
</list>
</property>
<!-- 设置编码格式 -->
<property name="fileEncoding" value="UTF-8"></property>
</bean>
</beans>

7.新建一个获取属性配置文件属性的类ConfigProperty.java:

package com.lee.property.test;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; /**
* config.properties文件映射类
*
*
*/
@Component("configProperty")
public class ConfigProperty { /** 作者名字 */
@Value("#{setting[author_name]}")
private String authorName;
/** 项目信息 */
@Value("#{setting[project_info]}")
private String projectInfo; public String getAuthorName() {
return authorName;
} public void setAuthorName(String authorName) {
this.authorName = authorName;
} public String getProjectInfo() {
return projectInfo;
} public void setProjectInfo(String projectInfo) {
this.projectInfo = projectInfo;
} }

8.新建测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class ConfigPropertyTest { @Resource(name = "configProperty")
private ConfigProperty configProperty; /**
* 测试Spring注解获取properties文件的值
*/
@Test
public void test() {
System.out.println(configProperty.getAuthorName());
System.out.println(configProperty.getProjectInfo());
} }

总结:

1.使用org.springframework.beans.factory.config.PropertiesFactoryBean获取属性的方式是:

@Value("${beanid['properties_key']}")

2.使用

<!-- 设置编码格式 -->
<property name="fileEncoding" value="UTF-8"></property>

主要为了解决属性文件中value为中文时乱码的问题。

spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer

上一篇文章spring管理属性配置文件properties——使用PropertiesFactoryBean中提到使用org.springframework.beans.factory.config.PropertiesFactoryBean管理属性文件,在学习过程中发现通过org.springframework.beans.factory.config.PropertyPlaceholderConfigurer也可以管理配置文件。

1.在上一个项目(spring_test)的基础上新建spring配置文件applicationContext2.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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config />
<context:component-scan base-package="com.*"></context:component-scan>
<!-- <context:property-placeholder location="file:config/*.conf" ignore-unresolvable="true"/>
<context:property-placeholder location="classpath:*.properties" ignore-unresolvable="true"/> --> <!-- 使用注解注入properties中的值 -->
<bean id="setting"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:*.properties</value>
<value>file:config/*.conf</value>
</list>
</property>
<!-- 设置编码格式 -->
<property name="fileEncoding" value="UTF-8"></property>
</bean>
</beans>

2.修改ConfigProperty.java:

@Component("configProperty")
public class ConfigProperty { /** 作者名字 */
@Value("${author_name}")
private String authorName;
/** 项目信息 */
@Value("${project_info}")
private String projectInfo; public String getAuthorName() {
return authorName;
} public void setAuthorName(String authorName) {
this.authorName = authorName;
} public String getProjectInfo() {
return projectInfo;
} public void setProjectInfo(String projectInfo) {
this.projectInfo = projectInfo;
} }

3.新建测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext2.xml"})
public class ConfigPropertyTest { @Resource(name = "configProperty")
private ConfigProperty configProperty; /**
* 测试Spring注解获取properties文件的值
*/
@Test
public void test() {
System.out.println(configProperty.getAuthorName());
System.out.println(configProperty.getProjectInfo());
} }

总结:

1.使用PropertiesFactoryBean管理属性文件获取属性的方式:

@Value("${properties_key}")

和org.springframework.beans.factory.config.PropertiesFactoryBean有点不同;

2.在spring配置文件中,对于bean的配置有这样一个配置:

<property name="ignoreUnresolvablePlaceholders" value="true" />   

这个主要是为了解决抛出cannot be resolved的异常。

【转】 http://blog.csdn.net/lee0723/article/details/48715827

http://blog.csdn.net/lee0723/article/details/48716515

【转】spring管理属性配置文件properties——使用PropertiesFactoryBean|spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer的更多相关文章

  1. spring 通过@Value 获取properties文件中设置了属性 ,与@Value # 和$的区别

    spring 获取 properties的值方法 在spring.xml中配置 很奇怪的是,在context-param 加载的spring.xml 不能使用 ${xxx} 必须交给Dispatche ...

  2. Spring Cloud Config-Client 无法获取 Config-Server 在 github 上的配置文件的属性值,竟然是因为

    Spring Cloud Config-Client 无法获取 Config-Server 在 github 上的配置文件的属性值,竟然是因为!!! 2018年07月23日 16:33:25 一颗很菜 ...

  3. Java框架spring 学习笔记(十):bean管理(注解和配置文件混合使用)

    配置文件和注解混合使用 创建对象操作使用配置文件方式实现 注入属性的操作使用注解方式实现 编写BookDao.java和OrderDao.java文件 BookDao.java package com ...

  4. Spring中使用@Value读取porperties文件中的属性值方法总结及注意事项

    本文为博主原创,转载请注明出处. 此前曾总结过使用工具类读取properties文件中的属性值,有兴趣的可以看一下. 如何快速获取properties中的配置属性值:https://www.cnblo ...

  5. Spring+SpringMVC+MyBatis深入学习及搭建(三)——MyBatis全局配置文件解析

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6874672.html 前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(二)——My ...

  6. Spring框架学习笔记(10)——Spring中的事务管理

    什么是事务 举例:A给B转500,两个动作,A的账户少500,B的账户多500 事务就是一系列的动作, 它们被当做一个单独的工作单元. 这些动作要么全部完成, 要么全部不起作用 一.注解添加事务管理方 ...

  7. Spring Boot 2.3.0正式发布:优雅停机、配置文件位置通配符新特性一览

    当大潮退去,才知道谁在裸泳..关注公众号[BAT的乌托邦]开启专栏式学习,拒绝浅尝辄止.本文 https://www.yourbatman.cn 已收录,里面一并有Spring技术栈.MyBatis. ...

  8. Spring企业级程序设计 • 【第4章 Spring持久化层和事务管理】

    全部章节   >>>> 本章目录 4.1 配置数据源资源 4.1.1 JdbcTemplate介绍 4.1.2通过ComboPooledDataSource创建数据源 4.1. ...

  9. Spring企业级程序设计 • 【第2章 Spring Bean管理进阶】

    全部章节   >>>> 本章目录 2.1 bean标签和import标签 2.1.1 标签中的id属性和name属性 2.1.2 Bean的作用范围和生命周期 2.1.2 Be ...

随机推荐

  1. css ie6最小高度问题

    最小高度问题:  这个最小高度 min-height:的问题,因为min-height:只在IE7\FF中起作用.至于这个IE6死活就是不认.而我这个页面又必需得用这个最小高度来定.   但头痛的是I ...

  2. 游戏排行榜-Python实现

    背景介绍 排行榜通常是游戏中为了激发玩家的一种策略,那么对于开发人员来说如何完成一个排行榜的设计呢?如果这个排行榜是动态的如何才能高效的对比出结果呢?如果排行榜实时性较高如何给用户展示出用户是进步了还 ...

  3. C语言之冒泡排序

    冒泡排序: 1). 简介 其实就是把一个数组的元素,按照从小到大(从大到小)得顺序,重新排列起来,这种排序就叫冒泡排序 例: int nums[5] = {5,4,3,2,1}; //经过排序后 下标 ...

  4. Linux实战教学笔记10:正则表达式

    第十节 正则表达式 标签(空格分隔):Linux实战教学笔记 ---更多资料点我查看 第1章 什么是正则表达式 正则表达式就是为了处理大量的文本|字符串而定义的一套规则和方法 通过定义的这些特殊符号的 ...

  5. trie树模板(统计难题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  6. Spark Graphx编程指南

    问题导读1.GraphX提供了几种方式从RDD或者磁盘上的顶点和边集合构造图?2.PageRank算法在图中发挥什么作用?3.三角形计数算法的作用是什么?Spark中文手册-编程指南Spark之一个快 ...

  7. delphi用TAdoStoredProc调用存储过程,兼容sql2005、2008、2014的远程事务问题

    delphi7写的程序,在sql2000里没问题,调用sql2008.2014里的存储过程时,如果存储过程里操作了大量数据,很容易会莫名其妙的自己撤销掉,但是程序还识别不到,认为还在正常执行.今天尝试 ...

  8. HMM 前向后向算法(转)

    最近研究NLP颇感兴趣,但由于比较懒,所以只好找来网上别人的比较好的博客,备份一下,也方便自己以后方便查找(其实,一般是不会再回过头来看的,嘿嘿 -_-!!) 代码自己重新写了一遍,所以就不把原文代码 ...

  9. Cg(C for Graphic)标准函数库之数学函数与几何函数

    和 C 的标准函数库类似, Cg 提供了一系列内建的标准函数.这些函数用于执行数学上的通用计算或通用算法(纹理映射等),例如,需要求取入射光线的反射光线方向向量可以使用标准函数库中的 reflect ...

  10. 分享基于.NET MVC+EF CodeFirst+IOC+EasyUI的框架设计

    **注:要做工,没什么时间,等有空时会上传到GIT,项目结构如上,简单的说一下: **支持IOC及多数据库等,各项目由MVC区域隔离: 主要使用基于接口与抽象类进行高度的抽象与接口隔离,与其它框架比较 ...