【问题标题】:Multiple datasource beans are created while using datasource-proxy使用 datasource-proxy 时创建了多个数据源 bean
【发布时间】:2015-12-06 18:26:20
【问题描述】:

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

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

侦听器和过滤器已按照文档中的方式正确配置。

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]

请帮忙。

【问题讨论】:

  • 你在代码中使用@Autowired注解吗?
  • 我没有为这个配置使用@Autowired注解。

标签: java hibernate spring-mvc datasource


【解决方案1】:

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

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 2012-09-15
    • 1970-01-01
    • 2019-10-25
    • 2017-08-25
    • 2019-07-02
    • 2018-01-25
    相关资源
    最近更新 更多