Spring-xml文件加载,环境配置过程
spring 读取xml文件 策略模式
返回用于将位置模式解析为资源实例的资源模式解析器。默认值是路径匹配资源模式解析器,支持Ant样式的位置模式。
可以在子类中重写,例如在web环境中,对于扩展的解析策略。
设置此应用程序上下文的配置文件位置
查看 resolvePath()
方法中的 getEnvironment()
可以配置的形式返回此应用程序上下文的环境,允许进一步自定义。
如果未指定,将通过 createEnvironment()初始化默认环境。
StandardEnvironment.java 具有默认具有两种环境配置
- 系统属性
- 系统环境变量
JVM系统属性 会覆盖 系统属性值
new StandardEnvironment()
父类AbstractEnvironment
构造器
public abstract class AbstractEnvironment implements ConfigurableEnvironment {
public AbstractEnvironment() {
this(new MutablePropertySources());
}
}
protected AbstractEnvironment(MutablePropertySources propertySources) {
this.propertySources = propertySources;
this.propertyResolver = createPropertyResolver(propertySources);
customizePropertySources(propertySources);
}
StandardEnvironment.java 实现父类 AbstractEnvironment#customizePropertySources方法,设置属性值
public class StandardEnvironment extends AbstractEnvironment {
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
propertySources.addLast(
new PropertiesPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties()));
propertySources.addLast(
new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment()));
}
读取配置AbstractEnvironment#getSystemEnvironment
AbstractEnvironment#getSystemProperties
路径Placeholder处理
修改测试代码
public class SimpleBeanSpring {
public static void main(String[] args) {
System.setProperty("spring", "classpath");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("${spring}:SimpleBean.xml");
SimpleBean bean = context.getBean(SimpleBean.class);
bean.send();
context.close();
}
resolveRequiredPlaceholders方法会将${}
中内容从环境变量中获取值进行替换
System.setProperty(“spring”, “classpath”);
ClassPathXmlApplicationContext context = new > ClassPathXmlApplicationContext(“${spring}:SimpleBean.xml”);
替换为
ClassPathXmlApplicationContext(“classpath:SimpleBean.xml”);
文件路径处理完毕后,再进行xml文件解析