2016-08-01 61 views
0

我試着在谷歌上查找有關該問題,但無法獲得解決方案。Spring MethodInvokingBean - DriverManagerDataSource不接受返回的值

我想實現

請參見下面的代碼,我所要做的是通過一個加密的密碼MethodInvokingBean,它採用com.xxxxxxx.CryptoUtil使用靜態方法decrypt解密。

解密後的值通過<property name="password" ref="decryptedDBPassword" />注入masterDBDatasource,但不起作用。

<bean id="decryptedDBPassword" class="org.springframework.beans.factory.config.MethodInvokingBean"> 
     <property name="targetClass" value="com.xxxxxxx.CryptoUtil"/> 
     <property name="targetMethod" value="decrypt"/> 
     <property name="arguments" value="${encrypted.db.password}" /> 
    </bean> 

    <bean id="masterDBDatasource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="${db.driver.class}" /> 
     <property name="url" value="${db.url}" /> 
     <property name="username" value="${db.username}" /> 
     <property name="password" ref="decryptedDBPassword" /> 
    </bean> 

異常

org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.springframework.beans.factory.config.MethodInvokingBean' to required type 'java.lang.String' for property 'password'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.beans.factory.config.MethodInvokingBean] to required type [java.lang.String] for property 'password': no matching editors or conversion strategy found 

我也跟着下面的教程作爲參考

https://www.mkyong.com/spring/spring-methodinvokingfactorybean-example/

我也試過​​ 但DB連接是說 - 訪問被拒絕因無效的密碼。

請幫忙。

回答

0

屬性password應該是一個字符串值。您正在將一個字符串作爲密碼傳遞給DriverManagerDataSource的bean(decryptedPassword)引用。它應該像

<property name="password" value="${db.password} /> 

類似於您提供的username

由於您需要傳遞解密的密碼,因此您可能需要查看Spring的表達式支持,以便在傳遞密碼之前處理密碼。

http://docs.spring.io/spring/docs/3.0.0.M3/spring-framework-reference/html/ch07s04.html

1

發佈確切的回答我的問題代碼,爲其他人,誰可能面臨類似問題的參考。

從@馬特的提示,我有最後設置如下利用規劃環境地政司(不使用MethodInvokingBean

<bean id="masterDBDatasource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="${db.driver.class}" /> 
     <property name="url" value="${db.url}" /> 
     <property name="username" value="${db.username}" /> 
     <property name="password" value='#{T(com.xxxxxxx.CryptoUtil).decrypt("${encrypted.db.password}")}' /> 
</bean> 
相關問題