0

我在我的项目中使用弹簧安全注释。在某些情况下,我想调用带注释对象的无安全版本。Spring 默认情况下会为带注释的对象创建一个启用安全的代理,并将其用于代码中的自动装配,有什么方法可以使用 spring 实现这一点?
  一个明显的方法是手动创建与我希望此功能具有注释的每个类对应的代理类,并且这些方法的实现只是将其委托给实际对象。

4

1 回答 1

0

作为 JDK 代理的一个选项,您可以在运行时获取实际的 bean:

MyBean proxy;    
if(AopUtils.isJdkDynamicProxy(proxy)) {
    MyBean actualInstance = (MyBean) ((Advised)proxy).getTargetSource().getTarget()
}

actualInstance.doSomethingSecured(); // no advice related to this method will be called
// so your security annotation will be ignored (transactions, cache, and everething that requires AOP too...)

但是从架构的角度来看,使用手动代理的方法看起来更少错误(除非您绝对确定您不需要安全性以及所有其他可能的方面)。

您可以使用泛型提高可读性:

MyBean actualInstance = extractProxyTarget(proxy, proxy.getClass());
actualInstance.doSomethingSecured();
于 2013-03-13T10:22:42.120 回答