通过反射 API 进行类检查。使用自定义注释

发布于 2025-01-18 20:02:37 字数 949 浏览 4 评论 0 原文

我在询问之前已经做了研究,但没有运气。

我有一个 StartUp Singleton bean。在这个 bean 中,我有一个 @Inject @Any Instance。我循环所有实现并尝试检查该类是否使用自定义注释进行注释。所有实现(我想要检查的所有类)都是有状态或无状态 bean。 有时找到我想要的类,我可以执行 getClass().isAnnotationPresent(ClassNameAnnotation.class)

大多数时候我得到一个代理对象,在这种情况下我无法执行上述检查。 我找不到获得真实对象的方法。我曾尝试获得超级班,但运气不佳。 我将附上一些代码,以便您可以有更好的想法。

@Singleton
@Startup
public class CacheLoader {

    @Inject
    @Any
    private Instance<ClassNameA> aClasses;

   .......

    @Lock(LockType.READ)
    public void evaluate() {

        if (!aClasses.isUnsatisfied()) {
            for (ClassNameA className : aClasses) {
                if (className.getClass().isAnnotationPresent(ClassNameAnnotation.class)) {
                    ....
                }
            }
        }
    }

}

我尝试使用代理对象的超类,但它没有返回我想要的内容。我也尝试通过 Proxy.getInspirationHandler() 。即使当我检查 Proxy.isProxyClass(getClass()) 或 isSynthetic() 方法时,也不会返回该对象是代理。

谢谢你!

I have done my research before asking but no luck.

I have a StartUp Singleton bean. In this bean I have an @Inject @Any Instance. I loop all the implementations and try to check if the class is annotated with a custom annotation. All the implementations(all the classes that I want to inspect) are Stateful or Stateless beans.
Sometime the class I want is found and I can perform getClass().isAnnotationPresent(ClassNameAnnotation.class)

Most of the times I get a proxy object and in this case I cannot perform the above check.
I cannot find a way to get the real object. I have tried to get the SuperClass but not luck.
I will attach some of the code so you can have a better idea.

@Singleton
@Startup
public class CacheLoader {

    @Inject
    @Any
    private Instance<ClassNameA> aClasses;

   .......

    @Lock(LockType.READ)
    public void evaluate() {

        if (!aClasses.isUnsatisfied()) {
            for (ClassNameA className : aClasses) {
                if (className.getClass().isAnnotationPresent(ClassNameAnnotation.class)) {
                    ....
                }
            }
        }
    }

}

I tried to use the SuperClass of the proxy object but it does not return what I want. I tried also via Proxy.getInvocationHandler(). Even when I check the methods Proxy.isProxyClass(getClass()) or isSynthetic() does not return that the object is a proxy.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

物价感观 2025-01-25 20:02:38

我认为,使用CDI便携式扩展名而不是单身人士EJB,您会更好地为您服务。 CDI中的几个原因

  • ,一切都是代理。因此,就像一些评论者所说的那样,使用反射将非常脆弱,因为它不是规格的一部分。您正在处理运行时定义的课程。如果您将自己绑在特定于实施的细节上,它可能会起作用,但是它可能会在CDI容器的版本之间破裂。

  • CDI容器将为您进行所有注释扫描:)

  • 便携式扩展程序在启动时运行,然后其他东西开始围绕您的应用程序飞行

,Google搜索给了我本指南,但是有很多: https://www.baeldung.com/cdi-portable-extension

如果您正在修改,我想您想在 process AngrotatedType()中挂钩BEAN声明或 AfterBeanDiscovery()如果您只是按照您说的记录。

实际上,我们在内部使用的CDI便携式扩展名,可以为环境带来一些配置魔法。配置参数之一是一个不是预选程序注释的注释,听起来像您想要的... CDI容器可以为您提供类型,您可以从中检查注释。

最后,这与您的问题无关,而是有用的:如果您的注释通过注释的字段驱动配置,则选择它们可能会非常复杂,因为Java类型和继承系统如何与注释一起使用。在这种情况下,您可以通过使用 AnnotationLitreal 受益。在此处在此处阅读此处: http://www.kurtsparber.de/?p = 387

编辑:
另一个旁注...甚至我认为您应该切换到便携式扩展名,您不再需要 @EJB的Singleton启动了!您可以使用纯cdi:

I think you would be better served by using a CDI Portable Extension, rather than a Singleton EJB. A couple of reasons

  • In CDI, everything is a proxy. So like some of the commenters have said, using reflection would be very fragile as it's not part of the spec. You're dealing with classes that are defined at runtime. It may work if you tie yourself to implementation-specific details, but it could break between releases of your CDI container.

  • The CDI Container will do all of the annotation scanning for you :)

  • A portable extension runs on startup, before other stuff starts flying around your app

A google search gave me this guide, but there are lots of them: https://www.baeldung.com/cdi-portable-extension

I think you would want to hook in processAnnotatedType() if you're modifying the bean declarations, or afterBeanDiscovery() if you're just documenting them as you said.

We actually have a CDI Portable Extension we use internally that does some config magic for environments. One of the config params is an annotation that is not a qualifier annotation, which sounds like what you want... the CDI container can get you the type, from which you can inspect the annotations.

Finally, this is not directed related to your question but may be useful: If your annotations drive configuration through fields of the annotations, selecting them can be quite complicated because of how the Java type and inheritance system works with annotations. You may benefit by using AnnotationLitreal in those cases. Read up here on this useful utility class here: http://www.kurtsparber.de/?p=387

EDIT:
Another side note... even thought I think you should switch to a Portable Extension, you shouldn't need @EJB's Singleton Startup anymore! you can do this with pure CDI: https://rmannibucau.wordpress.com/2015/03/10/cdi-and-startup/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文