Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

依赖报错 #68

Open
lvjing-tuzhi opened this issue Sep 5, 2023 · 1 comment
Open

依赖报错 #68

lvjing-tuzhi opened this issue Sep 5, 2023 · 1 comment

Comments

@lvjing-tuzhi
Copy link

您好,java在引入您这个依赖的时候报错:Annotation-specified bean name 'mapper' for bean class [com.thoughtworks.xstream.mapper.Mapper] conflicts with existing, non-compatible bean definition of same name and class [com.baomidou.mybatisplus.core.mapper.Mapper]

@binarywang
Copy link
Owner

你遇到的错误表明在Spring应用程序上下文中有两个名称为'mapper'的bean定义发生冲突,但它们属于不同的类。这通常是因为两个不同的组件被注册为同一个bean名称,导致了歧义。

以下是解决该冲突的方法:

  1. 重命名Bean:为其中一个冲突的bean指定一个不同的名称,以避免命名冲突。

    • 对于com.thoughtworks.xstream.mapper.Mapper

      @Bean(name = "xstreamMapper")
      public Mapper xstreamMapper() {
          return new com.thoughtworks.xstream.mapper.Mapper();
      }
    • 对于com.baomidou.mybatisplus.core.mapper.Mapper

      @Bean(name = "mybatisMapper")
      public Mapper mybatisMapper() {
          return new com.baomidou.mybatisplus.core.mapper.Mapper();
      }
  2. 使用@Qualifier注解:在注入这些bean时,使用@Qualifier注解来指定要注入的bean。

    @Autowired
    @Qualifier("xstreamMapper")
    private Mapper xstreamMapper;
    
    @Autowired
    @Qualifier("mybatisMapper")
    private Mapper mybatisMapper;
  3. 组件扫描:确保组件扫描不会无意中将两个类注册为相同的bean名称。可以使用@ComponentScan指定特定的包,或者排除一个类的组件扫描。

  4. 检查配置文件:验证你的Spring配置文件(XML或Java),确保没有重复的bean定义使用相同的名称。

  5. 检查依赖:确保这两个类都是你的应用程序需要的。如果其中一个不需要,可以考虑删除不必要的依赖。

通过实施上述方法之一或多个,你应该能够解决bean名称冲突,并使你的Spring应用程序正常启动。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants