stanlick
stanlick

Reputation: 1462

@Resource annotation not injecting a subclass

The @Resource annotation for a service is not injecting a subclass unless I explicitly include the @Component annotation in the subclass. Is there a way to request a subclass be "wired" with the parent @Resource without requiring annotations in each child class? The way I discovered this was by creating a subclass and failing to include the @Component annotation. I was quickly faced with the dreaded NPE.

Upvotes: 1

Views: 2200

Answers (1)

axtavt
axtavt

Reputation: 242696

@Component annotation works only for the class where it's declared, not for its subclasses (for performance reasons, I guess: otherwise it would require traversing of all ancestors for each class being scanned).

As a workaround, you can declare an assignable filter for your parent class in <context:component-scan>:

<context:component-scan base-package = "...">
    <context:include-filter type = "assignable" expression = "... your parent class ..." />
</context:component-scan>

Upvotes: 1

Related Questions