【问题标题】:spring Multi DataSource @Service annotation with existing error存在错误的spring Multi DataSource @Service注释
【发布时间】:2014-12-09 07:34:58
【问题描述】:

我的代码在 Spring 框架中有错误

错误原因我知道 所以我不知道如何解决这个问题。

我正在使用 mybatis 库

我有两个帐户数据库的多个数据源

我创建了一个 root-context.xml ------------------------root-context.xml ---------- -------------------------------------

Oracle 账户 1 测试1

<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 ------------------ ---------------------------------------

我没有使用上下文:组件扫描

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

我使用SpringJUnit4每个单元测试

依次为(DataSourceTest 和 SqlSessionFactory 测试和 SqlSession 测试 mapperScanTest)。

在 mapperScanTest 之前没有任何问题。

但是使用注解@Service时star会出错

------------------------------------------服务接口代码-- ----------------------------------

public interface Test2_SERVICE {

    public List<Test2_VO> GET_ListVO();

}

-------------------------------实现服务代码- ----------------------------------

@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();
    }

}

---------测试代码------------------------------

@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("---------------------");
        }


    }

错误信息-----------------------------------

原因: org.springframework.context.annotation.ConflictingBeanDefinitionException: bean 类的注解指定的 bean 名称“Test2_SERVICE” [test.service.server.test2.test2service.Test2_SERVICE] 与 相同名称和类的现有不兼容 bean 定义 [test.service.server.test2.test2service.Test2_SERVICEIMPLE]

----------------------------------- -结束---------------------------------------------

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

问题直到对象Test2_MAPPER才存在

但是,错误从 Test2_SERVICE 开始

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

因为这个问题

我痛苦了三天..

有人告诉我解决此错误消息的问题。

感谢您阅读我的文章。

【问题讨论】:

  • 那么你有两个同名的映射器?

标签: spring spring-mvc mybatis spring-annotations


【解决方案1】:

问题是你用这个注解为Test2_SERVICEIMPLE创建了一个名为“Test2_SERVICE”的bean:

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

然后将这个Test2_SERVICEIMPLE bean 准确分配给接口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;

这意味着接口想要使用 bean 来实现......

所以只需删除/更改@Qualifier("Test2_SERVICE") 或更改@Service("Test2_SERVICE") 的名称

如何autowire annotation qualifiers 以及如何name autodetected components

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-20
    • 2014-08-14
    • 1970-01-01
    • 2016-04-03
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 1970-01-01
    相关资源
    最近更新 更多