Spring AOP 前置通知

    我们使用AspectJ对Spring进行AOP操作,有两种方式,注解和XML配置方式,先在pom.xml中声明jar包

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.1.5.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.1.5.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.1.5.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>5.1.5.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.1</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.37</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
    <dependency>
      <groupId>aopalliance</groupId>
      <artifactId>aopalliance</artifactId>
      <version>1.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.1.5.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>5.1.5.RELEASE</version>
    </dependency>

  </dependencies>
View Code

    AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果。

    整完之后,加一个配置文件,先来创建一个新的接口和类作为计算器的基本功能,

Calculator.java

package com.fig.aop.impl;

public interface Calculator {

    int add(int i,int j);
    int sub (int i,int j);
    int mul (int i,int j);
    int div (int i,int j);
}
View Code

CalculatorImpl.java

package com.fig.aop.impl;



public class CalculatorImpl implements Calculator {
    @Override
    public int add(int i, int j) {
        return i + j;
    }

    @Override
    public int sub(int i, int j) {
        return i - j;
    }

    @Override
    public int mul(int i, int j) {
        return i * j;
    }

    @Override
    public int div(int i, int j) {
        return i / j;
    }
}
View Code

    我们先配置自动扫描的包,来到配置文件:

    <context:component-scan base-package="com.fig.aop.impl"/>
				

    接下来在实现类上加注解@Component:

    然后写个Main.java作为测试类,接下来就是走前几讲说的流程:

  1. 通过配置文件,创建Spring的IOC容器
  2. 从IOC容器中获取bean实例
  3. 使用bean

 

接着我们需要给计算器加一个日志功能:

 

创建一个日志切面类LoggingAspect.java,先写一个前置方法:

接着将这个类声明为一个切面:

  1. 需要把该类放入到IOC容器中,然后再声明为一个切面
  2. 声明方法为前置通知,在目标方法开始之前执行
  3. 在配置文件中加入<aop:aspectj-autoproxy>标签,自动为匹配类生成代理对象

看看运行结果:

如果我希望前置通知里面包含着参数信息,该怎么办?这里需要运用JoinPoint连接点类型参数:

 

在声明前置方法时,我们只声明了加法,如果我想声明该类下的所有方法,就该使用通配符:

也可以将限定标志用通配符进行替换:

 

 

先做一个小结:

  1. 加入jar包
  2. 在配置文件中加入aop的命名空间,IDEA可以自动帮我们完成这步

然后说说基于注解的方式配置:

  1. 在配置文件里加入<aop:aspectj-autoproxy>标签
  2. 把横切关注点地代码抽象到切面类中
    1. 切面首先是一个IOC中的bean,即加入@Component注解
    2. 切面还需要加入@Aspect
  3. 在类中声明各种通知
    1. 声明有个方法
    2. 在方法前加入@Before注解(前置)
  4. 可以通过参数JoinPoint访问链接细节,如方法名称和参数值

猜你喜欢

转载自www.cnblogs.com/figsprite/p/10783651.html
今日推荐