简体   繁体   中英

How to resolve this conflicting of two beans with same name in maven project?

Suppose i have a Maven Project A and Project B, and project B i am adding it as as a jar in Project A when i try to run Project A it gives me the error " bean name "xyz" conflicts with existing, non-compatible bean definition of same name and class "

So anyone has solution to this to ignore the bean "xyz" of Project B and run only "xyz" bean of Project A or is there any annotation or something like that?

Use the @Primary annotation on the bean defenition on the configuration class

@Bean(name = "beanOnA")
@Primary
public YourInterface yourBeanOnProjectA(){
    return new YourClass("Bean on Project A");
}

While autowiring the bean specify the bean name using qualifier annotation.

@Autowired
@Qualifier("beanOnA")
YourInterface yourInterface;

You can use Qualifier annotation to achieve this.

@Qualifier("ProjectA")

or

@Qualifier("complete.package.ProjectA")

例如 Use a Qualifier Annotation in your class file. eg -

@Autowired @Qualifier("ProjectA")

You can edit pom file for B to exclude the class file while packing enter link description here

Edit pom file A using shade plugin to exclude the class file from B, enter link description here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM