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. HTML <textarea> 标签的 wrap 属性

    HTML <textarea> 标签的 wrap 属性 wrap 属性 通常情况下,当用户在输入文本区域中键入文本后,浏览器会将它们按照键入时的状态发送给服务器.只有用户按下 Enter ...

  2. Windows平台下安装Eclipse插件,开发Hadoop应用

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  3. C语言之逻辑运算符

    一 逻辑运算符: &&:逻辑与,读作并且 表达式左右两边都为真,那么结果才为真 口诀:一假则假 ||:逻辑或,读作或者 表达式左右两边,有一个为真,那么结果就为真 口诀:一真则真 !: ...

  4. 程序集和反射(C#)

    这里我又唠叨几句,大家在学习的时候,如看书或者看视频时觉得非常爽,因为感觉基本都看得懂也都挺容易的,其实看懂是一回事,你自己会动手做出来是一回事,自己能够说出来又是另一回事了.应该把学到的东西变成自己 ...

  5. 无线网卡连接internet,有线网卡向另一台电脑分享网络(笔记本当有线路由器)

    有一台笔记本和一台台式机,都放在卧室内 笔记本能用无线网卡连接到客厅的路由器,台式机没有配备无线网卡,不能上网 要从客厅的路由器那边拉一条网线到卧室内连接台式机显然很蠢,在卧室内购置一台中继路由器也不 ...

  6. CodeForces 632D Longest Subsequence

    暴力. 虽然$a[i]$最大有${10^9}$,但是$m$最大只有${10^6}$,因此可以考虑暴力. 记$cnt[i]$表示数字$i$有$cnt[i]$个,记$p[i]$表示以$i$为倍数的情况下, ...

  7. Maven配置插件跳过测试代码的编译和运行

    Maven配置插件跳过测试代码的编译和运行: <!-- 编译插件 --> <plugin> <groupId>org.apache.maven.plugins< ...

  8. 【LeetCode】25. Reverse Nodes in k-Group

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k  ...

  9. Ubuntu 16.04 Django安装和配置

    之前有安装和配置过,换了台电脑,再安装和配置,忽然发现差不多都忘记了,这里记录下已备之后查阅. sudo apt-get install python-pip sudo apt-get install ...

  10. 命令行启动Hololens Emulator,可解决内存不足的问题

    有时候在虚拟机测试时常会出现 内存不足 的情况,导致应用卡顿,调整 /memsize 参数大小: start "HoloLens" "C:\Program Files ( ...