Org. springframework. beans. factory. config. PropertyPlaceholderConfigurer, propertyconfigurer

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.