java - 如何自动检测和连接从特定类型扩展的类?

标签 java spring ioc-container

Spring支持使用注解@Component和自动检测类,但是@Component不是@Inherited。

我尝试创建自定义可继承注释:

@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/

相关文章:

java - 在 Android 中显示图像上的关键点 - OpenCV

java - 从服务器检索字符串

java - 使用 swagger 创建 api 文档

c# - 在仍然使用依赖注入(inject)的同时创建新实例

python - 如何在 python 中替换特定的数据库实现?

java - Selenium WebDriver 找不到元素

java - 有没有任何工具可以帮助我将 Java Swing 代码中的数据转换为模板文件?

java - Guava LoadingCache中的load()方法是什么?

java - 如何将Excel的特定列插入数据库

c# - 使用 CaSTLe Windsor IoC 容器时添加服务初始化方法