0

I'm working on Module3 which is dependent on Module1 and Module2.

Module1 has the following classes:

class Module1Service {
    @Autowired
    MyBean myBean;
    .....
}

@Component("module1MyBean")
class MyBeanImpl implements MyBean{

}

Module2 has the following classes:

class Module2Service {
    @Autowired
    MyBean myBean;
    .....
}

@Component("module2MyBean")
class MyBeanImpl implements MyBean{

}

Unfortunately, both Module1Service and Module2Service do not contain @Resource or @Qualifier. Because of this, I get NonUniqueBeanDefinition exception when I try to bootstrap Module3.

Is there any way I can avoid this exception without making any changes to Module1 or Module2?

saravana_pc
  • 2,607
  • 11
  • 42
  • 66
  • You could exclude them from classpath scanning and create a `@Bean` in a `@Configuration` class. – Steve Feb 28 '15 at 13:43

1 Answers1

1

are you dependend on Module1Service or Module2Service, if not , you may use the context:exclude-filter discuss here .

Community
  • 1
  • 1
hongshuwei
  • 652
  • 2
  • 7
  • 17
  • that's a nice option, unfortunately I need both the services. – saravana_pc Feb 28 '15 at 10:14
  • 1
    You could use an exclude-filter to exclude `Module1Service` and `Module2Service` from component scanning and manually redefine them via xml of java config. This way you can manually inject the proper instance of `MyBean` into the services. I don't think there are many other options if you can't modify the source code of module 1 & 2 ... – Pieter Feb 28 '15 at 10:34
  • I think that's the only option. Do I need to exclude the services if we're anyway defining them in xml? Doesn't the xml config override annotation config? thanks – saravana_pc Feb 28 '15 at 11:13
  • If you're overriding the bean definitions via xml config then its not necessary to exclude them from component scanning. – Pieter Feb 28 '15 at 13:12