`

spring PreferencesPlaceholderConfigurer对properties文件读取值

 
阅读更多
1.创建config.propertites文件

key=value


2.配置文件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中的值 id=setting -->
    <bean id="setting"
        class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:*.properties</value>
            </list>
        </property>
        <!-- 设置编码格式 -->
        <property name="fileEncoding" value="UTF-8"></property>
    </bean>
</beans>


3.java类中引用
    // id=setting中找key的值
    @Value("${key}")
    private String key;



分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics