gpt4 book ai didi

java - 使用数据源代理时创建了多个数据源 bean

转载 作者:行者123 更新时间:2023-11-30 10:53:10 25 4
gpt4 key购买 nike

我正在尝试使用来自 Here 的当前应用程序实现数据源代理

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

Listener 和 filter 已按照文档正确配置。

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]

请帮忙。

最佳答案

我终于从 here 找到了这个问题的解决方案

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

我们可以在 java 中的主 bean 上指定 @Primary 注释

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

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

关于java - 使用数据源代理时创建了多个数据源 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34121002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com