ssm realizes dynamic loading of database key information

When configuring the datasource data source, you need to fill in the database connection information, such as username and password, etc.

The most primitive method is to fill in the plaintext directly

The second method is to extract the key information to the jdbc.properties file, and then use the PropertyPlaceholderConfigurer to read the configuration file

The third method is dynamic loading, inheriting PropertyPlaceholderConfigure and extending its convertProperty method, and returning the value corresponding to each key according to the custom loading method

public class MyPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    Map<String,String> map=new HashMap<>();

    public MyPropertyPlaceholderConfigurer(){
        map.put("username","123");
        map.put("password","123");
        map.put("url","jdbc:mysql://127.0.0.1:3306/home");
    }
    @Override
    protected String convertProperty(String propertyName, String propertyValue) {
        String temp=map.get(propertyName);
        if(temp!=null){
            return temp;
        }
        return super.convertProperty(propertyValue,propertyValue);
    }
}

 

 then configure

 

Guess you like

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