通过工厂bean重写spring数据源属性

9cbw7uwe  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(258)

我在我的spring应用程序上下文中这样配置db:

<bean id="dataSource" 
class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
    <property name="driverClassName" 
value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="${uri}"/>
    <property name="username" value="${user}"/>
    <property name="password" value="${password}"/>
</bean>

但是,我决定对密码进行编码。为了在xml文件中正确地读取它,我编写了一个自定义的:

public class CustomDataSource extends BasicDataSource {
    private String username;
    private String password;
    public DataSource createDataSource() {
   //         get the value and decrypt here and set
            BasicDataSource target = new BasicDataSource();
            target.setPassword()
        }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        System.out.println("setng user ");
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

更新的bean是:

<bean id = "customDatasource" class = "CustomDataSource" />
<bean id = "datasource" 
 factory-bean = "customDatasource" factory-method = "createDataSource">
 <property ... />
</bean>

但是我无法将xml文件中的值传递给自定义bean,知道我做错了什么吗?注意basicdatasource的类型为:org.apache.commons.dbcp2

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题