【转】spring管理属性配置文件properties——使用PropertiesFactoryBean|spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer
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的更多相关文章
- spring 通过@Value 获取properties文件中设置了属性 ,与@Value # 和$的区别
spring 获取 properties的值方法 在spring.xml中配置 很奇怪的是,在context-param 加载的spring.xml 不能使用 ${xxx} 必须交给Dispatche ...
- Spring Cloud Config-Client 无法获取 Config-Server 在 github 上的配置文件的属性值,竟然是因为
Spring Cloud Config-Client 无法获取 Config-Server 在 github 上的配置文件的属性值,竟然是因为!!! 2018年07月23日 16:33:25 一颗很菜 ...
- Java框架spring 学习笔记(十):bean管理(注解和配置文件混合使用)
配置文件和注解混合使用 创建对象操作使用配置文件方式实现 注入属性的操作使用注解方式实现 编写BookDao.java和OrderDao.java文件 BookDao.java package com ...
- Spring中使用@Value读取porperties文件中的属性值方法总结及注意事项
本文为博主原创,转载请注明出处. 此前曾总结过使用工具类读取properties文件中的属性值,有兴趣的可以看一下. 如何快速获取properties中的配置属性值:https://www.cnblo ...
- Spring+SpringMVC+MyBatis深入学习及搭建(三)——MyBatis全局配置文件解析
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6874672.html 前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(二)——My ...
- Spring框架学习笔记(10)——Spring中的事务管理
什么是事务 举例:A给B转500,两个动作,A的账户少500,B的账户多500 事务就是一系列的动作, 它们被当做一个单独的工作单元. 这些动作要么全部完成, 要么全部不起作用 一.注解添加事务管理方 ...
- Spring Boot 2.3.0正式发布:优雅停机、配置文件位置通配符新特性一览
当大潮退去,才知道谁在裸泳..关注公众号[BAT的乌托邦]开启专栏式学习,拒绝浅尝辄止.本文 https://www.yourbatman.cn 已收录,里面一并有Spring技术栈.MyBatis. ...
- Spring企业级程序设计 • 【第4章 Spring持久化层和事务管理】
全部章节 >>>> 本章目录 4.1 配置数据源资源 4.1.1 JdbcTemplate介绍 4.1.2通过ComboPooledDataSource创建数据源 4.1. ...
- Spring企业级程序设计 • 【第2章 Spring Bean管理进阶】
全部章节 >>>> 本章目录 2.1 bean标签和import标签 2.1.1 标签中的id属性和name属性 2.1.2 Bean的作用范围和生命周期 2.1.2 Be ...
随机推荐
- 如何在标题栏的title前添加网站logo
之前在哪个网上看到的,今天没事给写下来了: 1.将像素大小为16*16的图片命名为 favicon.ico,(图片使用的颜色好像不能超过16色),放到网站根目录下就可以了; 2.在head标签中添加代 ...
- 关于元素加上margin属性后以谁为基准移动的问题及负margin的问题
突然想起这个问题,这是很基础很基础的问题啊,但之前很多次都忘记了,然后困扰了我很久.不清不楚的感觉很不好,所以要做成笔记比较好记住,好记性不如烂笔头,以后再次困惑了再回来看看.推荐文章,海玉的< ...
- CodeForces 631D Messenger
$KMP$. $n=1$和$n=2$的时候可以单独计算.$n>2$时,可以拿字符和数字分别做一次匹配,然后扫描一遍判断一下就可以计算出答案了. #pragma comment(linker, & ...
- expressJS - 准备活动
安装 nodeJS 1. package.json 2. CommonJS, AMD, ES6 3. Babel
- 利用DataSet更改数据,将更改保存到数据库中
RowState 是 DataRow 很重要的一个属性, 表示 DataRow 当前的状态. RowState 有 Added, Modified, Unchanged, Deleted, Detac ...
- navicat导出数据结构及数据
右键选中数据库-->右键->数据传输->高级->选中所需导出的表->选择文件
- android之控件与布局
基本控件:TextViewButtonEditTextImageViewAlertDialog.BubliderProgressDialog 四种基本布局的特殊属性: LinerLayout andr ...
- python2 基础
标识符 标识符是由字母,下划线和字母组成的字符序列标识符必须以字母,下划线开头,不能以数字开头标识符不能是关键字标识符可以为任意长度 算术运算符+-*///**% 科学记数法 aEb 或者aE+b例: ...
- [HMLY]4.CocoaPods详解----制作
作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/20067595 转载请注明出处 学会使用别人的pods依赖库后,你一 ...
- 相机标定 matlab opencv ROS三种方法标定步骤(2)
二 ubuntu下Opencv的相机标定 一般直接用Opencv的源码就可以进行相机的标定,但是可能只是会实现结果,却不懂实现的过程,我也是模模糊糊的看了<计算机视觉中的多视图几何>以及 ...