开发者

@Resource annotation not injecting a subclass

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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜