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的异常。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于spring配置文件加载properties文件,可以使用以下几种方式: 1. 使用PropertyPlaceholderConfigurer属性占位符 可以在配置文件使用${}占位符来引用属性值,同时需要在配置文件中引入对应的*.properties文件。在Spring容器启动时,会通过PropertyPlaceholderConfigurerproperties配置文件中的键值对装载到Spring的环境变量中,供${}占位符使用。 例如: ``` <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:/config/app.properties</value> </list> </property> </bean> <bean id="user" class="com.example.User"> <property name="name" value="${user.name}"/> <property name="age" value="${user.age}"/> </bean> ``` 在上面的例子中,将classpath:/config/app.properties中的键值对装载到Spring的环境变量中,供${}占位符使用。 2. 使用util命名空间的PropertiesFactoryBean 可以在Spring配置文件使用util命名空间的PropertiesFactoryBean来装载properties文件中的属性,并且使用${}占位符引用这些属性值。 例如: ``` <util:properties id="appConfig" location="classpath:/config/app.properties"/> <bean id="user" class="com.example.User"> <property name="name" value="${user.name}" /> <property name="age" value="${user.age}" /> </bean> ``` 在上面的例子中,通过util:properties装载classpath:/config/app.properties中的属性。在User bean中使用${}占位符引用属性值。 希望这些方法能够帮到您!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值