标签:eth call hot import pen mod back glib public
1 <project xmlns="http://maven.apache.org/POM/4.0.0" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.benshou</groupId> 7 <artifactId>CGLibDynamicProxy</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <properties> 12 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 13 </properties> 14 <!-- 配置maven插件 --> 15 <build> 16 <plugins> 17 <!-- java编译插件 --> 18 <plugin> 19 <groupId>org.apache.maven.plugins</groupId> 20 <artifactId>maven-compiler-plugin</artifactId> 21 <version>3.2</version> 22 <configuration> 23 <source>1.8</source> 24 <target>1.8</target> 25 <encoding>UTF-8</encoding> 26 </configuration> 27 </plugin> 28 </plugins> 29 </build> 30 <dependencies> 31 <dependency> 32 <groupId>cglib</groupId> 33 <artifactId>cglib</artifactId> 34 <version>2.2.2</version> 35 </dependency> 36 </dependencies> 37 </project>
package com.benshou; public class Laozong { public void chifan() { System.out.println("吃饭"); } public void mubiao() { System.out.println("目标"); } }
package com.benshou; import java.lang.reflect.Method; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; public class Mishu implements MethodInterceptor{ @Override public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable { System.out.println("预约时间"); //invoke()调用子类重写的方法 // arg1.invoke(arg0, arg2); // arg3.invokeSuper(arg0, arg2); Object result = arg3.invokeSuper(arg0, arg2); System.out.println("备注"); return result; } }
package com.benshou.CGLibDynamicProxy; import com.benshou.Laozong; import com.benshou.Mishu; import net.sf.cglib.proxy.Enhancer; public class App { public static void main( String[] args ) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(Laozong.class); enhancer.setCallback(new Mishu()); Laozong laozong = (Laozong) enhancer.create(); laozong.chifan(); } }
标签:eth call hot import pen mod back glib public
原文地址:https://www.cnblogs.com/benshou/p/12345736.html