基于继承的 MethodInterceptor 动态代理(换种写法)

net. sf. cglib. proxy.Enhancer

Generates dynamic subclasses to enable method interception. This class started as a substitute for the standard Dynamic Proxy support included with JDK 1.3, but one that allowed the proxies to extend a concrete base class, in addition to implementing interfaces. The dynamically generated subclasses override the non-final methods of the superclass and have hooks which callback to user-defined interceptor implementations.

The original and most general callback type is the MethodInterceptor, which in AOP terms enables "around advice"--that is, you can invoke custom code both before and after the invocation of the "super" method. In addition you can modify the arguments before calling the super method, or not call it at all.

Although MethodInterceptor is generic enough to meet any interception need, it is often overkill. For simplicity and performance, additional specialized callback types, such as LazyLoader are also available. Often a single callback will be used per enhanced class, but you can control which callback is used on a per-method basis with a CallbackFilter.

The most common uses of this class are embodied in the static helper methods. For advanced needs, such as customizing the ClassLoader to use, you should create a new instance of Enhancer. Other classes within CGLIB follow a similar pattern.

All enhanced objects implement the Factory interface, unless setUseFactory is used to explicitly disable this feature. The Factory interface provides an API to change the callbacks of an existing object, as well as a faster and easier way to create new instances of the same type.

For an almost drop-in replacement for java.lang.reflect.Proxy, see the Proxy class.

substitute  ['sʌbstɪtjuːt] 代替

concrete ['kɒŋkriːt] 具体的

in addition to 除...之外

package cn.zno.newstar.base.utils.text;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class ProxyDemo {
    public static void main(String[] args) {

        TargetMI mi = new TargetMI();
        Target proxy = mi.newProxyInstance(Target.class);
        proxy.doSomething();
    }
}

class Target {
    public void doSomething() {
        System.out.println("doSomething");
    }
}

class TargetMI implements MethodInterceptor {

    @SuppressWarnings("unchecked")
    public <T> T newProxyInstance(Class<T> target) {
        Enhancer e = new Enhancer();
        e.setSuperclass(target);
        e.setCallback(this);
        T proxyInstance = (T) e.create();
        return proxyInstance;
    }

    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        System.out.println("proxy start");
        /* 
         * 方法调用前后
         * 方法名正则,参数,注解
         * 随意定制动态代理
         * 
         * */
        
        Object retValFromSuper = proxy.invokeSuper(obj, args);
        System.out.println("proxy end");
        return retValFromSuper;
    }
}

结果:

proxy start
doSomething
proxy end

注意:

net.sf.cglib.proxy.Enhancer  通过继承目标类,以达到目的。因此目标类不能是final修饰的(会抛异常),目标方法不能是final修饰的(会失效)

  •  ['sʌbstɪtjuːt]

猜你喜欢

转载自www.cnblogs.com/zno2/p/9153140.html