Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

513
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda