spring-properties 介绍
- 在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们都可以把这些.properties文件类似的配置文件中,源代码中读取properties里面的值,这样后期只需要改动properties文件即可,不需要去寻找源代码进行修改,这样更加方便。在Spring中有两种加载properties文件的方式:基于xml方式和基于注解方式。
- spring管理属性配置文件properties
- org.springframework.beans.factory.config.PropertiesFactoryBean管理属性
- org.springframework.beans.factory.config.PropertyPlaceholderConfigurer也可以管理配置文件。
使用方法
1.新建一个Java project,命名settingConfig;
2 新建一个文件夹config 存放 .properties配置文件;
3.新建一个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.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:*.properties</value>
<value>/WEB-INF/config/settingConfig.properties</value>
</list>
</property>
<!-- 设置编码格式 -->
<property name="fileEncoding" value="UTF-8"></property>
</bean>
</beans>
4.创建settinConfig对象
5.使用@注解方式 读取.properties 值 为settinConfig对象赋值
package com.cdgk.flxh.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* setting.properties 的映射文件
*
/
@Component("settinConfig")
public class SettinConfig {
@Value("${namConfig.key}")
private String test_01;
@Value("${namConfig.key}")
private String test_02;
@Value("${namConfig.key}")
private String test_03;
public String getTest_01() {
return test_01;
}
public void setTest_01(String test_01) {
this.test_01= test_01;
}
public String getTest_01() {
return test_01;
}
public void setTest_02(String test_02) {
this.test_01= test_02;
}
public String getTest_02() {
return test_02;
}
public void setTest_03(String test_03) {
this.test_01= test_03;
}
public String getTest_03() {
return test_03;
}
}
5.也可以使用xml进行赋值
<bean id="settingConfig" class="${com.cdgk.flxh.config.SettingConfig}"> <!-- 这些配置Spring在启动时会去config.properties中找 -->
<property name="test" value="${key}" />
<property name="test" value="${key}" />
<property name="test" value="${key}" />
<property name="test" value="${key}" />
</bean>
- 总结
- 使用PropertyPlaceholderConfigurer管理属性文件获取属性的方式 @Value("${properties_key}")
- 使用PropertiesFactoryBean管理属性文件获取属性的方式 @Value("${bean_id['properties_key']}")
拓展
一、Spring中报"Could not resolve placeholder"的解决方案
除去properites文件路径错误、拼写错误外,出现”Could not resolve placeholder”很有可能是使用了多个PropertyPlaceholderConfigurer或者多个<context:property-placeholder>(<context:property-placeholder>底层也是PropertyPlaceholderConfigurer)的原因。
因为spring扫描注册PropertyPlaceholderConfigurer的bean被设置为单例模式,spring只会扫描一个PropertyPlaceholderConfigurer或者<context:property-placeholder>配置,其它多余将被忽略,所以才会出现替代属性值的占位符无法解析。
解决方案:
方案一:将多个PropertyPlaceholderConfigurer或者多个<context:property-placeholder>合并到一个里面,统一加载所有的资源文件
1.如下代码所示:
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:xxx.properties</value>
<value>classpath:jyyy.properties</value>
</list>
</property>
</bean>
或者:
<context:property-placeholder location="classpath:xxx.properties,classpath:yyy.properties"/>
方案二:坚持使用多个PropertyPlaceholderConfigurer或者多个<context:property-placeholder>,那么需要添加ignoreUnresolvablePlaceholders属性或者ignore-unresolvable属性,设置其值为true
1.如下代码所示:
<bean id="xxx" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="xxx.properties" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
<bean id="yyy" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="yyy.properties" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
或者:
<context:property-placeholder location="xxx.properties" ignore-unresolvable="true"/>
<context:property-placeholder location="yyy.properties" ignore-unresolvable="true"/>