Spring引用properties文件

    Srping提供了PropertyPlaceholderConfigurer,它是一個容器後處理器,負責讀取properties屬性文件裏的屬性值,並將這些屬性值設置成Spring配置文件的元數據。java

如定義Properties類:spring

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配置文件:this

myProperty.name = propertyName
myProperty.age = 13

測試程序:code

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() + " 了");
    }
}

控制檯輸出:xml

propertyName如今13 了

若是須要讀取多個perperties文件的話,將配置文件改成以下方法:get

<!--引用MyProperty.properties-->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>MyProperty.properties</value>
				<!--若是有多個配置文件繼續列出-->
			</list>
		</property>
	</bean>

在XML中的第二種配置方法:io

<?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>
相關文章
相關標籤/搜索