springAOP秘书老总案例
ILaoZong接口
public interface ILaoZong {
void eat();
}
ILaoZongImpl实现类
package com.ly.demo01;
public class ILaoZongImpl implements ILaoZong{
@Override
public void eat() {
System.out.println("吃佛跳墙");
}
}
MiShu实现类
package com.ly.demo01;
public class MiShu {
public void dianyan(){
System.out.println("点烟");
}
public void daojiu(){
System.out.println("倒酒");
}
}
Test测试类
import com.ly.demo01.ILaoZongImpl;
import com.ly.demo01.MiShu;
import org.junit.Test;
import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.cglib.proxy.Proxy;
import java.lang.reflect.Method;
public class Demo01Test {
@Test
public void test01(){
ILaoZongImpl laoZong = new ILaoZongImpl();
MiShu miShu = new MiShu();
ClassLoader classLoader =ILaoZongImpl.class.getClassLoader();
Class[] interfaces = new Class[]{ILaoZong.class};
InvocationHandler handler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
miShu.daojiu();
Object returnVal = method.invoke(laoZong, args);
miShu.dianyan();
return returnVal;
}
};
ILaoZong iLaoZong = (ILaoZong) Proxy.newProxyInstance(classLoader,interfaces,handler);
iLaoZong.eat();
}
}
测试结果
版权声明:本文为qq_41680667原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。