cglib动态代理实现AOP拦截

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

package com.springmvc.test;

public class Base {

	public void add(){
		
		System.out.println("add ----- ");
	}
}
package com.springmvc.test;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

public class CglibProxy implements MethodInterceptor{

	@Override
	public Object intercept(Object object, Method method, Object[] args,
			MethodProxy proxy) throws Throwable {

		System.out.println("before--------");
		proxy.invokeSuper(object, args);
		System.out.println("after--------");
		
		return null;
	}

}
package com.springmvc.test;

import org.springframework.cglib.proxy.Enhancer;

public class Factory {

	public static Base getInstance(CglibProxy proxy){
		
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(Base.class);
		enhancer.setCallback(proxy);
		Base base = (Base) enhancer.create();
		return base;
	}
}
package com.springmvc.test;

public class Test01 {

	public static void main(String[] args) {
		
		CglibProxy proxy = new CglibProxy();
		Base base = Factory.getInstance(proxy);
		base.add();
	}
}

转载于:https://my.oschina.net/projerry/blog/807404

你可能感兴趣的:(cglib动态代理实现AOP拦截)