0

I am trying to configure 2 different datasources in my application as it is required.

My configuration in the AppContext.xml is as follows:

<bean id="dataSourceA" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
     <property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver"/>  
     <property name="url" value="datasourceAURL"/>  
     <property name="username" value="aaaa"/>  
     <property name="password" value="pppp"/>    
</bean>
      <bean id="dataSourceB" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
     <property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver"/>  
     <property name="url" value="datasourceBURL"/>  
     <property name="username" value="bbbb"/>  
     <property name="password" value="cccc"/>    
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
           <property name="basePackage" value="com/ex/myBatis/mappings" />  
      </bean> 

      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
     <property name="dataSource" ref="dataSourceA" />  
     <property name="typeAliasesPackage" value="com.ex.myBatis.entities"/>  
</bean>
      <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">  
     <constructor-arg index="0" ref="sqlSessionFactory" />  
      </bean>

      <bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean">  
     <property name="dataSource" ref="configDataSource" />  
     <property name="typeAliasesPackage" value="com.ex.myBatis.entities"/>  
      </bean>

     <bean id="sqlSession1" class="org.mybatis.spring.SqlSessionTemplate">  
    <constructor-arg index="0" ref="sqlSessionFactory1" />  
     </bean>

     <bean id="callService" class="com.ex.myBatis.Service.callService">
    <property name="sqlSession" ref="sqlSession1" />
     </bean> 

But while accessing the bean I am getting the below Exception

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'callService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public final void org.mybatis.spring.support.SqlSessionDaoSupport.setSqlSessionTemplate(org.mybatis.spring.SqlSessionTemplate); nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.mybatis.spring.SqlSessionTemplate] is defined: expected single matching bean but found 2: sqlSession,sqlSession1

Please somebody help me in figuring out the issue. Also please suggest me if there is someother way to configure 2 Datasources.

1
  • Can somebody help me out in fixing this issue?? Commented Jun 12, 2014 at 7:10

1 Answer 1

1

Your configuration is mostly correct. The problem you face is that you use autowiring to inject one of callService dependencies.

Seems that you use SqlSessionDaoSupport and its sqlSessionTemplate field is autowired. There are two templates defined so spring cannot automatically wire dependencies. You need specify correct template manually.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.