Org. springframework. beans. factory. config. PropertyPlaceholderConfigurer, propertyconfigurer
Attribute values in the context (configuration file) can be placed in another standard java Properties file. Replace the value in the specified properties file with $ {key} in the XML file. In this case, you only need to modify the properties file instead of the xml configuration file.
From this, we can see that propertyplaceholderproceser implements three bean lifecycle interfaces: BeanFactoryAware & BeanNameAware & BeanFactoryPostProcessor. For more information about the lifecycle of spring bean, see http://blog.csdn.net/gjb724332682/article/details/46767463.
PropertyResourceConfigurer. postProcessBeanFactory () performs merge and convert on the properties file, and finally calls PropertyPlaceholderConfigurer. processProperties () to traverse bean definitions and replace attribute placeholders.
Example:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location"><span><bean id="econsoleDS" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"><property name="driverClass"><span>import java.util.Properties;import org.springframework.beans.BeansException;import org.springframework.beans.factory.BeanInitializationException;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;import com.xxx.util.AESUtils;public class DecryptPropertyPlaceholderConfigurer extendsPropertyPlaceholderConfigurer {private String key = "xxxxxx";@Overrideprotected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)throws BeansException {try {String driverClassName = props.getProperty("driverClassName");if (driverClassName != null) {props.setProperty("driverClassName",AESUtils.aesDecrypt(driverClassName, key));}String url = props.getProperty("url");if (url != null) {props.setProperty("url", AESUtils.aesDecrypt(url, key));}String username = props.getProperty("username");if (username != null) {props.setProperty("username",AESUtils.aesDecrypt(username, key));}String password = props.getProperty("password");if (password != null) {props.setProperty("password",AESUtils.aesDecrypt(password, key));}super.processProperties(beanFactory, props);} catch (Exception e) {e.printStackTrace();throw new BeanInitializationException(e.getMessage());}}}
You can override the processProperties method.