10

可能是我想得不够努力,或者答案真的难以捉摸。快速场景(尝试代码。它编译)。

考虑一个遗留接口

public interface LegacyInterfaceNoCodeAvailable{
    void logInfo(String message);
}

考虑上面接口的遗留实现

public abstract class LegacyClassNoCodeAvailable implements LegacyInterfaceNoCodeAvailable{

    public abstract void executeSomething();

    public void rockItOldSchool(){
        logInfo("bustin' chops, old-school style");
    }

    @Override
    public void logInfo(String message){
        System.out.println(message);
    }
}

现在我以这个雄心勃勃的人的身份进来,为“新”系统编写了一个类,但它在“旧”框架内运行,因此我必须扩展旧基类。

public class lass SpankingShiny extends LegacyClassNoCodeAvailable{

    public void executeSomething(){
        rockItOldSchool();
        logInfo("I'm the King around here now");
        System.out.println("this new stuff rocks!!");
    }
}

一切都很好,就像您期望的那样:

SpankingShiny shiny = new SpankingShiny();
shiny.executeSomething();

上面的代码产生(如预期的那样):

bustin' chops, old-school style
I'm the King around here now
this new stuff rocks!!

现在您可以看到,'System.out.println()' 忠实地打印了所需的输出。但我希望用记录器替换“System.out.println()”。

问题:

我无法让 CGLIB 代理拦截“logInfo(string)”的方法并让它通过记录器打印出我想要的消息(顺便说一下,我已经完成了日志记录配置)。该方法调用“显然”没有命中代理。

代码:

public class SpankingShinyProxy implements MethodInterceptor{

    private SpankingShiny realShiny;
    private final Logger logger = Logger.getLogger(SpankingShinyProxy.class);

    public SpankingShinyProxy(SpankingShiny realShiny) {
        super();
        this.realShiny = realShiny;
    }

    @Override
    public Object intercept(Object proxyObj, Method proxyMethod, Object[] methodParams, MethodProxy methodProxy) throws Throwable {
        String methodName = proxyMethod.getName();
        if("logInfo".equals(methodName)){
            logger.info(methodParams[0]);
        }
        return proxyMethod.invoke(realShiny, methodParams);
    }

    public static SpankingShiny createProxy(SpankingShiny realObj){
        Enhancer e = new Enhancer();
        e.setSuperclass(realObj.getClass());
        e.setCallback(new SpankingShinyProxy(realObj));
        SpankingShiny proxifiedObj = (SpankingShiny) e.create();
        return proxifiedObj;
    }
}

主要方法:

public static void main(String... args) {

        SpankingShiny shiny = new SpankingShiny();
        shiny.executeSomething();

        SpankingShiny shinyO = SpankingShinyProxy.createProxy(shiny);
        shinyO.executeSomething();
    }

上面的代码产生(不像预期的那样):

bustin' chops, old-school style
I'm the King around here now
this new stuff rocks!!
bustin' chops, old-school style
I'm the King around here now
this new stuff rocks!!

我会在哪里出错?

谢谢!

4

2 回答 2

3

我有同样的问题。就我而言,realObj它本身就是一个代理(一个 Spring Bean - a @Component)。

所以我必须做的是改变.setSuperClass()部分:

Enhancer e = new Enhancer();
e.setSuperclass(realObj.getClass());
e.setCallback(new SpankingShinyProxy(realObj));
SpankingShiny proxifiedObj = (SpankingShiny) e.create();

我变了:

e.setSuperclass(realObj.getClass());

至:

e.setSuperclass(realObj.getClass().getSuperClass());

如前所述,这是因为realObj.getClass()它本身就是一个 CGLIB 代理,并且该方法返回了一个疯狂名称 CGLIB 生成的类,例如a.b.c.MyClass$$EnhancerBySpringCGLIB$$1e18666c. 当我添加.getSuperClass()它时,它返回了它应该首先返回的类。

于 2016-03-31T22:16:33.377 回答
1

好吧,首先,你很幸运你的代理没有被击中。如果您在 内引用实际代理intercept,您将最终陷入无限循环,因为您的反射方法 incocation 将被相同的调度SpankingShinyProxy。一次又一次。

代理不起作用,因为您只是将executeSomething代理上的方法调用委托给某个未代理的对象。您不得使用realObj. 所有方法调用都必须由您的代理分派,那些由代理调用的方法调用也必须命中代理本身!

intercept将方法中的最后一行更改为methodProxy.invokeSuper(proxyObj, args). 然后,使用Enhancer. 如果您的构造函数SpankingShiny不需要参数,create则可以不带任何参数调用。否则,将您通常提供给该方法的构造函数的对象提供给该create方法。然后,只使用你从中得到的对象,你create就很好。

如果您想了解有关 cglib 的更多信息,您可能需要阅读这篇博客文章: http: //mydailyjava.blogspot.no/2013/11/cglib-missing-manual.html

于 2013-11-14T01:05:21.663 回答