net.sf.cglib.proxy.Enhancer
        <dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>

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修饰的(会失效)

如果代扣类构造函数是带参数的,需要

基于继承的 MethodInterceptor 动态代理(换种写法)的更多相关文章

  1. Java 动态代理 两种实现方法

    AOP的拦截功能是由java中的动态代理来实现的.说白了,就是在目标类的基础上增加切面逻辑,生成增强的目标类(该切面逻辑或者在目标类函数执行之前,或者目标类函数执行之后,或者在目标类函数抛出异常时候执 ...

  2. 基于接口的 InvocationHandler 动态代理(换种写法)

    InvocationHandler is the interface implemented by the invocation handler of a proxy instance. Each p ...

  3. 基于 CGLIB 库的动态代理机制

    之前的文章我们详细的介绍了 JDK 自身的 API 所提供的一种动态代理的实现,它的实现相对而言是简单的,但是却有一个非常致命性的缺陷,就是只能为接口中的方法完成代理,而委托类自己的方法或者父类中的方 ...

  4. 【Java EE 学习 50】【Spring学习第二天】【使用注解的DI实现】【spring中的继承】【动态代理伪hibernate实现】

    一.使用注解的DI实现 1.@Resource 使用该注解能够实现引用型属性的DI实现,该注解能够根据属性名和属性类型自动给属性赋值.一般使用@Resource(name="student& ...

  5. 基于jdk proxy的动态代理模式

    代理模式 是spring AOP机制的实现基础,有必要学习一下. 有两种,一种是目标类有接口的, 采用JDK动态代理,一种是目标类没接口的,采用CGLIB动态代理. 先看一组代码, package c ...

  6. 死磕Spring之AOP篇 - 初识JDK、CGLIB两种动态代理

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  7. Java 动态代理原理图解 (附:2种实现方式详细对比)

    ​ 动态代理在 Java 中有着广泛的应用,例如:Spring AOP 面向切面编程,Hibernate 数据查询.以及 RPC Dubbo 远程调用等,都有非常多的实际应用@mikechen 目录 ...

  8. Java代理模式,一次复习完4种动态代理实现方式

    代理模式也是一种非常常见的设计模式.了解Spring框架的都知道,Spring AOP 使用的就是动态代理模式.今天就来系统的重温一遍代理模式. 在现实生活中代理是随处可见的,当事人因某些隐私不方便出 ...

  9. JDK动态代理(Proxy)的两种实现方式

    JDK自带的Proxy动态代理两种实现方式 前提条件:JDK Proxy必须实现对象接口 so,创建一个接口文件,一个实现接口对象,一个动态代理文件 接口文件:TargetInterface.java ...

随机推荐

  1. NYOJ37-回文字符串(dp)

    37-回文字符串 内存限制:64MB 时间限制:3000ms 特判: No通过数:88 提交数:177 难度:4 题目描述: 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如& ...

  2. 7、...arg ...[1,2,3] 数组扩展

    1.将离散的参数转成数组 2.将数组拆成单个离散的值 https://blog.csdn.net/qq_30100043/article/details/53391308 箭头函数写法 函数名 -&g ...

  3. SVM-sklearn

    目的:1000张数字0-9的手写数字,训练识别手写数字:将其作为32*32的0,1化的数字,随后会将其变为1024列的一个向量 原理:SVM就是把平面的点变为一个空间的点,更好切,核函数就是怎么把他变 ...

  4. C语言基础02

    1. .so -- shared object,用户层的动态库 ..ko -- kernel object,内核模块,可以在Linux内核起来之后动态的加载和卸载. 2.

  5. 自己实现HashSet

    HashSet的实现相对比较简单.它强依赖于HashMap,包括底层数据实际上就是存储于HashMap,由于HashMap在哈希碰撞下,如果value值相同,那么将会覆盖该value,HashSet正 ...

  6. Appium 学习三:Appium-Desktop 填坑

    之前使用appium执行自动化脚本,现使用appium-desktop  遇到了一些问题 1.使用name定位的报错   Locator Strategy 'name' is not supporte ...

  7. java批量生成用例脚本-保留字关键词的用例脚本生成

    20171012新补丁包程序,程序整理了全部的关键字和保留字,支持 字段a = "字段b" 与 字段a = 字段b,并做了代码重构.对补丁包进行关键字专项测试,验证关键字保留字作为 ...

  8. 项目总结20:阿里云免费https证书申请

    项目总结20:阿里云免费https证书申请 1. 登录阿里云控制台 www.aliyun.com,用账户信息登录 2. 在”产品与服务”搜索SSL,选择SSL证书 3. 点击购买证书 4. 选择” S ...

  9. 更换Appsecrect应该要注意的问题

    1. 有时候因为需要,有些地方需要填写Appsecrect, 但是大家都知道微信公众平台上这个值 即使你自己是管理员是看不到的,只能重置,但是重置后,一定要记住刷新AccessToken啊,不然就尴尬 ...

  10. Callable Future接口的设计原理

    我们都知道Callable接口作为任务给线程池来执行,可以通过Future对象来获取返回值,他们背后的实现原理是什么?通过总结背后的实现原理有助于我们深入的理解相关技术,做到触类旁通和举一反三. 文章 ...