I tried to create 2 bins:

@Component class A{...} @Component class B extends A{...} 

but when autowire- ping error

No qualifying bean of type is found:

What should I do if I need to inherit one bin from another?

  • The inherited class is already annotated, because it is inherited from the appropriate one, isn't it? autowirings classes are not noted - arturk
  • @arturk did not understand the question - voipp

1 answer 1

And how should Spring understand in which case which bean to use? Or, give the bin a name and inject the concrete with @Qualifier :

 @Component("a") class A{...} @Component("b") class B extends A{...} @Autowired @Qualifier("b") A b; @Autowired @Qualifier("a") A a; A a1 = appContext.getBean("a"); A b1 = appContext.getBean("b"); 

or refuse @Autowired and configure dependencies manually via setters.

  • and if I want to get a bin out of context by the getBean method? there seems to be no qualifier to ask - voipp
  • to pull out via getBean() (although you can do without 99% of this), give the bin a name via @Component("a") and @Component("b") . - Nofate
  • Added an example in the answer. - Nofate