Spring Properties使用的几种方式

2019-06-06  本文已影响0人  银河舰长

1.PropertyPlaceholderConfigurer类

org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

<!-- 在xml中的使用方式 -->
<property name="***" value="${***}"/>
/** 在java类中的使用方式 **/
@Value("${***}")
private String ***;
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:resource1.properties</value>
            <value>classpath:config/resource2.properties</value>
            <value>classpath*:resource3.properties</value>
        </list>
    </property>
</bean>

2. context:property-placeholder标签

<context:property-placeholder location="classpath:***"/>

<beans xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath*:config/*.properties,classpath*:other.properties"/>

3.PropertiesFactoryBean类

org.springframework.beans.factory.config.PropertiesFactoryBean

<!-- id是必须声明的,注入键值时需要使用 -->
<bean id="proertiesHolder" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <list>
            <value>classpath:resource1.properties</value>
            <value>classpath:config/resource2.properties</value>
            <value>classpath*:resource3.properties</value>
        </list>
    </property>
</bean>
<!-- 在xml中的使用方式 -->
<property name="***" value="#{proertiesHolder['***']}"/>
/** 在java类中的使用方式 **/
@Value("#{proertiesHolder['***']}")
private String ***;
上一篇 下一篇

猜你喜欢

热点阅读