Spring使用外部属性文件

在配置文件里配置bean时,有时需要在bean的配置里混入系统部署的细节信息(例如:文件路径,数据源配置信息等)而这些部署细节实际上需要和bean配置相分离,于是Spring提供了一个Property Placeholder Configurer的BeanFactory后置处理器,Spring2.5之后,可通过<context:property-placeholder>元素简化:

步骤:

1、<beans>中添加context schema定义

2、在配置文件中加入如下配置

<context:property-placeholder location="cn\yinghuo\model\person.properties"/> //路径

Spring还允许在属性文件中使用${prop name},以实现属性之间相互引用

 

代码实现如下:

person类

package cn.yinghuo.model;

public class Person {
    //定义属性
	private String name;
	private String age;
	private String address;
	
    //show方法
	public void show(){
		System.out.println(name);
		System.out.println(age);
		System.out.println(address);
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
}

外部配置文件person.properties

name=Howie
age=17
address=china

Main方法实现

package cn.yinghuo.action;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.yinghuo.model.Person;

public class MainAction {
	public static void main(String[] args) {
		 ApplicationContext context= new ClassPathXmlApplicationContext("ApplicationContext.xml");
		 Person person = (Person)context.getBean("person");
		 person.show();
	}
}

XML配置

在引入xmlns:context="http://www.springframework.org/schema/context"时,也要在xsi:schemaLocation中添加context的解析文件

http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd

如果只有xmlns:context="http://www.springframework.org/schema/context"没有添加context的解析文件则会出现通配符的匹配很全面,但无法找到元素的异常。还有一点需要注意spring-context-4.2.xsd中4.2是spring的版本,如果是其它版本要换其它版本号

<?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:p="http://www.springframework.org/schema/p"
    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-4.2.xsd"
    >
    
    //引入spring容器
 	<context:property-placeholder location="cn\yinghuo\model\person.properties"/>

    //$[]引用注入
	<bean id="person" class="cn.yinghuo.model.Person">
	    <property name="name" value="${name}" />
	    <property name="age" value="${age}" />
	    <property name="address" value="${address}" />
	</bean>
	
	
</beans>

结果就是person.properties定义的那些,properties属性中的名称不需要与person类中的属性名一致

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值