保留 CGLIB 代理上的注释?

发布于 2024-08-11 08:53:05 字数 130 浏览 4 评论 0原文

我正在尝试使用 AOP 框架创建一个对象,该框架使用 CGLIB 创建代理对象。 奇怪的是,“增强型”代理对象没有前一个类所具有的任何注释!

谁能告诉我如何让 CGLIB 保留它创建的代理上的注释?

干杯! 尼拉夫

Am trying to create an object using an AOP framework which uses CGLIB to create proxy objects.
Strangely enough, the "enhanced" proxy object is devoid of ANY annotations the previous class had!

Can anyone tell me how can I make CGLIB retain the annotations on the proxies it creates?

Cheers!
Nirav

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

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

发布评论

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

评论(4

水中月 2024-08-18 08:53:05

CGLIB 创建给定类的子类来生成代理。除非在注释定义中明确指定,否则注释不会保留在子类中。使用 @Inherited 注释为此目的。

您可以在您定义的注释中使用此注释,并使它们在子类中可访问,如下所示:

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyAnnotation {
}

CGLIB creates subclasses of given classes to generate proxies. Annotations are not preserved in subclasses unless explicitly specified in annotation definition. @Inherited annotation is used for this purpose.

You can use this annotation in the annotations you define, and make them reachable in subclasses, as following:

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyAnnotation {
}
纵性 2024-08-18 08:53:05

这不是“保留”注释的问题。 CGLIB 代理实际上是目标对象类的生成子类。这些子类可能没有注释,但它们的超类(即您自己的类)仍然会有它们。您使用的任何反映注释的代码都需要能够回顾类层次结构以查找注释。

This isn't an issue with "retaining" the annotations. CGLIB proxies are actually generated subclasses of the target object's class. These subclasses may not have annotations, but their superclass (i.e. your own class) will still have them. Any annotation-reflecting code you use needs to be able to look back up the class hierarchy to look for annotations.

可是我不能没有你 2024-08-18 08:53:05

Cglib 无法在不更改其内部实现的情况下保留注释。然而,这相当复杂,相信我,我尝试过。然而,我最终想出的修改版本是如此复杂,以至于我决定宁愿实现 Byte Buddy,另一个代码生成库,它具有这样的功能。

下面是一个如何创建子类的示例,

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation { }

@MyAnnotation
class MyClass { }

assertThat(new ByteBuddy()
  .subclass(Object.class)
  .attribute(TypeAttributeAppender.ForSuperType.INSTANCE)
  .make()
  .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
  .getLoaded()
  .isAnnotationPresent(MyAnnotation.class), is(true));

Byte Buddy 附带了广泛的全文文档和 javadoc,并且它具有很强的可扩展性。希望您充分利用图书馆。

Cglib is not capable of retaining annotations without changing its internal implementation. This is however quite complicated and believe me I tried. My altered version I finally came up with was however so complicated that I decided to rather implement Byte Buddy, another code generation library which is capable of such functionality.

Here is an example of how you can create subclass that

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation { }

@MyAnnotation
class MyClass { }

assertThat(new ByteBuddy()
  .subclass(Object.class)
  .attribute(TypeAttributeAppender.ForSuperType.INSTANCE)
  .make()
  .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
  .getLoaded()
  .isAnnotationPresent(MyAnnotation.class), is(true));

Byte Buddy comes with an extensive full-text documentation and javadoc and it is quite extensible. Hope you make good use of the library.

谢绝鈎搭 2024-08-18 08:53:05

这是一个有效的问题(我自己现在遇到了),因为a)并非所有框架都足够聪明来检查父类b)即使它们足够聪明,他们也可能选择不这样做。吉斯似乎属于后者。 FWIW,https://issues.apache.org/jira/browse/WICKET-1130< /a> 是我发现这个问题时正在解决的问题。

It's a valid problem (I'm running into now myself) as a) not all frameworks are smart enough to inspect parent classes b) even if they are smart enough, they may chose not to. The latter seems to be the case with Guice. FWIW, https://issues.apache.org/jira/browse/WICKET-1130 is the problem I was working on when I found this out.

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