天天看点

Spring 占位符解析器的作用 - PropertyPlaceholderHelper

在Spring的众多功能中,有一个帮助类PropertyPlaceholderHelper,专门负责解析路径中或者名字中含有占位符的字串,并负责填充上具体的值。

public String replacePlaceholders(String value, final Properties properties) {
		Assert.notNull(properties, "Argument 'properties' must not be null.");
		return replacePlaceholders(value, new PlaceholderResolver() {
			public String resolvePlaceholder(String placeholderName) {
				return properties.getProperty(placeholderName);
			}
		});
	}
           

例举一个例子:

public static void main(String[] args) throws Exception {
		Properties p = System.getProperties();
		p.setProperty("name", "be");
		ApplicationContext factory = new ClassPathXmlApplicationContext("classpath:${name}ans.xml");
		LoggingBean lb = (LoggingBean) factory.getBean("logging");
		lb.run();
		BeanDestried sd = (BeanDestried)factory.getBean("beanDestried");
		sd.run();
		//print();
	}
           

这样占位符就换成了 我们想要的内容。其实在真正的配置文件中,此种问题很普遍,但是归根结底,还是需要这个帮助类来处理。