Spring支持使用注解@Component和
我尝试创建自定义可继承注释:
@Component
@Inherited
@Retention(RUNTIME)
public @interface MyInheritableComponent {}
不幸的是,@MyInheritableComponent 不起作用。
有什么想法吗?
编辑
文件 my/package/Injectable.java:
@MyInheritableComponent
public class Injectable {}
文件 my/package/Foo.java:
public class Foo extends Injectable {}
组件扫描定义为:
<context:component-scan base-package="my.package" />
在上面的示例中,不会自动检测到 Foo。
我的意图是,隐藏某些特定类的@Component注释。
最佳答案
由于缺少@Inherited
上@Component
<context:component-scan>
使用默认过滤器不尊重 @Inherited
在您的自定义注释上。
但是,如果您为注释添加自定义过滤器,它将按预期工作:
<context:component-scan base-package="my.package">
<context:include-filter type = "annotation" expression = "MyInheritableComponent" />
</context:component-scan>
关于java - 如何自动检测和连接从特定类型扩展的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5227529/