ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
在终端执行以下命令: ```shell git checkout -b 07 ``` 依次创建以下6个接口: ```java package net.zhaoxuyang.ioc.aop.intercept; public interface Advice { } package net.zhaoxuyang.ioc.aop.intercept; public interface Interceptor extends Advice { } package net.zhaoxuyang.ioc.aop.intercept; public interface MethodInterceptor extends Interceptor { Object invoke(MethodInvocation invocation) throws Throwable; } package net.zhaoxuyang.ioc.aop.intercept; import java.lang.reflect.AccessibleObject; public interface Joinpoint { Object proceed() throws Throwable; Object getThis(); AccessibleObject getStaticPart(); } package net.zhaoxuyang.ioc.aop.intercept; public interface Invocation extends Joinpoint { Object[] getArguments(); } package net.zhaoxuyang.ioc.aop.intercept; import java.lang.reflect.Method; public interface MethodInvocation extends Invocation { Method getMethod(); } ``` 创建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/aop/AopProxy.java` ,其内容为: ```java package net.zhaoxuyang.ioc.aop; public interface AopProxy { Object getProxy(); } ``` 创建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/aop/TargetSource.java` ,其内容为: ```java package net.zhaoxuyang.ioc.aop; /** * 被代理的对象 * @author zhaoxuyang */ public class TargetSource { private final Class<?> targetClass; private final Object target; public TargetSource( Object target, Class<?> targetClass) { this.target = target; this.targetClass = targetClass; } public Class<?> getTargetClass() { return targetClass; } public Object getTarget() { return target; } } ``` 创建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/aop/ReflectiveMethodInvocation.java` ,其内容为: ```java package net.zhaoxuyang.ioc.aop; import java.lang.reflect.AccessibleObject; import java.lang.reflect.Method; import net.zhaoxuyang.ioc.aop.intercept.MethodInvocation; /** * * @author zhaoxuyang */ public class ReflectiveMethodInvocation implements MethodInvocation{ private Object target; private Method method; private Object[] args; public ReflectiveMethodInvocation(Object target, Method method, Object[] args) { this.target = target; this.method = method; this.args = args; } @Override public Method getMethod() { return method; } @Override public Object[] getArguments() { return args; } @Override public Object proceed() throws Throwable { return method.invoke(target, args); } @Override public Object getThis() { return target; } @Override public AccessibleObject getStaticPart() { return method; } } ``` 创建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/aop/AdvisedSupport.java` ,其内容为: ```java package net.zhaoxuyang.ioc.aop; import net.zhaoxuyang.ioc.aop.intercept.MethodInterceptor; /** * 代理相关的元数据 * @author zhaoxuyang */ public class AdvisedSupport { private TargetSource targetSource; private MethodInterceptor methodInterceptor; public TargetSource getTargetSource() { return targetSource; } public void setTargetSource(TargetSource targetSource) { this.targetSource = targetSource; } public MethodInterceptor getMethodInterceptor() { return methodInterceptor; } public void setMethodInterceptor(MethodInterceptor methodInterceptor) { this.methodInterceptor = methodInterceptor; } } ``` 创建文件 `/Code/z8g/ioc/src/main/java/net/zhaoxuyang/ioc/aop/JdkDynamicAopProxy.java` ,其内容为: ```java package net.zhaoxuyang.ioc.aop; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import net.zhaoxuyang.ioc.aop.intercept.MethodInterceptor; public class JdkDynamicAopProxy implements AopProxy, InvocationHandler { private AdvisedSupport advised; public JdkDynamicAopProxy(AdvisedSupport advised) { this.advised = advised; } @Override public Object getProxy() { return Proxy.newProxyInstance( getClass().getClassLoader(), new Class<?>[]{advised.getTargetSource().getTargetClass()}, this ); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { MethodInterceptor interceptor = advised.getMethodInterceptor(); Object target = advised.getTargetSource().getTarget(); return interceptor.invoke(new ReflectiveMethodInvocation(target, method, args)); } } ``` 将测试包中的TestService修改为接口和实现类: 修改文件 `/Code/z8g/ioc/src/test/java/net/zhaoxuyang/ioc/bean/TestService.java` ,其内容为: ```java package net.zhaoxuyang.ioc.bean; public interface TestService { void echo(); } ``` 创建文件 `/Code/z8g/ioc/src/test/java/net/zhaoxuyang/ioc/bean/TestServiceImpl.java` ,其内容为: ```java package net.zhaoxuyang.ioc.bean; public class TestServiceImpl implements TestService{ private OutputService outputService; private String text; @Override public void echo() { System.out.println(text); } public void setText(String text) { this.text = text; } public OutputService getOutputService() { return outputService; } public void setOutputService(OutputService outputService) { this.outputService = outputService; } } ``` 修改文件 `/Code/z8g/ioc/src/test/resources/ioc.xml` 的 `testService`处,其内容为: ```xml <bean name="testService" class="net.zhaoxuyang.ioc.bean.TestServiceImpl"> ``` 创建文件 `/Code/z8g/ioc/src/test/java/net/zhaoxuyang/ioc/aop/TimeInterceptor.java` ,其内容为: ```java package net.zhaoxuyang.ioc.aop; import net.zhaoxuyang.ioc.aop.intercept.MethodInterceptor; import net.zhaoxuyang.ioc.aop.intercept.MethodInvocation; public class TimerInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { long time = System.nanoTime(); System.out.println("Invocation of Method " + invocation.getMethod().getName() + " start!"); Object proceed = invocation.proceed(); System.out.println("Invocation of Method " + invocation.getMethod().getName() + " end! takes " + (System.nanoTime() - time) + " nanoseconds."); return proceed; } } ``` 创建文件 `/Code/z8g/ioc/src/test/java/net/zhaoxuyang/ioc/aop/JdkDynamicAopProxyTest.java` ,其内容为: ```java package net.zhaoxuyang.ioc.aop; import net.zhaoxuyang.ioc.bean.TestService; import net.zhaoxuyang.ioc.context.ApplicationContext; import net.zhaoxuyang.ioc.context.ClassPathXmlApplicationContext; import org.junit.Test; public class JdkDynamicAopProxyTest { @Test public void testInterceptor() throws Exception { // testService without AOP ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ioc.xml"); TestService testService = (TestService) applicationContext.getBean("testService"); testService.echo(); // testService with AOP // 1. 设置被代理对象(Joinpoint) AdvisedSupport advisedSupport = new AdvisedSupport(); TargetSource targetSource = new TargetSource(testService, TestService.class); advisedSupport.setTargetSource(targetSource); // 2. 设置拦截器(Advice) TimerInterceptor timerInterceptor = new TimerInterceptor(); advisedSupport.setMethodInterceptor(timerInterceptor); // 3. 创建代理(Proxy) JdkDynamicAopProxy jdkDynamicAopProxy = new JdkDynamicAopProxy(advisedSupport); TestService testServiceProxy = (TestService) jdkDynamicAopProxy.getProxy(); // 4. 基于AOP的调用 testServiceProxy.echo(); } } ``` 在终端执行以下命令: ```shell $ git add . $ git commit -m 'method-intercaptor-by-jdk-dynamic-proxy' ```