The @Resource annotatio开发者_开发知识库n 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.
@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>
精彩评论