spring多数据源注入问题 100分求解

lixinbill 2015-09-17 03:03:41
我的配置文件:
<?xml version="1.0" encoding="GB2312"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="cls.hlw"/>
<bean class="cls.hlwp.jkpro.util.db.InitializeBean" lazy-init="false"/>

<bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
destroy-method="close" scope="singleton" p:minIdle="1" p:maxActive="20" p:initialSize="1"
p:maxWait="20000" p:username="hlwp_bidata" p:password="lhf0op" p:url="jdbc:oracle:thin:hlwp_bidata/lhf0op@192.168.17.90:1521:cnfc">
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="120" />
<property name="testWhileIdle" value="true" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="validationQuery" value="SELECT 1+1 from dual" />
<property name="connectionProperties" value="config.decrypt=false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="50" />
</bean>


<!-- 定义默认事务控制过程 -->

<tx:annotation-driven transaction-manager="tx" />

<bean id="tx" p:dataSource-ref="datasource" lazy-init="true"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager" />

<bean id="jdbcTemplate" p:resultsMapCaseInsensitive="true" lazy-init="default"
class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="datasource" />

<bean id="seqCookie" class="org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer"
p:incrementerName="seq_cookieid" p:dataSource-ref="datasource"/>





<bean id="datasource2" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
destroy-method="close" scope="singleton" p:minIdle="1" p:maxActive="20" p:initialSize="1"
p:maxWait="20000" p:username="HLWP_PVDATA" p:password="lhf0op" p:url="jdbc:oracle:thin:HLWP_PVDATA/lhf0op@192.168.17.90:1521:cnfc">
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="120" />
<property name="testWhileIdle" value="true" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="validationQuery" value="SELECT 1+1 from dual" />
<property name="connectionProperties" value="config.decrypt=false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="50" />
</bean>


<!-- 定义默认事务控制过程 -->

<tx:annotation-driven transaction-manager="tx2" />

<bean id="tx2" p:dataSource-ref="datasource2" lazy-init="true"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager" />

<bean id="jdbcTemplate" p:resultsMapCaseInsensitive="true" lazy-init="default"
class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="datasource2" />

<bean id="seqCookie2" class="org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer"
p:incrementerName="seq_cookieid" p:dataSource-ref="datasource2"/>

</beans>



dao:
@Repository
public class QdyhDao {

@Autowired @Qualifier("jdbcTemplate")
public JdbcTemplate jdbcTemplate;

@Autowired @Qualifier("jdbcTemplate2")
public JdbcTemplate jdbcTemplate2;








错误信息:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'qdyhDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public org.springframework.jdbc.core.JdbcTemplate cls.hlwp.jkpro.funcpoint.dxqd.qdyh.QdyhDao.jdbcTemplate2; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=jdbcTemplate2)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
.................
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public org.springframework.jdbc.core.JdbcTemplate cls.hlwp.jkpro.funcpoint.dxqd.qdyh.QdyhDao.jdbcTemplate2; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=jdbcTemplate2)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
... 44 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=jdbcTemplate2)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
... 46 more
...全文
284 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 3 楼 u010087908 的回复:
你定义了bean "jdbcTemplate" 但是并没有定义 bean "jdbcTemplate2"
娜娜很厉害 楼主看看这个
NANU-NANA 2015-09-21
  • 打赏
  • 举报
回复
你定义了bean "jdbcTemplate" 但是并没有定义 bean "jdbcTemplate2"
编号792458 2015-09-17
  • 打赏
  • 举报
回复
你有两个jdbcTemplate,产生二义性
lixinbill 2015-09-17
  • 打赏
  • 举报
回复
大神们 站出来啊

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧