在终端执行以下命令:
```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'
```
- 空白目录
- 精简版Spring的实现
- 0 前言
- 1 注册和获取bean
- 2 抽象工厂实例化bean
- 3 注入bean属性
- 4 通过XML配置beanFactory
- 5 将bean注入到bean
- 6 加入应用程序上下文
- 7 JDK动态代理实现的方法拦截器
- 8 加入切入点和aspectj
- 9 自动创建AOP代理
- Redis原理
- 1 Redis简介与构建
- 1.1 什么是Redis
- 1.2 构建Redis
- 1.3 源码结构
- 2 Redis数据结构与对象
- 2.1 简单动态字符串
- 2.1.1 sds的结构
- 2.1.2 sds与C字符串的区别
- 2.1.3 sds主要操作的API
- 2.2 双向链表
- 2.2.1 adlist的结构
- 2.2.2 adlist和listNode的API
- 2.3 字典
- 2.3.1 字典的结构
- 2.3.2 哈希算法
- 2.3.3 解决键冲突
- 2.3.4 rehash
- 2.3.5 字典的API
- 2.4 跳跃表
- 2.4.1 跳跃表的结构
- 2.4.2 跳跃表的API
- 2.5 整数集合
- 2.5.1 整数集合的结构
- 2.5.2 整数集合的API
- 2.6 压缩列表
- 2.6.1 压缩列表的结构
- 2.6.2 压缩列表结点的结构
- 2.6.3 连锁更新
- 2.6.4 压缩列表API
- 2.7 对象
- 2.7.1 类型
- 2.7.2 编码和底层实现
- 2.7.3 字符串对象
- 2.7.4 列表对象
- 2.7.5 哈希对象
- 2.7.6 集合对象
- 2.7.7 有序集合对象
- 2.7.8 类型检查与命令多态
- 2.7.9 内存回收
- 2.7.10 对象共享
- 2.7.11 对象空转时长
- 3 单机数据库的实现
- 3.1 数据库
- 3.1.1 服务端中的数据库
- 3.1.2 切换数据库
- 3.1.3 数据库键空间
- 3.1.4 过期键的处理
- 3.1.5 数据库通知
- 3.2 RDB持久化
- 操作系统
- 2021-01-08 Linux I/O 操作
- 2021-03-01 Linux 进程控制
- 2021-03-01 Linux 进程通信
- 2021-06-11 Linux 性能优化
- 2021-06-18 性能指标
- 2022-05-05 Android 系统源码阅读笔记
- Java基础
- 2020-07-18 Java 前端编译与优化
- 2020-07-28 Java 虚拟机类加载机制
- 2020-09-11 Java 语法规则
- 2020-09-28 Java 虚拟机字节码执行引擎
- 2020-11-09 class 文件结构
- 2020-12-08 Java 内存模型
- 2021-09-06 Java 并发包
- 代码性能
- 2020-12-03 Java 字符串代码性能
- 2021-01-02 ASM 运行时增强技术
- 理解Unsafe
- Java 8
- 1 行为参数化
- 1.1 行为参数化的实现原理
- 1.2 Java 8中的行为参数化
- 1.3 行为参数化 - 排序
- 1.4 行为参数化 - 线程
- 1.5 泛型实现的行为参数化
- 1.6 小结
- 2 Lambda表达式
- 2.1 Lambda表达式的组成
- 2.2 函数式接口
- 2.2.1 Predicate
- 2.2.2 Consumer
- 2.2.3 Function
- 2.2.4 函数式接口列表
- 2.3 方法引用
- 2.3.1 方法引用的类别
- 2.3.2 构造函数引用
- 2.4 复合方法
- 2.4.1 Comparator复合
- 2.4.2 Predicate复合
- 2.4.3 Function复合
- 3 流处理
- 3.1 流简介
- 3.1.1 流的定义
- 3.1.2 流的特点
- 3.2 流操作
- 3.2.1 中间操作
- 3.2.2 终端操作
- 3.3.3 构建流
- 3.3 流API
- 3.3.1 flatMap的用法
- 3.3.2 reduce的用法
- 3.4 collect操作
- 3.4.1 collect示例
- 3.4.2 Collector接口