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)