`
风过无声
  • 浏览: 87721 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

propeties配置文件

 
阅读更多

1.PropertyPlaceholderConfigurer & <context:property-placeholder />

--单个PropertyPlaceholderConfigurer 

ApplicationContext.xml

<context:property-placeholder 
location="classpath:test.properties,classpath:test1.properties"/>

test.properties

student.id=1000
student.name=siyuan

test1.properties

student.id=10001
student.name=siyuan1

注入(占位符${...})

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("stu")
public class Student {
	
	@Value("${student.id}")
	private int id;
	
	@Value("${student.name}")
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

Test.java

 

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.siyuan.test.spring.Student;


public class Test {

	public static void main(String[] args) {
		ApplicationContext ctxt = new ClassPathXmlApplicationContext("ApplicationContext.xml");
		Student stu = (Student) ctxt.getBean("stu");
		System.out.println(stu.getId() + ":" + stu.getName());
	}

}

输出结果

 

10001:siyuan1

结论:多个properties按先后顺序加载,重复则覆盖

--多个PropertyPlaceholderConfigurer

ApplicationContext.xml

 

    <context:property-placeholder location="classpath:test.properties"/>
    <context:property-placeholder location="classpath:test1.properties"/>

输出结果

 

1000:siyuan

结论:按先后顺序加载,重复则忽略,即先加载的优先级高

改变优先级

ApplicationContext.xml

 

    <context:property-placeholder location="classpath:test.properties" order="2"/>
    <context:property-placeholder location="classpath:test1.properties" order="1"/>

输出结果

 

10001:siyuan1

结论:可通过设置order属性改变优先级,优先级按order从小到大排序,即order小的优先级高

2.PropertiesFactoryBean & <util:properties/>

配置:

<util:properties id="studentProp" location="classpath:test.properties"/>

注入(SpEL):

@Value("#{studentProp['student.id']}")
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics