使用datasource-proxy

时间:2015-12-06 18:26:20

标签: java hibernate spring-mvc datasource

我尝试使用Here

中的当前应用程序实现数据源代理

我在spring xml中配置了数据源对象,即在dataSourceProxy对象中传递dataSourceReal。

在docs中正确配置了监听器和过滤器。

Spring xml文件:

<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> 

数据源名称的JNDI名称的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]

请帮忙。

1 个答案:

答案 0 :(得分:1)

我终于从here

找到了解决这个问题的方法

每当我们的应用程序配置了多个数据源时,

我们可以在java

中的主bean上指定@Primary注释

OR

使用xml时,我们可以将主bean指定为

<bean id="dataSourceProxy" primary="true" class="net.ttddyy.dsproxy.support.ProxyDataSource">