AspectJ对AOP的实现

本文介绍了AspectJ如何实现AOP,相较于Spring的AOP,AspectJ更简单且支持注解开发。AspectJ提供了五种通知类型:前置、后置、环绕、异常和最终通知。同时,文章详细阐述了AspectJ的切入点表达式、开发环境配置以及基于注解的AOP实现步骤,包括定义接口、切面类、目标对象和测试。最后,讲解了各种通知类型的使用,如@Before、@AfterReturning、@Around等。
摘要由CSDN通过智能技术生成

引言

1.对于AOP编程思想,很多框架都进行了实现,Spring就是其中之一,可以完成面向切面编程。然后,AspectJ也实现了AOP的功能。并且实现的更加简单、方便,并且还支持注解式开发。所以,spring又将AspectJ的对于AOP的实现也引入到了自己的框架中。

2.在Spring中使用AOP开发的时候,一般使用AspectJ的实现方式。

3.AspectJ是一个优秀的面向切面的框架,它扩展了java语言,提供了强大的切面实现,它是Eclipse的一个开源项目。

2.AspectJ的通知类型

AspectJ中常用的通知有五种类型:
1.前置通知@Before
2.后置通知@AfterReturning
3.环绕通知@Arround
4.异常通知@After Throwing
5.最终通知@After

3.AspectJ的切入点表达式

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.准备AspectJ的开发环境

1.加入Maven依赖


```xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.bjpowernode</groupId>
  <artifactId>ch06-aop-aspectj</artifactId>
  <version>1.0-SNAPSHOT</version>


  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!--spring依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>
    <!--aspectj依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.1</version>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

2.引入AspectJ依赖

在这里插入图片描述

4.AspectJ基于注解的AOP实现

实现步骤
a.定义业务接口和实现类

public interface SomeService {
   
    void doSome(String name,Integer age);
}

//目标类  厂家
public class SomeServiceImpl implements SomeService {
   
    @Override
    public void doSome(String name,Integer age) {
   
        //给doSome方法增加一个功能,在doSome()执行之前, 输出方法的执行时间
        System
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值