Why you should understand the bean life cycle

Why you should understand the bean life cycle

 

Many friends will skip the chapter on the life cycle when learning spring. This is actually a huge waste of wealth. 

 

Take beanPostProcessor as an example:

 

 

background

The company has an online data source encryption package. This package is based on the original open source package. The source code is modified and the plaintext password is set to ciphertext. Over time, this package is not maintained, and the configuration has caused various problems. A lot of project time Wasted on configuring this data source. I had this problem when building a new project.

 

 

question

I don't want to use this old data source with modified source code, so the problem is: 

The open source data source package only supports the configuration of plaintext passwords. My requirement is that the password in the configuration file is ciphertext. After the bean of the dataSource is loaded, the ciphertext of the configuration file is dynamically decrypted into plaintext, and the setPassword is set to the DataSource. 

 

 

Then here comes the problem

How to get the password field of the bean, decrypt it, and reset it before the dataSource is called after the bean of the dataSource is initialized (the url, password, etc. in the configuration file are loaded into the bean)? 

 

 

If you understand the spring life cycle, you should be familiar with spring's BeanPostProcessor interface. There is a method in the class that implements this interface:

@Override  
    public Object postProcessAfterInitialization(Object bean, String name)  
            throws BeansException {  
          
          
        return bean;  
    }  

 

 

solution

During the initialization process of the spring container, after the bean is initialized, the method of the bean that implements the BeanPostProcessor interface and is configured to the IOC container will be called uniformly and cyclically. 

 

Then get the password of the DataSource in the method body, decrypt it, and set the plaintext password to meet the requirements at the beginning. 

 

Code:

public class DataSourcePasswordEncryptPostProcessor implements  
        BeanPostProcessor {  
  
    @Override  
    public Object postProcessAfterInitialization(Object bean, String name)  
            throws BeansException {  
        // TODO Auto-generated method stub  
          
        if(bean instanceof ProxoolDataSource){  
            ProxoolDataSource ds = (ProxoolDataSource)bean;  
            String password = ds.getPassword();  
            PBEStringEncryptor encryptor = new StandardPBEStringEncryptor();  
            encryptor.setPassword("xxxxxxx");  
            // plaintext password  
            String encPassword = encryptor.decrypt(password);  
            ds.setPassword(encPassword);  
        }  
          
        return bean;  
    }  
  
    @Override  
    public Object postProcessBeforeInitialization(Object bean, String name)  
            throws BeansException {  
        // TODO Auto-generated method stub  
        return bean;  
    }  
  
}  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326529166&siteId=291194637