user3689808
user3689808

Reputation: 77

spring Multi DataSource @Service annotation with existing error

I have a code that is an error in the Spring Framework

Error Cause I know So I do not know how to solve it the question.

I am being used with mybatis library

I had multi DataSource of two Account DataBase

I created a root-context.xml -----------------------root-context.xml -----------------------------------------------------------

Oracle Account 1 Test1

<bean id="dataSourceTest1" class="org.apache.commons.dbcp.BasicDataSource" destroy-m

ethod="close">
      <property name="driverClassName" value="net.sf.log4jdbc.DriverSpy"/>
      <property name="url" value="jdbc:log4jdbc:oracle:thin:@111.111.1111.1111:1111:Test1"/>
      <property name="username" value="TEST1"/>
      <property name="password" value="TEST1"/>
      <property name="maxIdle" value="200"/>
      <property name="maxActive" value="200"/>
      <property name="minIdle" value="5"/>
    </bean>

    <bean id="sqlSessionFactoryTest1" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSourceTest1" />
      <property name="mapperLocations" value="classpath*:test/service/server/test1/**/*.xml" />
    </bean>


     <bean id="sqlSessionTest1" class="org.mybatis.spring.SqlSessionTemplate" name="sqlSessionTest1">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryTest1" />
    </bean>

    <mybatis-spring:scan base-package="test.service.server.test1" template-ref="sqlSessionTest1" />

Oracle Account test2

<bean id="dataSourceTest2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      <property name="driverClassName" value="net.sf.log4jdbc.DriverSpy"/>
      <property name="url" value="jdbc:log4jdbc:oracle:thin:@222.222.2222.222:2222:Test2"/>
      <property name="username" value="Test2"/>
      <property name="password" value="Test2"/>
      <property name="maxIdle" value="200"/>
      <property name="maxActive" value="200"/>
      <property name="minIdle" value="5"/>
    </bean>


    <bean id="sqlSessionFactoryTest2" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSourceTest2" />
      <property name="mapperLocations" value="classpath*:test/service/server/test2/**/*.xml" />
    </bean>


     <bean id="sqlSessionTest2" class="org.mybatis.spring.SqlSessionTemplate" name="sqlSessionTest2">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryTest2" />
    </bean>


    <mybatis-spring:scan base-package="test.service.server.test2" template-ref="sqlSessionTest2"/>

-----------------------root-context.xml END ---------------------------------------------------------

i am not used context:component-scan

<!-- <context:component-scan base-package="test.service.server.test1.test1service"/>-->
<!-- <context:component-scan base-package="test.service.server.test2.test2service"/>-->

I use the SpringJUnit4 each unit test

Were sequentially to (DataSourceTest and SqlSessionFactory Test and SqlSession Test mapperScanTest).

did not have any problems until mapperScanTest.

However, an error occurs when star using the annotation @ Service

------------------------------------------service interface code ------------------------------------

public interface Test2_SERVICE {

    public List<Test2_VO> GET_ListVO();

}

-------------------------------------------implement service code----------------------------------

@Service("Test2_SERVICE") *//<--Error annotaion*
public class Test2_SERVICEIMPLE implements Test2_SERVICE{

    @Resource
    Test2_MAPPER mapper;

    @Override
    public List<Test2_VO> GET_ListVO() {
        return mapper.GET_ListMapperVO();
    }

}

---------test Code------------------------------

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/**/*-context.xml" })
    public class TestService {

    Logger logger = Logger.getLogger(Thread.currentThread().getClass());    

        @Autowired
        @Qualifier("Test2_SERVICE")
        Test2_SERVICE test2_SERVICE;



        @Override
        public void testString() {


            logger.info("---------------------");
            List<Test2_VO> listVO = test2_SERVICE.GET_ListVO();
            logger.info(listVO );
            logger.info("---------------------");
        }


    }

Error Messages-----------------------------------

Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'Test2_SERVICE' for bean class [test.service.server.test2.test2service.Test2_SERVICE] conflicts with existing, non-compatible bean definition of same name and class [test.service.server.test2.test2service.Test2_SERVICEIMPLE]

------------------------------------------------end---------------------------------------------

@Service("Test2_SERVICE") *//<--Error annotaion*

The problem did not exist until the object Test2_MAPPER

However, the error begins in Test2_SERVICE

@ Service ("Test2_SERVICE") Where there is only used here (Test2_SERVICEIMPLE).

Because of this problem

I am suffering for three days..

Somebody tell me solve the problem for this error message.

Thank you for reading my article.

Upvotes: 0

Views: 844

Answers (1)

Tobika
Tobika

Reputation: 1062

The problem is that you create a bean with the name "Test2_SERVICE" for Test2_SERVICEIMPLE with this annotation:

@Service("Test2_SERVICE")
//creates the bean with id="TEST2_Service" of the type Test2_SERVICEIMPLE
public class Test2_SERVICEIMPLE implements Test2_SERVICE

and then assign exactly this Test2_SERVICEIMPLE bean to the interface Test2Service

@Autowired
@Qualifier("Test2_SERVICE")
//here you assign the bean of the not matching type Test2_SERVICEIMPLE to 
//a variable of type Test2_SERVICE
Test2_SERVICE test2_SERVICE;

That means that the interface wants to use the bean for the implementation...

So just remove/change the @Qualifier("Test2_SERVICE") or change the name of the @Service("Test2_SERVICE")

How to autowire annotation qualifiers and how to name autodetected components

Upvotes: 1

Related Questions