Search code examples
javaspringautowiredspring-mybatis

Spring - conflicts with existing, non-compatible bean definition of same name and class


The specific exception is:

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]

Those MyService interfaces are not even annotated, they represent EJB 2.0 stateless beans.

My annotation configuration is as follow.

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

Might be an incompatibility with @MapperScan (from MyBatis) and @ComponentScan?

The exception come from the SpringBeanAutowiringInterceptor I'm using to Autowire EJB 3.0 fields.


Solution

  • The documentation for MapperScannerConfigurer says that:

    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.

    Basically I was mapping thousands of interfaces as beans. Not cool!
    Guys, my fault.