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
    
  • 相关阅读:
    SharePoint 2010 At Work--SharePoint 2010 Tab Page 创建选项卡页面
    SharePoint At Work----Hyperlinks in the Data View Web Part
    SharePoint At Work----SharePoint Data View Web Part
    SharePoint 2010 品牌化和自定义--母版页
    面对复杂的或者高并发的或者海量数据的问题
    提升算力的程序设计
    关于方案,关于设计,关于思考
    关于测试
    数据资源管理程序的功能以及设计的总结
    如何做软件设计
  • 原文地址:https://www.cnblogs.com/ningxinjie/p/14289184.html
Copyright © 2011-2022 走看看