Spring引用properties文件

    Srping提供了PropertyPlaceholderConfigurer,它是一个容器后处理器,负责读取properties属性文件里的属性值,并将这些属性值设置成Spring配置文件的元数据。

如定义Properties类:

package com.custle.spring;

/**
 * Created by admin on 2018/3/2.
 */
public class Properties {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

在XML中为Properties类通过读取properties文件获取属性值:

<?xml version="1.0" encoding="UTF-8"?>
<beans 
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
                                   http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!--引用MyProperty.properties-->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location">
			<value>MyProperty.properties</value>
		</property>
	</bean>
	<bean id="testPro" class="com.custle.spring.Properties">
		<property name="name" value="${myProperty.name}"/>
		<property name="age" value="${myProperty.age}"/>
	</bean>
</beans>

MyProperty.properties配置文件:

myProperty.name = propertyName
myProperty.age = 13

测试程序:

package com.custle.spring.test;

import com.custle.spring.Properties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by admin on 2018/3/2.
 */
public class PropertyTest {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring-property.xml");
        Properties testPro = ac.getBean("testPro", Properties.class);
        System.out.println(testPro.getName() + "现在" + testPro.getAge() + " 了");
    }
}

控制台输出:

propertyName现在13 了

如果需要读取多个perperties文件的话,将配置文件改为如下方法:

<!--引用MyProperty.properties-->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>MyProperty.properties</value>
				<!--如果有多个配置文件继续列出-->
			</list>
		</property>
	</bean>

在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.xsd
							http://www.springframework.org/schema/context
							http://www.springframework.org/schema/context/spring-context.xsd">
    <!--引用MyProperty.properties文件-->
    <context:property-placeholder location="MyProperty.properties"/>
	<bean id="testPro" class="com.custle.spring.Properties">
		<property name="name" value="${myProperty.name}"/>
		<property name="age" value="${myProperty.age}"/>
	</bean>
	
</beans>

猜你喜欢

转载自my.oschina.net/u/3697923/blog/1627825
今日推荐