保留 CGLIB 代理上的注释?
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
CGLIB 创建给定类的子类来生成代理。除非在注释定义中明确指定,否则注释不会保留在子类中。使用 @Inherited 注释为此目的。
您可以在您定义的注释中使用此注释,并使它们在子类中可访问,如下所示:
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:
这不是“保留”注释的问题。 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.
Cglib 无法在不更改其内部实现的情况下保留注释。然而,这相当复杂,相信我,我尝试过。然而,我最终想出的修改版本是如此复杂,以至于我决定宁愿实现 Byte Buddy,另一个代码生成库,它具有这样的功能。
下面是一个如何创建子类的示例,
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
Byte Buddy comes with an extensive full-text documentation and javadoc and it is quite extensible. Hope you make good use of the library.
这是一个有效的问题(我自己现在遇到了),因为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.