基于继承的 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]

转载于:https://www.cnblogs.com/zno2/p/9153140.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值