zoukankan      html  css  js  c++  java
  • Spring框架之AOP

    SpringAop:
    1.加入 jar 包
    com.springsource.org.aopalliance-1.0.0.jar
    com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
    spring-aop-4.0.0.RELEASE.jar
    spring-aspects-4.0.0.RELEASE.jar
     
    commons-logging-1.1.1.jar
    spring-beans-4.0.0.RELEASE.jar
    spring-context-4.0.0.RELEASE.jar
    spring-core-4.0.0.RELEASE.jar
    spring-expression-4.0.0.RELEASE.jar
     
    2.在配置文件中加入aop 命名空间.
     
    3.基于注解的方式来配置AOP
    1)在配置文件中加入如下配置:
    <context:component-scan base-package="com.mmz.spring.aop"></context:component-scan>
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
     
    2)把横切关注点的代码抽象到切面中,切面首先是一个IOC容器的 bean ,即加入@Component 注解,切面还需要加入 @Aspect 注解,(可以使用@Order 指定切面的优先级)
     
    3)在类中声明各种通知注解(@Before,@After,@AfterRunning,@AfterThrowing,@Around)
    a.在方法前声明注解,注解里面添加切入点表达式:在 AspectJ 中, 切入点表达式可以通过操作符 &&, ||, ! 结合起来,方法可以添加参数 JoinPoint joinPoint,然后能间接访问链接细节
    b.eg.
    @Before("execution(public int com.mmz.spring.aop.ArithmeticCalculator.*(int, int))")
    public void beforeMethod(JoinPoint joinPoint){
        String methdName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("The method "+methdName+" begins width"+args);
    }
     
    在方法返回结果之后执行:
    @AfterReturning(value="execution(public int com.mmz.spring.aop.*.*(int, int))",returning="result")
    public void afterRunningMethod(JoinPoint joinPoint,Object result){
        String methdName = joinPoint.getSignature().getName();
        System.out.println("The method "+methdName+" ends width "+result);
    }
    

      

     
    异常通知,在方法抛出异常之后
    @AfterThrowing(value="execution(public int com.mmz.spring.aop.*.*(int, int))",throwing="ex")
    public void afterRunningMethod(JoinPoint joinPoint,Exception ex){
        String methdName = joinPoint.getSignature().getName();
        System.out.println("The method "+methdName+" ends width "+ex);
    }
     
    重用切入点表达式:
    1.定义一个方法,用于声明切入点表达式,该方法中不需要添加其他代码
    2.使用@PointCut 来声明切入点表达式
    3.后面的其他通知直接使用方法名来使用当前的切入点表达式.
     
    4.基于配置文件的方式来配置AOP
    1.<!-- 配置业务层的bean -->
    <bean id="arithmeticCalculator" class="com.mmz.spring.aop.xml.ArithmeticCalculatorImpl"></bean>
     
    2.<!-- 配置切面的bean -->
    <bean id="loggingAspect" class="com.mmz.spring.aop.xml.LoggingAspect"></bean>
     
    <bean id="validationAspect" class="com.mmz.spring.aop.xml.ValidationAspect"></bean>
     
    3.<!-- 配置AOP-->
    <aop:config>
    <!-- 配置切入点表达式 -->
    <aop:pointcut expression="execution(* com.mmz.spring.aop.xml.*.*(int, int))" id="pointcut"/>
    <!-- 配置切面及通知 -->
    <aop:aspect ref="loggingAspect" order="2">
    <aop:before method="beforeMethod" pointcut-ref="pointcut"/>
    <aop:after method="afterMethod" pointcut-ref="pointcut"/>
    <aop:after-returning method="afterRunningMethod" pointcut-ref="pointcut" returning="result"/>
    <aop:after-throwing method="afterThrowingMethod" pointcut-ref="pointcut" throwing="ex"/>
    </aop:aspect>
     
    <aop:aspect ref="validationAspect" order="1">
    <aop:before method="validateMethod" pointcut-ref="pointcut"/>
    </aop:aspect>
    </aop:config>
  • 相关阅读:
    npm ci命令解析
    performance分析
    mpvue 引入直播功能
    lodash按需加载
    React生命周期分析
    vue 项目打包优化(远不止dll)
    Git 底层数据结构和原理
    Docker 部署 Nuxt.js 项目
    TMS320DM642调试出现#10247-D creating output section ".capChaACrSpace" without a SECTIONS 解决办法
    TMS320DM642的emif(外部存储器接口)的结构设置
  • 原文地址:https://www.cnblogs.com/xiaoming0601/p/5865326.html
Copyright © 2011-2022 走看看