Spring PropertyPlaceholderConfigurer类载入外部配置
通常在Spring项目中如果用到配置文件时,常常会使用org.springframework.beans.factory.config.PropertyPlaceholderConfigurer来简化代码,例如:
<bean id="aaa" class="com.zero.spring.SpringIoc.test.AAA">
<property name="name" value="zero"/>
<property name="gender">
<value>man</value>
</property>
</bean>
我们希望将name、gender的值写在配置文件中,以后的改动只需要对配置文件进行修改即可,于是就会用到PropertyPlaceholderConfigurer:
test.properties
name=zero
gender=man
beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="propertyConf" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="test.properties"/>
</bean>
<bean id="aaa" class="com.zero.spring.SpringIoc.test.AAA">
<property name="name" value="${name}"/>
<property name="gender">
<value>${gender}</value>
</property>
</bean>
</beans>
这里可以更简化我们的配置,使用<context:property-placeholder/>,它会自动加载PropertyPlaceholderConfigurer这个Bean(详细请看源码),于是就有了如下的配置:
<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="test.properties"/>
<bean id="aaa" class="com.zero.spring.SpringIoc.test.AAA">
<property name="name" value="${name}"/>
<property name="gender">
<value>${gender}</value>
</property>
</bean>
</beans>
<context:property-placeholder/>其它参数如下:
<context:property-placeholder
location="属性文件,多个之间逗号(,)分隔"
file-encoding="文件编码"
ignore-resource-not-found="是否忽略找不到的属性文件,默认false,即不忽略,找不到将抛出异常"
ignore-unresolvable="是否忽略解析不到的属性,如果不忽略,找不到将抛出异常"
properties-ref="本地Properties配置"
local-override="是否本地覆盖模式,即如果true,那么properties-ref的属性将覆盖location加载的属性,否则相反"
system-properties-mode="系统属性模式,默认ENVIRONMENT(表示先找ENVIRONMENT,再找properties-ref/location的),NEVER:表示永远不用ENVIRONMENT的,OVERRIDE类似于ENVIRONMENT"
order="顺序,当配置多个<context:property-placeholder/>时的查找顺序"
/>
转载于:https://my.oschina.net/zjllovecode/blog/1791907
Spring PropertyPlaceholderConfigurer类载入外部配置的更多相关文章
- Spring Boot 支持多种外部配置方式
Spring Boot 支持多种外部配置方式 http://blog.csdn.net/isea533/article/details/50281151 这些方式优先级如下: 命令行参数 来自java ...
- Spring Boot实践——用外部配置填充Bean属性的几种方法
引用:https://blog.csdn.net/qq_17586821/article/details/79802320 spring boot允许我们把配置信息外部化.由此,我们就可以在不同的环境 ...
- Spring 后置处理器 PropertyPlaceholderConfigurer 类(引用外部文件)
一.PropertyPlaceholderConfigurer类的作用 PropertyPlaceholderConfigurer 是 BeanFactory 后置处理器的实现,也是 BeanFact ...
- 使用 spring.profiles.active 及 @profile 注解 动态化配置内部及外部配置
引言:使用 spring.profiles.active 参数,搭配@Profile注解,可以实现不同环境下(开发.测试.生产)配置参数的切换 一.根据springboot的配置文件命名约定,结合ac ...
- spring读取配置文件PropertyPlaceholderConfigurer类的使用
这里主要介绍PropertyPlaceholderConfigurer这个类的使用,spring中的该类主要用来读取配置文件并将配置文件中的变量设置到上下文环境中,并进行赋值. 一.此处使用list标 ...
- 外部配置属性值是如何被绑定到XxxProperties类属性上的?--SpringBoot源码(五)
注:该源码分析对应SpringBoot版本为2.1.0.RELEASE 1 前言 本篇接 SpringBoot是如何实现自动配置的?--SpringBoot源码(四) 温故而知新,我们来简单回顾一下上 ...
- Spring Boot -- 外部配置的属性使用
Spring Boot允许使用propertities文件.yaml文件或者命令行参数作为外部配置. 命令行参数配置 Spring Boot可以基于jar包运行,打成jar包的程序可以直接通过下面的命 ...
- Spring Cloud Feign 在调用接口类上,配置熔断 fallback后,输出异常
Spring Cloud Feign 在调用接口类上,配置熔断 fallback后,出现请求异常时,会进入熔断处理,但是不会抛出异常信息. 经过以下配置,可以抛出异常: 将原有ErrorEncoder ...
- Spring Boot 注入外部配置到应用内部
Spring Boot允许你外部化你的配置,这样你就可以在不同的环境中使用相同的应用程序代码,你可以使用properties文件.YAML文件.环境变量和命令行参数来外部化配置,属性值可以通过使用@V ...
随机推荐
- 【WPF学习】第六十六章 支持可视化状态
上一章介绍的ColorPicker控件,是控件设计的最好示例.因为其行为和可视化外观是精心分离的,所以其他设计人员可开发动态改变其外观的新模板. ColorPicker控件如此简单的一个原因是不涉及状 ...
- CF632(div.2)C. Eugene and an array
https://codeforces.ml/contest/1333/problem/C 大概题意是规定和为0的数组为不合格数组,询问给定数组中共有多少个合格子数组. 解题 子数组的数量 一个长度为 ...
- DG磁盘分区提示错误
文章更新于:2020-02-26 一.当你使用 DG 分区遇到错误时 1.错误复现 2.解决办法 以管理员身份打开cmd 运行 chkdsk /f /x g:(这里的 g 替换成你要检查的盘符) 然后 ...
- Jdk 和 jre 的 关系和区别
Jdk 和 jre 的 关系和区别 区别: JDK:是Java Development Kit 的简称–>翻译过来就是:Java 开发工具包.是程序员使用java语言编写java程序所需的开发工 ...
- ThinkPHP5中raw的作用
在tp5中,我们一般在模板中输出变量是这样的:{$test} 但是有时候在有些源码中我们可以看到这样的方式:{$test|raw} 这个时候如果你去找手册会发现,全文基本没有提到这个raw的作用. 那 ...
- 10.2 io流 之字节流和字符流
FileWriter 用于写入字符流.要写入原始字节流,请考虑使用 FileOutputStream. io流相关文档: https://www.cnblogs.com/albertrui/p/836 ...
- 史上最详细mac安装Qt教程
史上最详细mac安装Qt教程,小白看过来! 这是一篇非常适合Qt入门小白的的安装Qt教程,因为这学期我们小组的一个关于高速救援的项目要用到Qt与web进行交互式展现相关的图像,由于没有MSVC这个插件 ...
- 3分钟掌握Quartz.net分布式定时任务的姿势
引言 长话短说,今天聊一聊分布式定时任务,我的流水账笔记: ASP.NET Core+Quartz.Net实现web定时任务 AspNetCore结合Redis实践消息队列 细心朋友稍一分析,就知道还 ...
- Win8.1/Win10在某些程序输入中文变成问号的解决方法
之前我是使用Win8.1,在某些软件上输入中文,却显示问号,换输入法也没用,当时也没用太在意,后来升级到Win10还是一样.同样的软件在其它Win8.1/Win10电脑却可以正常显示中文. 解决方法如 ...
- redis集群搭建-3.0/4.0版本
1. Redis的安装 1.1. Redis的安装 Redis是c语言开发的. 安装redis需要c语言的编译环境.如果没有gcc需要在线安装.yum install gcc-c++ 安装步骤: 第 ...