Mybatis不同包里出现了同名类,启动时报错如下:
org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'sysSetMapper' for bean class [com.onlyas.appserver.dao.SysSetMapper] conflicts with existing, non-compatible bean definition of same name and class [com.onlyas.appserver.dao.dbo.SysSetMapper]
通过查看Spring源码得知,当我们使用注解创建bean时,spring使用了AnnotationBeanNameGenerator来创建bean的名称。
解决方案:自己写一个Generator吧。如下:
@SpringBootApplication
@ComponentScan(nameGenerator = MultidataApplication.SpringBeanNameGenerator.class)
public class MultidataApplication {
public static class SpringBeanNameGenerator extends AnnotationBeanNameGenerator {
@Override
protected String buildDefaultBeanName(BeanDefinition definition) {
return definition.getBeanClassName();
}
}
public static void main(String[] args) {
SpringApplication.run(MultidataApplication.class, args);
}
}
Mybatis的MapperScan里新增:
nameGenerator = MultidataApplication.SpringBeanNameGenerator.class
示例:
@MapperScan(basePackages="com.onlyas.appserver.dao", nameGenerator = MultidataApplication.SpringBeanNameGenerator.class)
好了,这样再运行起来就不会报错了。