Spring两个数据源配置在容器启动出错,No unique bean of type [org.springframework.jdbc.core.JdbcTemplate] is defined:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">


<!--这里使用JEE容器,所以用SpringJta来管理 <tx:jta-transaction-manager /> -->
<context:property-placeholder location="classpath:apiStar.properties" />

<bean id="dataSourceApiStar" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${mailbill.url}" />
<property name="username" value="${mailbill.user}" />
<property name="password" value="${mailbill.password}" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
<property name="minIdle" value="1" />
<property name="maxWait" value="600000" />       <!-- 一个查询10分钟内没有返回,自动放弃 -->  
<property name="validationQuery" value="SELECT 1" />   <!-- 数据库连接可用性测试语句 -->  
        <property name="testOnBorrow" value="true" />          <!-- 每次获取一个连接的时候,验证一下连接是否可用,语句在validationQuery里面 -->  
        <property name="removeAbandoned" value="true" />       <!-- 自动处理连接未关闭的问题,Setting this to true can recover db connections from poorly written applications which fail to close a connection.  -->  
        <property name="removeAbandonedTimeout" value="300" /> <!-- 连接使用后5分钟未关闭,则抛弃 -->  
        
</bean>

<bean id="apiStarJdbcTemplate" name="mailBillJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" autowire="byName">
         <constructor-arg index="0" ref="dataSourceApiStar"></constructor-arg>
    </bean>


</beans>
最近需要做一些web的东西,包括spring的使用,在过程中配置连个数据源的时候,在web项目发布的时候遇到这样的问题;
No unique bean of type [org.springframework.jdbc.core.JdbcTemplate] is defined: expected single match.....


Spring 配置如下,第一个:


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">




<!--这里使用JEE容器,所以用SpringJta来管理 <tx:jta-transaction-manager /> -->
<context:property-placeholder location="classpath:test1.properties" />

<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${test1.url}" />
<property name="username" value="${test1.user}" />
<property name="password" value="${test2.password}" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
<property name="minIdle" value="1" />
<property name="maxWait" value="600000" />       <!-- 一个查询10分钟内没有返回,自动放弃 -->  
<property name="validationQuery" value="SELECT 1" />   <!-- 数据库连接可用性测试语句 -->  
        <property name="testOnBorrow" value="true" />          <!-- 每次获取一个连接的时候,验证一下连接是否可用,语句在validationQuery里面 -->  
        <property name="removeAbandoned" value="true" />       <!-- 自动处理连接未关闭的问题,Setting this to true can recover db connections from poorly written applications which fail to close a connection.  -->  
        <property name="removeAbandonedTimeout" value="300" /> <!-- 连接使用后5分钟未关闭,则抛弃 -->  
</bean>

<bean id="test1JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" autowire="byName">
         <constructor-arg index="0" ref="dataSource1"></constructor-arg>
    </bean>


第二个:


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">




<!--这里使用JEE容器,所以用SpringJta来管理 <tx:jta-transaction-manager /> -->
<context:property-placeholder location="classpath:test2.properties" />

<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${test2.url}" />
<property name="username" value="${test2.user}" />
<property name="password" value="${test2.password}" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
<property name="minIdle" value="1" />
<property name="maxWait" value="600000" />       <!-- 一个查询10分钟内没有返回,自动放弃 -->  
<property name="validationQuery" value="SELECT 1" />   <!-- 数据库连接可用性测试语句 -->  
        <property name="testOnBorrow" value="true" />          <!-- 每次获取一个连接的时候,验证一下连接是否可用,语句在validationQuery里面 -->  
        <property name="removeAbandoned" value="true" />       <!-- 自动处理连接未关闭的问题,Setting this to true can recover db connections from poorly written applications which fail to close a connection.  -->  
        <property name="removeAbandonedTimeout" value="300" /> <!-- 连接使用后5分钟未关闭,则抛弃 -->  
</bean>

<bean id="test2JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" autowire="byName">
         <constructor-arg index="0" ref="dataSource2"></constructor-arg>
    </bean>






因为有多个数据库需要连接,所以在spring里面配置了多个数据数据源,然后提供给DAO去调用,在DAO里面定义:


@Autowired 
private JdbcTemplate test1JdbcTemplate;


@Autowired 
private JdbcTemplate test2JdbcTemplate;


这样的的。


然后在发布项目的时候发现总是 出现 No unique bean of type [org.springframework.jdbc.core.JdbcTemplate] is defined: expected single match..... 这样的错误。




在网上找了些资料,发现解决方法:  
@Autowired @Qualifier("test1JdbcTemplate")
private JdbcTemplate test1JdbcTemplate;


@Autowired @Qualifier("test2JdbcTemplate")
private JdbcTemplate test2JdbcTemplate;


其实就是每次web容器启动的时候都会去给 private JdbcTemplate test2JdbcTemplate赋值,但是由于现在容器里面有两个bean,他不能自动识别怎么样去赋值,所以需要手动的在dao里面自己赋值。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值