1、在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean 配置相分离
2、Spring 提供了一个 PropertyPlaceholderConfigurer 的 BeanFactory 后置处理器, 这个处理器允许用户将 Bean 配置的部分内容外移到属性文件中. 可以在 Bean 配置文件里使用形式为 ${var} 的变量, PropertyPlaceholderConfigurer 从属性文件里加载属性, 并使用这些属性来替换变量.
3、Spring 还允许在属性文件中使用 ${propName},以实现属性之间的相互引用。
4、文件
①TestProperties.java:测试类
②PerpertiesBean.java:properties数据模型
③properties.properties:测试用的properties
④properties2.properties:测试用的properties
⑤properties3.properties:测试用的properties
⑥propertiesContext.xml:配置文件
5、TestProperties.java
package com.demo.sshtest.properties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestProperties {
public static void main(String[] args) {
scope();
}
public static void scope(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("propertiesContext.xml");
PerpertiesBean perpertiesBean = (PerpertiesBean)applicationContext.getBean("propertiesbean");
System.out.println(perpertiesBean);
PerpertiesBean perpertiesBean2 = (PerpertiesBean)applicationContext.getBean("propertiesbean2");
System.out.println(perpertiesBean2);
PerpertiesBean perpertiesBean3 = (PerpertiesBean)applicationContext.getBean("propertiesbean3");
System.out.println(perpertiesBean3);
}
}
6、PerpertiesBean.java
package com.demo.sshtest.properties;
public class PerpertiesBean {
private Integer id;
private String name;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "PerpertiesBean [id=" + id + ", name=" + name + ", password=" + password + "]";
}
}
7、properties.properties
id=11
name=12306
password=r344r3
8、properties2.properties
id2=22
name2=444444
password2=ERRERERE
9、properties3.properties
id3=33
name3=77777
password3=iiuIK
10、propertiesContext.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
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.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<!--
导入属性文件
1、使用context:property-placeholder标签导入properties
2、context:property-placeholder中使用location属性来定位properties文件,其中"classpath:"表示文件位于类路径下
3、配置porperty的时候只需要用"${key}"的写法读取properties文件中对应属性名的值,为对应property的value值赋值就可以了
4、一个spring配置文件中,只能配置一个context:property-placeholder标签
5、如果需要加载多个properties文件,则需要用逗号隔开
6、配置相对路径时,可以使用"classpath:",配置绝对路径时,可以使用"file:"
7、如果加载的properties中的属性名重复,那么在使用${}读取时候,先读取的值会被后读取的同名的属性的值覆盖,(如:properties.properties和properties2.properties的属性都有"id"这个属性,则使用${}读取时,读取出来的结果为properties2.properties的"id"的值)
-->
<context:property-placeholder location="classpath:properties.properties,classpath:properties2.properties,file:E:/workspace/SSHnote_SpringBeanConfig1/src/properties3.properties"/>
<bean id="propertiesbean" class="com.demo.sshtest.properties.PerpertiesBean">
<property name="id" value="${id}"></property>
<property name="name" value="${name}"></property>
<property name="password" value="${password}"></property>
</bean>
<bean id="propertiesbean2" class="com.demo.sshtest.properties.PerpertiesBean">
<property name="id" value="${id2}"></property>
<property name="name" value="${name2}"></property>
<property name="password" value="${password2}"></property>
</bean>
<bean id="propertiesbean3" class="com.demo.sshtest.properties.PerpertiesBean">
<property name="id" value="${id3}"></property>
<property name="name" value="${name3}"></property>
<property name="password" value="${password3}"></property>
</bean>
</beans>
11、项目目录
12、demo
https://download.youkuaiyun.com/download/qq_22778717/10470345