Spring 加载和使用properties配置文件


1.让Spring加载管理properties文件

基于注解的方式

可以使用Environment来获取配置信息,也可以参考第二点中的使用方式.
PropertySource注解的value是String[],支持配置多个properties文件.

@Configuration
@PropertySource("classpath:system.properties")
public class ApplicationContextConf {

    @Autowired
    private Environment environment;

    @Bean
    public DirConf dirConf() {
        DirConf dirConf = new DirConf();
        dirConf.addDirMap("yveshe.tmpfile.dir", environment.getProperty("yveshe.tmpfile.dir", ""));// 没有配置事,默认值为空串
        dirConf.addDirMap("yveshe.log.dir", environment.getProperty("yveshe.log.dir", ""));// 没有配置事,默认值为空串
        dirConf.addDirMap("yveshe.message.dir", environment.getProperty("yveshe.message.dir", ""));// 没有配置事,默认值为空串
        return dirConf;
    }
}

基于XML配置的方式(新的配置方式和旧的配置方式)

方式一: 使用context:property-placeholder标签配置
context:property-placeholder的localtioin属性可以配置多个文件,用,进行分隔既可.

<?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:c="http://www.springframework.org/schema/c"
	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-4.0.xsd">

	<context:property-placeholder location="classpath:system.properties" />
	<bean class="com.yveshe.runtimeinject.usage3.bean.DirConf">
		<property name="dirMap">
			<map>
				<entry key="yveshe.tmpfile.dir" value="${yveshe.tmpfile.dir}"></entry>
				<entry key="yveshe.message.dir" value="${yveshe.message.dir}"></entry>
				<entry key="yveshe.log.dir" value="${yveshe.log.dir}"></entry>
			</map>
		</property>
	</bean>

</beans>

方式二: 使用配置BeanPropertyPlaceholderConfigurer

<bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="locations">
            <list>
                <value>classpath:system.properties</value>
  <value>classpath:system2.properties</value>
            </list>
        </property>
</bean>

2.在Spring中使用properties文件内容

  • 在xml文件中可以用 ${属性key}获取value;
  • 在spring管理的javabean中也可以获取-> @Value(“属性key”) String xx;
  • 还统一通过Environment 直接调用API获取

xml中使用例子

	<bean class="com.yveshe.runtimeinject.usage3.bean.DirConf">
		<property name="dirMap">
			<map>
				<entry key="yveshe.tmpfile.dir" value="${yveshe.tmpfile.dir}"></entry>
				<entry key="yveshe.message.dir" value="${yveshe.message.dir}"></entry>
				<entry key="yveshe.log.dir" value="${yveshe.log.dir}"></entry>
			</map>
		</property>
	</bean>

注解使用例子:

@Value("${yveshe.tmpfile.dir}")
private String tmpFileDir = "/tmp";
@Value("${yveshe.message.dir}")
private int messageDir = "/tmp";

3.相关配置说明

这里以<context:property-placeholder/>方式的属性进行说明,其他方式都有等价实现

location:表示属性文件位置,多个之间通过如逗号/分号等分隔;
file-encoding:文件编码;
ignore-resource-not-found:如果属性文件找不到,是否忽略,默认false,即不忽略,找不到将抛出异常
ignore-unresolvable:是否忽略解析不到的属性,如果不忽略,找不到将抛出异常
properties-ref:本地java.util.Properties配置
local-override:是否本地覆盖模式,即如果true,那么properties-ref的属性将覆盖location加载的属性
system-properties-mode:系统属性模式,ENVIRONMENT(默认),NEVER,OVERRIDE
ENVIRONMENT:将使用Spring 3.1提供的PropertySourcesPlaceholderConfigurer,其他情况使用Spring 3.1之前的PropertyPlaceholderConfigurer
OVERRIDE: PropertyPlaceholderConfigurer使用,因为在spring 3.1之前版本是没有Enviroment的,所以OVERRIDE是spring 3.1之前版本的Environment
NEVER:只查找properties-ref、location;
order:当配置多个时的查找顺序

使用注意:
1.location中的加载文件的顺序
如果location中有多个文件:
classpath:db.properties,classpath:default.properties,classpath:default3.properties,classpath:default2.properties
将依次加载,值得注意的是如果后一个文件中有和前面某一个文件中属性名是相同的,最终取的值是后加载的值
举例来说:
default.properties文件中有个属性名userId,其对应的值为-1
default2.properties文件中也有一个属性名userId,其对应的值为-2
default3.properties文件中特有一个属性名userId,其对于那个的值为-3

default.properties文件先加载,此时userId的值为-1,当default3.properties文件加载时将更新原来的值,此时userId的值为-3,同理,最后加载default2.properties文件,所以userId最终值为-2
所以需要避免不同属性文件中的属性名称重名



参考:
spring 配置加载properties属性文件(context:property-placeholder的使用)
https://blog.csdn.net/qiaziliping/article/details/77575262

老版本中org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的使用说明
https://bijian1013.iteye.com/blog/2256728

context:property-placeholder标签的使用说明
注意(Spring配置文件中只能存在一份)
https://blog.csdn.net/rickesy/article/details/50791534

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值