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. 结构-行为-样式-AngularJs--指令实现 手动触发响应到页面指定位置

    最近工作需要增强用户体验,项目经理说下拉框架用户体验太差,于是乎我开始想了如下指令.这个指令可以让用户点击的时候把下拉或者其他的响应显示的东西不会因为屏幕的滚动而看不见,也就是让用户看见他想看见的. ...

  2. 【CSS学习笔记】CSS选择器

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  3. USACO 3.4 Raucous Rockers

    Raucous Rockers You just inherited the rights to N (1 <= N <= 20) previously unreleased songs ...

  4. cocoaPods第三方库使用详解

    终端上安装了cocoapods后,打开终端输入下面命令: cd /Users/Sivek_lin/Desktop/AFNTest/AFNTest touch podfile pod search af ...

  5. 【锋利的Jquery】读书笔记四

    jquery中的事件及动画 一.事件 页面加载 $(document).ready(function(){xxxxx}) 简写 $(function(){ //do something }) 元素绑定 ...

  6. Uva 11889 Benefit (lcm与gcd)

    题意:给你两个数,a,c,求出 lcm(a,b)==c 时的 b 的最小值 思路:我们知道一个性质 gcd(a,b)*lcm(a,b) = a*b 由此我们可以得到 b = gcd(a,b)*lcm( ...

  7. New : HTML5 中的新标签

    基础 标签 描述 <!DOCTYPE>  定义文档类型. <html> 定义 HTML 文档. <title> 定义文档的标题. <body> 定义文档 ...

  8. mybatis学习笔记四(动态sql)

    直接贴图,注解在代码上,其他的配置文件在学习一中就不贴了 1 数据库 2 实体类 package com.home.entity; /** * 此类是: 用户实体类 * @author hpc * @ ...

  9. QTREE - Query on a tree

    QTREE - Query on a tree 题目链接:http://www.spoj.com/problems/QTREE/ 参考博客:http://blog.sina.com.cn/s/blog ...

  10. 2014年蓝桥杯预选赛 C/C++ 本科A组试题--切面条

    //主要是要找到f(n)=2*f(n-1)-1的规律. #include <stdio.h> #include <math.h> int f(int n) { if(n==0) ...