1.报错
org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'articlesRepository' for bean class [com.dragon.springboot3vue3.mapper.ArticlesRepository] conflicts with existing, non-compatible bean definition of same name and class [org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactoryBean]
2.报错原因
@MapperScan
是 MyBatis 的注解,扫描 Mapper 接口;
@EnableElasticsearchRepositories
是 Spring Data 的注解,扫描 Elasticsearch Repository;
当两者扫描同一个包路径时,会导致 Bean 定义冲突。
3.解决方案
3.1 (推荐)分离包路径,启动类添加 @MapperScan,@EnableElasticsearchRepositories
@SpringBootApplication
@MapperScan("com.dragon.springboot3vue3.mapper") // MyBatis Mapper
@EnableElasticsearchRepositories("com.dragon.springboot3vue3.es.repository") // ES Repository
public class Springboot3Vue3Application {public static void main(String[] args) {SpringApplication.run(Springboot3Vue3Application.class, args);}
}
调整目录结构
src/main/java/com/dragon/springboot3vue3/
├── mapper/ # MyBatis Mapper 接口
│
└── es/
├── repository/ # ES Repository 接口
└── model/ # ES 文档实体
3.2 启动类完全移除注解,让 Spring Boot 自动扫描。但是 MyBatis 的每个Mapper层必须添加 @Mapper 注解
@SpringBootApplication
// @MapperScan("com.dragon.springboot3vue3.mapper") // 删除 MyBatis MapperScan
// @EnableElasticsearchRepositories("com.dragon.springboot3vue3.es.repository") // 删除 ES Repository
public class Springboot3Vue3Application {public static void main(String[] args) {SpringApplication.run(Springboot3Vue3Application.class, args);}
}