zoukankan      html  css  js  c++  java
  • Spring-Aop梳理

    org.springframework.aop.config.internalAutoProxyCreator
    -->AnnotationAwareAspectJAutoProxyCreator
    
    AnnotationAwareAspectJAutoProxyCreator 重写了initBeanFactory
    	AspectJAwareAdvisorAutoProxyCreator
    		AbstractAdvisorAutoProxyCreator (重写了setbeanFactory)
    			AbstractAutoProxyCreator
    				AbstractAutoProxyCreator 
    					-->implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware
    


    Aop原理就是:首先通过@EnableAspectJAutoProxy注册了AnnotationAwareAspectJAutoProxyCreator类型的组件

    然后这个组件在前后会拦截创建,也就是在创建前,就是实例化前,会判断,如果被拦截下来,使用动态代理创建,否则再使用基本的反射创建。

    动态代理创建完成后,对象就被包装成了增强对象,这个对象的这个方法就具有切面的功能了。

    最最最最清晰讲解:https://www.bilibili.com/video/BV1ME411o7Uu?p=35

    具体的后面切面顺序,首先获取这些代理对象的增强器,形成chain链,然后调用process,反射调用方法,递归调用,然后层层返回,最后执行完成。

    具体使用:

    package com.nxj.aop;
    
    /**
     * @author ningxinjie
     * @date 2021/1/17
     */
    public class AopTest {
        public void myAopTestMethod(){
            System.out.println("myAopTestMethod~~~");
        }
    }
    
    package com.nxj.aop;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    
    /**
     * @author ningxinjie
     * @date 2021/1/17
     */
    @Aspect
    public class LogAspects {
    
        @Pointcut("execution(public * com.nxj.aop.AopTest.myAopTestMethod())")
        public void pointCut(){};
    
        @Before(value = "execution(public * com.nxj.aop.AopTest.myAopTestMethod())")
        public void logBefore(JoinPoint joinPoint){
            System.out.println("logBefore" + joinPoint.getArgs().length);
        }
    
        @After("pointCut()")
        public void logAfter(){
            System.out.println("logAfter");
        }
    
        @AfterReturning("pointCut()")
        public void logReturn(){
            System.out.println("logReturn");
        }
    
        @AfterThrowing("pointCut()")
        public void logThrowing(){
            System.out.println("logThrowing");
        }
    
        @Around("pointCut()")
        public void logAround(ProceedingJoinPoint point) throws Throwable {
            System.out.println("logAround around之前");
            point.proceed();
            System.out.println("logAround around之后");
        }
    }
    
    package com.nxj.config;
    
    import com.nxj.aop.AopTest;
    import com.nxj.aop.LogAspects;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.EnableAspectJAutoProxy;
    
    /**
     * @author ningxinjie
     * @date 2021/1/17
     */
    @Configuration
    @EnableAspectJAutoProxy
    public class MyAopConfig {
        @Bean
        public AopTest aopTest(){
            return new AopTest();
        }
    
        @Bean
        public LogAspects logAspects(){
            return new LogAspects();
        }
    
    }
    

    测试:

    /**
     * @author ningxinjie
     * @date 2021/1/17
     */
    public class MyAopTest {
        @Test
        public void aopTest(){
            ApplicationContext context = new AnnotationConfigApplicationContext(MyAopConfig.class);
            AopTest aopTest = context.getBean("aopTest", AopTest.class);
            aopTest.myAopTestMethod();
        }
    }
    

    结果:

    logAround around之前
    logBefore0
    myAopTestMethod~~~
    logAround around之后
    logAfter
    logReturn
    
  • 相关阅读:
    没有插件的sublime编辑器是没有灵魂的
    原生JS简单的无缝自动轮播
    自学前端的日子,记录我的秃头之旅
    简洁快速的数组去重
    最困难的是带着自己的选择生活下去
    css画图那些事
    css3画图那些事(三角形、圆形、梯形等)
    SVN那些事
    关于使用JQ scrollTop方法进行滚动定位
    linux下mysql出现Access denied for user 'root'@'localhost' (using password: YES)解决方法
  • 原文地址:https://www.cnblogs.com/ningxinjie/p/14289184.html
Copyright © 2011-2022 走看看