在终端执行以下命令:
```shell
git checkout -b 09
```
在 `net.zhaoxuyang.ioc.aop.AdvisedSupport` 中插入以下内容:
```
private MethodMatcher methodMatcher;
public MethodMatcher getMethodMatcher() {
return methodMatcher;
}
public void setMethodMatcher(MethodMatcher methodMatcher) {
this.methodMatcher = methodMatcher;
}
```
修改 `net.zhaoxuyang.ioc.aop.TargetSource` 中的 `targetClass` 的类型:
```java
private final Class<?>[] targetClass;
public TargetSource( Object target, Class<?>... targetClass) {
this.target = target;
this.targetClass = targetClass;
}
public Class<?>[] getTargetClass() {
return targetClass;
}
```
新增 `net.zhaoxuyang.ioc.bean.BeanPostProcessor` :
```java
package net.zhaoxuyang.ioc.bean;
public interface BeanPostProcessor {
Object before(Object bean,String beanName) throws Exception;
Object after(Object bean,String beanName) throws Exception;
}
```
新增 `net.zhaoxuyang.ioc.bean.factory.BeanFactoryAware`:
```java
package net.zhaoxuyang.ioc.bean.factory;
public interface BeanFactoryAware {
void setBeanFactory(BeanFactory beanFactory) throws Exception;
}
```
在 `net.zhaoxuyang.ioc.bean.factory.AbstractBeanFactory` 中添加:
```java
private List<BeanPostProcessor> beanPostProcessors = new LinkedList<>();
protected Object initBean(Object bean, String name) throws Exception{
Object obj = bean;
for(BeanPostProcessor entry:beanPostProcessors){
obj = entry.before(bean, name);
}
for(BeanPostProcessor entry:beanPostProcessors){
obj = entry.after(bean, name);
}
return obj;
}
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) throws Exception {
this.beanPostProcessors.add(beanPostProcessor);
}
@SuppressWarnings("unchecked")
public List getBeansForType(Class<?> type) throws Exception{
List beans = new LinkedList<>();
for(String key:beanDefinitionNames){
if(type.isAssignableFrom(beanDefinitionMap.get(key).getBeanClass())){
beans.add(getBean(key));
}
}
return beans;
}
```
创建 `net.zhaoxuyang.ioc.aop.AspectJAroundAdvice` :
```java
package net.zhaoxuyang.ioc.aop;
import java.lang.reflect.Method;
import net.zhaoxuyang.ioc.aop.intercept.Advice;
import net.zhaoxuyang.ioc.aop.intercept.MethodInterceptor;
import net.zhaoxuyang.ioc.aop.intercept.MethodInvocation;
import net.zhaoxuyang.ioc.bean.factory.BeanFactory;
public class AspectJAroundAdvice implements Advice, MethodInterceptor{
private BeanFactory beanFactory;
private Method aspectJAdviceMethod;
private String aspectInstanceName;
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
return aspectJAdviceMethod.invoke(beanFactory.getBean(aspectInstanceName), invocation);
}
public BeanFactory getBeanFactory() {
return beanFactory;
}
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
public Method getAspectJAdviceMethod() {
return aspectJAdviceMethod;
}
public void setAspectJAdviceMethod(Method aspectJAdviceMethod) {
this.aspectJAdviceMethod = aspectJAdviceMethod;
}
public String getAspectInstanceName() {
return aspectInstanceName;
}
public void setAspectInstanceName(String aspectInstanceName) {
this.aspectInstanceName = aspectInstanceName;
}
}
```
修改 `net.zhaoxuyang.ioc.aop.JdkDynamicAopProxy` :
```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(),
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();
if(advised.getMethodMatcher() != null
&& advised.getMethodMatcher().matches(method, target.getClass())){
return interceptor.invoke(new ReflectiveMethodInvocation(target, method, args));
}else {
return method.invoke(target, args);
}
}
}
```
新增 `net.zhaoxuyang.ioc.aop.AspectAwareAdvisorAutoProxyCreator`:
```java
package net.zhaoxuyang.ioc.aop;
import java.util.List;
import net.zhaoxuyang.ioc.aop.intercept.MethodInterceptor;
import net.zhaoxuyang.ioc.bean.BeanPostProcessor;
import net.zhaoxuyang.ioc.bean.factory.AbstractBeanFactory;
import net.zhaoxuyang.ioc.bean.factory.BeanFactory;
import net.zhaoxuyang.ioc.bean.factory.BeanFactoryAware;
public class AspectAwareAdvisorAutoProxyCreator implements BeanPostProcessor, BeanFactoryAware{
private AbstractBeanFactory beanFactory;
@Override
public Object before(Object bean, String beanName) throws Exception {
return bean;
}
@Override
public Object after(Object bean, String beanName) throws Exception {
if(bean instanceof AspectJExpressionPointcutAdvisor){
return bean;
}
if(bean instanceof MethodInterceptor){
return bean;
}
List<AspectJExpressionPointcutAdvisor> advisors = beanFactory
.getBeansForType(AspectJExpressionPointcutAdvisor.class);
for(AspectJExpressionPointcutAdvisor advisor:advisors){
if(advisor.getPointcut().getClassFilter().matches(bean.getClass())){
AdvisedSupport advisedSupport = new AdvisedSupport();
advisedSupport.setMethodInterceptor((MethodInterceptor)advisor.getAdvice());
advisedSupport.setMethodMatcher(advisor.getPointcut().getMethodMatcher());
TargetSource targetSource = new TargetSource(bean,bean.getClass().getInterfaces());
advisedSupport.setTargetSource(targetSource);
return new JdkDynamicAopProxy(advisedSupport).getProxy();
}
}
return bean;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws Exception {
this.beanFactory = (AbstractBeanFactory) beanFactory;
}
}
```
修改 `net.zhaoxuyang.ioc.context.AbstractApplicationContext`:
```java
package net.zhaoxuyang.ioc.context;
import java.util.List;
import net.zhaoxuyang.ioc.bean.BeanDefinition;
import net.zhaoxuyang.ioc.bean.BeanPostProcessor;
import net.zhaoxuyang.ioc.bean.factory.AbstractBeanFactory;
public abstract class AbstractApplicationContext implements ApplicationContext {
protected AbstractBeanFactory beanFactory;
public AbstractApplicationContext(AbstractBeanFactory beanFactory){
this.beanFactory = beanFactory;
}
public void refresh() throws Exception{
loadBeanDefinitions(beanFactory);
registerBeanPostProcessor(beanFactory);
onRefresh();
}
protected abstract void loadBeanDefinitions(AbstractBeanFactory beanFactory) throws Exception;
protected void registerBeanPostProcessor(AbstractBeanFactory beanFactory) throws Exception {
List<?> beanPostProcessors = beanFactory.getBeansForType(BeanPostProcessor.class);
for(Object obj:beanPostProcessors){
beanFactory.addBeanPostProcessor((BeanPostProcessor)obj);
}
}
protected void onRefresh() throws Exception{
beanFactory.preInstantiateSingletons();
}
@Override
public void registerBeanDefinition(String name, BeanDefinition bd) throws Exception {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public Object getBean(String name) throws Exception {
return beanFactory.getBean(name);
}
}
```
修改 `net.zhaoxuyang.ioc.context.ClassPathXmlApplicationContext`:
```java
package net.zhaoxuyang.ioc.context;
import java.util.Map;
import net.zhaoxuyang.ioc.bean.BeanDefinition;
import net.zhaoxuyang.ioc.bean.factory.AbstractBeanFactory;
import net.zhaoxuyang.ioc.bean.factory.AutowireCapableBeanFactory;
import net.zhaoxuyang.ioc.bean.io.ResourceLoader;
import net.zhaoxuyang.ioc.bean.xml.XmlBeanDefinitionReader;
public class ClassPathXmlApplicationContext extends AbstractApplicationContext{
private String configLocation;
public ClassPathXmlApplicationContext(String configLocation) throws Exception{
this(configLocation, new AutowireCapableBeanFactory());
}
public ClassPathXmlApplicationContext(String configLocation,AbstractBeanFactory beanFactory)
throws Exception{
super(beanFactory);
this.configLocation = configLocation;
refresh();
}
public void refresh() throws Exception{
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(new ResourceLoader());
reader.loadBeanDefinitions(configLocation);
for(Map.Entry<String,BeanDefinition> entry:reader.getRegistry().entrySet()){
beanFactory.registerBeanDefinition(entry.getKey(), entry.getValue());
}
}
@Override
protected void loadBeanDefinitions(AbstractBeanFactory beanFactory) throws Exception {
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(new ResourceLoader());
reader.loadBeanDefinitions(configLocation);
for (Map.Entry<String, BeanDefinition> entry : reader.getRegistry().entrySet()) {
beanFactory.registerBeanDefinition(entry.getKey(), entry.getValue());
}
}
}
```
在终端输入以下命令:
```shell
$ git add .
$ git commit -m 'auto-create-aop-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接口