2015-12-06 28 views
2

我試圖從Here多個數據源的咖啡豆,而使用數據源代理

實現數據源代理我目前的應用程序,我配置數據源對象如春XML,即通過dataSourceReal在dataSourceProxy對象創建的。

監聽器和過濾器已正確配置,如文檔中所示。

的Spring XML文件:用於JNDI名稱

<bean id="dataSourceReal" class="org.springframework.jndi.JndiObjectFactoryBean"> 
     <property name="jndiName" value="jdbc/myDS" /> 
     <property name="resourceRef" value="true" /> 
     <property name="lookupOnStartup" value="false" /> 
     <property name="proxyInterface" value="javax.sql.DataSource" /> 
    </bean> 


    <bean id="dataSourceProxy" class="net.ttddyy.dsproxy.support.ProxyDataSource"> 
      <property name="dataSource" ref="dataSourceReal" /> 
      <property name="listener" ref="listeners" /> 
     </bean> 

     <bean id="listeners" class="net.ttddyy.dsproxy.listener.ChainListener"> 
      <property name="listeners"> 
       <list> 
        <bean 
         class="com.my.sql.logging.DataSourceQueryLoggingListener" /> 
       </list> 
      </property> 
     </bean> 

XML文件中的數據源名稱:

<Resource name="jdbc/myDS" auth="Container" 
type="javax.sql.DataSource" 
maxActive="25" maxIdle="5" maxWait="10000" 
username="abc" password="abc" 
driverClassName="oracle.jdbc.driver.OracleDriver" 
url="jdbc:oracle:thin:@localhost:1521:myDB" 
validationQuery="Select 1 from dual" /> 

我收到此錯誤:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: [dataSourceReal, dataSourceProxy] 

請幫助。

+0

你在你的代碼中使用@Autowired anotation嗎? –

+0

我沒有使用此配置的@Autowired註釋。 –

回答

1

我終於找到了解決這個問題,從here

每當我們配置了與我們的應用不止一個數據源,

我們可以在java中我們的主Bean指定@Primary註釋

OR

使用XML時,我們可以

指定我們的主Bean