java - Spring - 与现有的、不兼容的同名和类的 bean 定义冲突

标签 java spring autowired spring-mybatis

具体的异常(exception)是:

Failed to instantiate [org.springframework.context.annotation.AnnotationConfigApplicationContext]: Constructor threw exception;
nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException:
Annotation-specified bean name 'myService' for bean class [my.package.ejb.MyService] conflicts with existing, non-compatible bean definition of same name and class [my.package.other.ejb.MyService]

那些 MyService 接口(interface)甚至没有注释,它们代表 EJB 2.0 无状态 bean。

我的注释配置如下。

@Configuration
@ComponentScan("my.package")
@MapperScan("my.package")
public class ApplicationConfiguration {
    @Bean
    public DataSource dataSource() {
        return new JndiDataSourceLookup().getDataSource("...");
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(final DataSource dataSource) {
        final SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        sqlSessionFactory.setConfigLocation(new ClassPathResource("..."));
        return sqlSessionFactory;
    }

    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager(final DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}

可能与@MapperScan(来自MyBatis)和@ComponentScan不兼容?

异常来 self 用于 Autowiring EJB 3.0 字段的 SpringBeanAutowiringInterceptor

最佳答案

MapperScannerConfigurer 的文档说:

This class supports filtering the mappers created by either specifying a marker interface or an annotation. The annotationClass property specifies an annotation to search for. The markerInterface property specifies a parent interface to search for. If both properties are specified, mappers are added for interfaces that match either criteria. By default, these two properties are null, so all interfaces in the given basePackage are added as mappers.

基本上,我将数千个接口(interface)映射为 bean。不酷!
伙计们,我的错。

关于java - Spring - 与现有的、不兼容的同名和类的 bean 定义冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50545300/

相关文章:

java - 无法在 Junit 测试中 Autowiring JpaRepository - Spring 启动应用程序

php - 使用 Symfony Autowiring 时是否可以排除多个目录?

java - 如何从命令行显示 Java keystore SecretKeyEntry

java - 单击按钮后显示一个面板,在同一帧中单击第二个按钮时显示另一个面板

java - 正则表达式在字符串中查找价格

Java 类型推断在编译时失败,但 Eclipse 编译并运行得很好

java - 使用 application.yml 中的属性从 spring data mongodb 注入(inject) @Document 中的集合名称

java - Spring Security 5.1 - 使用 WebClient 获取客户端凭证流的 token

java - 如何在 Spring MVC 中使用 JasperReports?

java - spring boot注释 Autowiring 的问题