CGlib 动态代理

CGlib 动态代理

CGlib动态代理的简单测试
继承 MethodInterceptor

Boy.java

public class Boy {
	private int age;
	private String name;
	public void eat() {
		System.out.println("I am eating ");
		
	}
	
	public void playGame() {
		System.out.println("I am playing games");
	}
	public void playGame(String game) {
		System.out.println("I am playing"+game+" games");
	}
}

CGLIbFactoryBoy2.java

import java.lang.reflect.Method;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

public class CGLIbFactoryBoy2 implements MethodInterceptor {
	private Object target;
	
	public CGLIbFactoryBoy2() {
		super();
	}
	public CGLIbFactoryBoy2(Object taObject) {
		super();
		this.target=taObject;
	}
	public Object createWatching() {
		//增强器
		Enhancer enhancer = new Enhancer();
		//创建子类,作为代理类
		enhancer.setSuperclass(Boy.class);
		//设置回调类
		enhancer.setCallback(this);
		return enhancer.create();
		}
	@Override
	public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
		System.out.println("before");
		method.invoke(target,args);
		System.out.println("after");
		return null;
	}
}

TestGetBean.java

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class TestGetBean {
	public static void main(String[] args) {
		final Boy boy=new Boy();
		Boy proxyBoy=(Boy) new CGLIbFactoryBoy2(boy).createWatching();
		proxyBoy.playGame("porn");
		proxyBoy.eat();
	}
}

运行结果

before
I am playingporn games
after
before
I am eating 
after
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CGLIB是一个强大的开源代码生成库,它可以在运行时扩展Java类和实现接口。CGLIB动态代理CGLIB库的一个重要特性,它通过生成目标类的子类来实现代理。 在CGLIB动态代理中,我们可以使用`MethodInterceptor`接口来定义代理逻辑。`MethodInterceptor`接口有一个`intercept`方法,该方法在目标方法被调用时被触发。在`intercept`方法中,我们可以编写自定义的逻辑来增强目标方法的功能。 下面是一个使用CGLIB动态代理的示例代码: ```java import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; import java.lang.reflect.Method; public class CglibProxyExample implements MethodInterceptor { public Object createProxy(Object target) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(target.getClass()); enhancer.setCallback(this); return enhancer.create(); } @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { // 在目标方法执行前的逻辑 System.out.println("Before method: " + method.getName()); // 调用目标方法 Object result = proxy.invokeSuper(obj, args); // 在目标方法执行后的逻辑 System.out.println("After method: " + method.getName()); return result; } } ``` 在上述示例中,我们首先创建了一个`CglibProxyExample`类,实现了`MethodInterceptor`接口。然后,我们通过`Enhancer`类创建了一个代理对象,并设置了目标类和代理逻辑。在`intercept`方法中,我们可以在目标方法执行前后添加自定义的逻辑。 使用CGLIB动态代理时,我们可以通过调用`createProxy`方法来创建代理对象。例如: ```java SomeClass target = new SomeClass(); CglibProxyExample proxyExample = new CglibProxyExample(); SomeClass proxy = (SomeClass) proxyExample.createProxy(target); ``` 这样,我们就可以通过`proxy`对象来调用目标类的方法,并在方法执行前后添加自定义的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值