使用 BeanPostProcessor,ImportSelector,Jdk Proxy,Cglib Proxy 模拟实现乞丐版Spring AOP

实现简易版Aop

使用 BeanPostProcessor,ImportSelector,Jdk Proxy,Cglib Proxy 模拟实现

EnableMockAop 为自己实现的注解,作为Aop开启与关闭的按钮

@Configuration
@ComponentScan("com.popcivilar.code.spring.mockaop")
@EnableMockAop
public class AppConfig {

}

AopImportSelector 引入 BeanPostProcessor的实现类

@Retention(RetentionPolicy.RUNTIME)
@Import(AopImportSelector.class)
public @interface EnableMockAop {
}
public class AopImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        return new String[]{AopBeanPostProcessor.class.getName()};
    }
}
public class AopBeanPostProcessor implements BeanPostProcessor , MethodInterceptor, InvocationHandler {

    private Object target; //jdk 代理时,目标对象

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return null;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        //0.扫描得到需要被代理的类,否则return


        if(bean.getClass().getInterfaces().length > 0 || bean.getClass().isInterface()){
            this.target = bean;
            return Proxy.newProxyInstance(this.getClass().getClassLoader(),
                    bean.getClass().getInterfaces(),
                    this);
        }else{
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(bean.getClass());
            enhancer.setCallback(this);
            return enhancer.create();
        }

    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("cglib proxy before");
        methodProxy.invokeSuper(o,objects);
        System.out.println("cglib proxy after");
        return o;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("jdk proxy before");
        method.invoke(target,args);
        System.out.println("jdk proxy after");
        return proxy;
    }
}

没有实现接口的bean

@Service
public class UserDao2 {

    public void list(){
        System.out.println(1123333);
    }
}

实现接口的bean

@Service
public class Apple implements Fruit {
    @Override
    public void eat() {
        System.out.println("apple");
    }
}

测试函数

 public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext =
                new AnnotationConfigApplicationContext();
        applicationContext.register(AppConfig.class);
        applicationContext.refresh();
        UserDao2 o = (UserDao2) applicationContext.getBean("userDao2");
        o.list();
        Fruit apple = (Fruit) applicationContext.getBean("apple");
        apple.eat();

    }

开启@EnableMockAop
使用 BeanPostProcessor,ImportSelector,Jdk Proxy,Cglib Proxy 模拟实现乞丐版Spring AOP_第1张图片

去掉@EnableMockAop
使用 BeanPostProcessor,ImportSelector,Jdk Proxy,Cglib Proxy 模拟实现乞丐版Spring AOP_第2张图片

切面,切点的模拟,日后完善。

你可能感兴趣的:(Spring)