Aop(面向切面编程)

  1. 使用注解的方式:

    1. 加入相应的jar包:

      1. com.springsource.org.aopalliance-1.0.0.jar
      2. com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
      3. sping-aspects-4.3.3.RELEASE.jar
    2. 创建一个切面类

      package com.alibaba.wlq.invoke;
      
      import java.util.Arrays;
      
      import org.aspectj.lang.JoinPoint;
      import org.aspectj.lang.annotation.After;
      import org.aspectj.lang.annotation.Aspect;
      import org.aspectj.lang.annotation.Before;
      import org.springframework.stereotype.Component;
      //@Aspect:表示该类为切面类
      @Aspect
      //@Component:表示该类由Spring管理
      @Component
      public class logAspect {
      //@Before:表示before方法在被通知的程序类的方法执行之前执行
      //execution():表示需要被通知的类中的add方法,add方法可以用通配符表示,那就表示该类中所有的方法都要被通知
      @Before(value="execution(* com.alibaba.wlq.invoke.ArithmeticImp.add(double,double))")
      //JoinPoint:是程序执行中的一个精确执行点,例如类中的一个方法。它是一个抽象的概念,在实现AOP时,并不需要去定义一个join point
      public void before(JoinPoint joinpoint) {
      //获得被通知程序类的参数,返回类型是一个数组
      Object[] args = joinpoint.getArgs();
      //获取被通知程序类的方法名,返回类型是字符串
      String name = joinpoint.getSignature().getName();
      System.out.println("alibaba------>the method "+name+" begin with"+Arrays.asList(args));
      } //@After:后置通知,表示在类的方法执行后、类中的return方法执行前执行
      @After(value="execution(* com.alibaba.wlq.invoke.ArithmeticImp.add(double,double))")
      public void after() {
      System.out.println("alibaba------>the method *** end result");
      }
      }
    3. 创建被通知的类的接口

      package com.alibaba.wlq.invoke;
      
      public interface Arithmetic {
      public double add(double a,double b);
      }
    4. 创建被通知的类并且继承接口重写其中的方法

      package com.alibaba.wlq.invoke;
      
      import org.springframework.stereotype.Component;
      
      @Component
      public class ArithmeticImp implements Arithmetic{ @Override
      public double add(double a, double b) {
      double result = a + b;
      System.out.println(result);
      return result;
      } }
    5. 配置文件中的配置

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <!-- 包扫描,前提加入aop的jar包 -->
      <context:component-scan base-package="com.alibaba.wlq"></context:component-scan>
      <!-- 开启切面注解 -->
      <aop:aspectj-autoproxy/>
      </beans>
    6. 测试类

      package com.alibaba.wlq.test;
      
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext; import com.alibaba.wlq.controller.UsersController;
      import com.alibaba.wlq.invoke.Arithmetic;
      import com.alibaba.wlq.invoke.ArithmeticImp; public class Test2 { public static void main(String[] args) {
      // TODO Auto-generated method stub
      ApplicationContext app = new ClassPathXmlApplicationContext("app3.xml");
      Arithmetic arith = (Arithmetic) app.getBean("arithmeticImp");
      arith.add(15.0, 20.0);
      }
      }
    7. 打印结果

      alibaba------>the method add begin with[15.0, 20.0]
      35.0
      alibaba------>the method *** end result
  2. 使用xml配置文件

    1. 导入相应的jar包

    2. 创建接口并且创建相应的类实现该接口

      package com.alibaba.wlq.invoke;
      
      public interface Metic {
      public double add(double a,double b);
      public double sub(double a,double b);
      public double mul(double a,double b);
      public double div(double a,double b);
      }
      package com.alibaba.wlq.invoke;
      
      public class MeticImp implements Metic{
      
      	@Override
      public double add(double a, double b) {
      double result = a + b;
      System.out.println(result);
      return result;
      } @Override
      public double sub(double a, double b) {
      double result = a - b;
      System.out.println(result);
      return result;
      } @Override
      public double mul(double a, double b) {
      double result = a * b;
      System.out.println(result);
      return result;
      } @Override
      public double div(double a, double b) {
      double result = a / b;
      System.out.println(result);
      return result;
      } }
    3. 创建切面类

      package com.alibaba.wlq.invoke;
      
      import java.util.Arrays;
      
      import org.aspectj.lang.JoinPoint;
      
      public class Invoke {
      public void before(JoinPoint joinPoint) {
      Object[] args = joinPoint.getArgs();
      String name = joinPoint.getSignature().getName();
      System.out.println("alibaba------>the method "+name+" begin with "+Arrays.asList(args));
      }
      public void after(JoinPoint joinPoint) {
      String name = joinPoint.getSignature().getName();
      System.out.println("alibaba------>the method "+name+" end result:");
      }
      public void afterreturn(Object result) {
      System.out.println("result:========"+result);
      }
      }
    4. 配置文件

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <!-- 定义被通知的程序类 -->
      <bean id="metic" class="com.alibaba.wlq.invoke.MeticImp"></bean> <!-- 定义切面类的Bean -->
      <bean id="invoke" class="com.alibaba.wlq.invoke.Invoke"></bean> <!-- 配置切面 -->
      <aop:config>
      <!-- 定义表达式、切点 -->
      <aop:pointcut expression="execution(* com.alibaba.wlq.invoke.*.*(..))" id="point"/>
      <!-- 定义切面 -->
      <aop:aspect ref="invoke">
      <!-- 定义前置通知 -->
      <aop:before method="before" pointcut-ref="point" />
      <!-- 定义后置通知 -->
      <aop:after method="after" pointcut-ref="point"/>
      <!-- 定义返回通知returning属性值得名称要和方法中的参数名称一致 -->
      <aop:after-returning method="afterreturn" pointcut-ref="point" returning="result"/>
      </aop:aspect>
      </aop:config>
      </beans>
    5. 测试类

      package Test;
      
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext; import com.alibaba.wlq.invoke.Metic; public class Test1 {
      public static void main(String[] args) {
      ApplicationContext app = new ClassPathXmlApplicationContext("app.xml");
      Metic arith = (Metic) app.getBean("metic");
      arith.add(15, 10);
      }
      }
    6. 输出结果

      alibaba------>the method add begin with [15.0, 10.0]
      25.0
      alibaba------>the method add end result:
      result:========25.0

AOP面向切面编程(使用注解和使用配置文件)的更多相关文章

  1. Spring AOP 面向切面编程相关注解

    Aspect Oriented Programming 面向切面编程   在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业.   ...

  2. Spring -07 -AOP [面向切面编程] - 使用注解@+ AspectJ 方式实现环绕/前/后等通知 -超简洁 --静态代理/动态代理{JDK/cglib}

    1.spring 不会自动去寻找注解,必须告诉 spring 哪些包下的类中可能有注解;使用注解来取代配置文件.1.1 引入xmlns:context ,指定扫描范围 <context:comp ...

  3. spring:AOP面向切面编程(注解)03

    使用注解写aop时最好使用环绕通知写 切面类: /** * 用于记录日志的工具类,它里面提供了公共的代码 */ @Component("logger") @Aspect //表示当 ...

  4. AOP面向切面编程的四种实现

     一.AOP(面向切面编程)的四种实现分别为最原始的经典AOP.代理工厂bean(ProxyFacteryBean)和默认自动代理DefaultAdvisorAutoProxyCreator以及Bea ...

  5. Spring:AOP面向切面编程

    AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果. AOP是软件开发思想阶段性的产物,我们比较熟悉面向过程O ...

  6. Spring Boot2(六):使用Spring Boot整合AOP面向切面编程

    一.前言 众所周知,spring最核心的两个功能是aop和ioc,即面向切面和控制反转.本文会讲一讲SpringBoot如何使用AOP实现面向切面的过程原理. 二.何为aop ​ aop全称Aspec ...

  7. 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~

    简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...

  8. 谈一谈AOP面向切面编程

    AOP是什么 : AOP面向切面编程他是一种编程思想,是指在程序运行期间,将某段代码动态的切入到指定方法的指定位置,将这种编程方式称为面向切面编程 AOP使用场景 : 日志 事务 使用AOP的好处是: ...

  9. 基于SpringBoot AOP面向切面编程实现Redis分布式锁

    基于SpringBoot AOP面向切面编程实现Redis分布式锁 基于SpringBoot AOP面向切面编程实现Redis分布式锁 基于SpringBoot AOP面向切面编程实现Redis分布式 ...

  10. 极简SpringBoot指南-Chapter05-SpringBoot中的AOP面向切面编程简介

    仓库地址 w4ngzhen/springboot-simple-guide: This is a project that guides SpringBoot users to get started ...

随机推荐

  1. [日常摸鱼]bzoj4802 欧拉函数-PollardRho大整数分解算法

    啊居然要特判,卡了好久QAQ (好像Windows下的rand和Linux下的不一样? QwQ一些东西参考了喵铃的这篇blog:http://www.cnblogs.com/meowww/p/6400 ...

  2. Helm 带你飞

    文章目录 目录 文章目录 在没使用 Helm之前,向 K8S部署应用,我们要依次部署 deployment. svc 等,步骤较繁琐.况且随着很多项目微服务化,复杂的应用在容器中部署以及管理显得较为复 ...

  3. Spring Cloud Alibaba基础教程-Nacos(一)

    2019快结束,也有很久没写博客了,今天我们来谈谈Nacos,如果对您有帮助,麻烦左上角点个关注 ,谢谢 ! 嘻嘻 今天先写第一篇 文章目录 为什么要使用Nacos Eureka 闭源 Nacos的优 ...

  4. .NET Core 使用MediatR CQRS模式 读写分离

    前言 CQRS(Command Query Responsibility Segregation)命令查询职责分离模式,它主要从我们业务系统中进行分离出我们(Command 增.删.改)和(Query ...

  5. python基本输入与输出

    内置函数print()用于输出信息到标准控制台或指定文件,语法格式为: print(value1,value2,... , sep=' ', end='\n', file=sys.stdout, fl ...

  6. umi-request 统一异常处理实践

    首发于语雀文档 前言 本人在工作中用到了 umi-request,百度谷歌搜了一遍,感觉都没找到超过 3 篇合适且含代码的文章,因此只能自行实践总结了. umi-request 有点不同 umi-re ...

  7. 2021年第一个flag

    2021年开始更新本文列出的系列文章,根据书籍和自己的理解整理出spring框架的相关的学习 Spring 的设计理念和整体架构 学习目标 Spring的各个子项目 Spring的设计目标 Sprin ...

  8. Spring Data Cache

    Spring Data Cache注解使用 方法是一个对象,使用对象中的属性,如ID,作为cache key RedisConnection超时设置 参考 SpringBoot使用Redis缓存

  9. dp的冗余(选数类)

    我们先来看一个例题: 在一个长度为n的序列中选出任意个数的数,要求每m个数中至少一个被选,要求选的数之和最小化. 我们很容易想出用f[i][j]来表示前i个数选的最后一个数是j,也就有 for(int ...

  10. vue-cli Cannot find module 'less'

    首先,今天下午对OneloT项目进行试图修改,我向在在线的时候先将接口中的数据缓存到本地,通过文件写入的方式,但是没有成功,捣鼓了一会.重新启动浏览器,发现命令行报错,error cannot fin ...