PropertyPlaceholderConfigurer,用于将properties文件中定义的属性替换到bean定义的property占位符。其类图如下:
从上图中,我们看到PropertyPlaceholderConfigurer实现了三个bean生命周期的接口:BeanFactoryAware & BeanNameAware & BeanFactoryPostProcessor。
PropertyResourceConfigurer.postProcessBeanFactory()将properties文件中的属性进行merge,convert,最后调用PropertyPlaceholderConfigurer.processProperties()完成遍历bean definition替换属性占位符。
ps:使用eclipse新建properties文件默认编码为ISO-8859-1,不支持中文。如何才能支持中文呢?xml?说对了,
使用xml定义属性, http://java.sun.com/dtd/properties.dtd
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment></comment> <entry key="firstname"><![CDATA[猪]]></entry> <entry key="lastname"><![CDATA[小]]></entry> </properties>