Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

514
Views
Inherit @Component in Spring

I have a hierarchy of classes. I want mark them with @Component. I'm trying to mark only the parent class. I expect Spring will mean childs as components too. But it doesn't happen.

I tried to use custom @InheritedComponent annotation like described here. It doesn't work.

I wrote an unit test. It fails with: "No qualifying bean of type 'bean.inherit.component.Child' available". Spring version is 4.3.7.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Component
@Inherited
public @interface InheritedComponent {}

@InheritedComponent
class Parent { }

class Child extends Parent {
}

@Configuration
@ComponentScan(basePackages = "bean.inherit.component")
class Config {
}

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Config.class)
public class InheritComponentTest {

    @Autowired
    private Child child;

    @Test
    public void name() throws Exception {
        assertNotNull(child);
    }
}
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can use a ComponentScan.Filter for this.

@ComponentScan(basePackages = "bean.inherit.component", 
includeFilters = @ComponentScan.Filter(InheritedComponent.class))

This will allow Spring to autowire your Child, without you having to attach an annotation to Child directly.

about 4 years ago · Santiago Trujillo Report

0

To answer the question posed under the accepted answers "How do do in XML" This is taken from Spring in Action with a trivial example by me.

Annotate the base class with component, add a qualifier

@Component
@InstrumentQualifier("TUBA")
public class Instrument {}

Qualify the subclass - NOTE- No need for @component

@InstrumentQualifier("GUITAR")
public class Guitar extends Instrument {}

Inject it like so

@Autowired
@InstrumentQualifier("GUITAR")
public void setInstrument(Instrument instrument) {this.instrument =instrument;}

In context.xml, add an include filter for assignable type

<context:component-scan base-package="some.pkg">
<context:include-filter type="assignable" expression="some.pkg.Instrument"/>
</context:component-scan>

The type and expression work as define the strategy here. You could also use the type as "annotation" and the expression would be the path to where the custom qualifier is defined to achieve the same goal.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!