在配置datasource数据源时需要填写数据库连接信息,如用户名密码等

最原始的方法是直接明文填写

第二种方法是将关键信息抽取到jdbc.properties文件,然后使用PropertyPlaceholderConfigurer读取配置文件

第三种方法是动态加载,继承PropertyPlaceholderConfigure并扩展它的convertProperty方法,按照自定的加载方式返回每个key对应的value值

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);
    }
}

 

 然后配置