如何在具有受保护方法的 Spring 中注入 JDK 代理类? [英] How to inject an JDK Proxied class in Spring which has protected methods?

查看:43
本文介绍了如何在具有受保护方法的 Spring 中注入 JDK 代理类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组相当奇怪的情况.我将 Spring 3.0.6 与实现和接口的 bean 一起使用.到目前为止,很正常的东西.我的 bean 也有一些受保护的方法.我正在使用一些 AOP(JDK 代理)并且一切正常.

I have a fairly odd set of circumstances. I'm using Spring 3.0.6 with a bean which implements and interface. So far, pretty normal stuff. My bean also has some protected methods. I'm using some AOP (JDK Proxies) and everything works fine.

当我想将这个 bean 注入另一个包类时,我的问题就出现了.在正常情况下,我将能够注入实现并访问受保护的方法.遗憾的是,由于是JDK代理的,所以只能基于接口注入.

My problem comes in when I want to inject this bean into another package class. Under normal circumstances, I would be able to inject the implementation and get access to the protected methods. Unfortunately, since it is JDK proxied, I can only inject based on the interface.

因为我需要访问受保护的方法,所以我不能在接口中声明这些方法,所以我有点处于 catch-22 的情况.我尝试切换到 CGLIB 代理,但它们与其他顾问崩溃,建议使用最终方法等),因此这不是真正的解决方案.

Since I need access to the protected methods, I cannot declare the methods in the interface, so I'm somewhat in a catch-22 situation. I tried switching to CGLIB proxies, but they crash with other advisors advising beans with final methods, etc) so that isn't really a solution.

对我能做什么有什么建议吗?我曾尝试使用 @PostConstruct 方法从应用程序上下文中检索 bean,但同样(不足为奇)它也只能检索代理 bean,因此无法将其强制转换为所需的实现.

Any suggestions as to what I can do? I've tried using a @PostConstruct method to retrieve the bean from the application context, but there too (not surprisingly) it is only able to retrieve the Proxied bean and consequently cannot cast it to the implementation required.

如有任何建议,我们将不胜感激.

Any suggestions would be appreciated.

谢谢!

埃里克

推荐答案

我能想到的解决方案是从代理对象中获取实现.

The solution I can think of is to fetch implementation from proxy object.

http://www.techper.net/2009/06/05/how-to-acess-target-object-behind-a-spring-proxy/

如上面链接中给出的((Advised)proxy).getTargetSource().getTarget()"从代理对象返回目标实现.现在您可以将此目标对象转换为 Impl 类 &然后在那个 impl 类上你可以调用 protected 方法.

As given in above link "((Advised)proxy).getTargetSource().getTarget()" returns target implementation fro proxy object. Now you can cast this target object into the Impl class & then on that impl class you can call protected method.

看看这是否有效.

这篇关于如何在具有受保护方法的 Spring 中注入 JDK 代理类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆