码迷,mamicode.com
首页 > 其他好文 > 详细

使用Maven实现CGLibDynamicProxy

时间:2020-10-18 10:03:44      阅读:14      评论:0      收藏:0      [点我收藏+]

标签: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();
    }
}

  

使用Maven实现CGLibDynamicProxy

标签:eth   call   hot   import   pen   mod   back   glib   public   

原文地址:https://www.cnblogs.com/benshou/p/12345736.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!