注解方式实现AOP

简介: 注解方式实现AOP

在这里插入图片描述

✨博客主页:👉 不会压弯的小飞侠
✨欢迎关注:👉点赞👍收藏⭐留言✒
✨系列专栏:👉 spring专栏
✨如果觉得博主的文章还不错的话,请三连支持一下博主。 ✨欢迎大佬指正,一起学习!一起加油!

@TOC


一、注解实现AOP快速入门

  • 基于注解的aop开发步骤:

    • 创建目标接口和目标类(内部有切点)
    • 创建切面类(内部有增强方法)
    • 将目标类和切面类的对象创建权交给spring
    • 在切面类中使用注解配置织入关系
    • 在配置文件中开启组件扫描和AOP的自动代理
    • 测试

案例:

1.导入AOP相关坐标

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.9.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

2.创建目标接口

package com.study.proxy.aop;

public interface TargetInterface {
    void save();
}

3.创建目标类

package com.study.proxy.anno;

import org.springframework.stereotype.Component;

@Component("target")
public class Target implements TargetInterface{
    public void save() {
        System.out.println("TargetImpl....");
    }
}

4.创建切面类

package com.study.proxy.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component("myAspect")
@Aspect  //标注当前MyAspect是一个切面类
public class MyAspect {
    //配置前置通知
   /* @Before("execution(* com.study.proxy.anno.*.*(..))")*/
    @Before("pointcut()")
    public void before(){
        System.out.println("前置增强.....");
    }
    /*@AfterReturning("execution(* com.study.proxy.anno.*.*(..))")*/
    @AfterReturning("pointcut()")
    public void afterReturning(){
        System.out.println("后置增强.....");
    }
   /* @Around("execution(* com.study.proxy.anno.*.*(..))")*/
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强.....");
        //切点方法
        Object proceed = pjp.proceed();
        System.out.println("环绕后增强.....");
        return proceed;
    }
    //定义切点表达式
    @Pointcut("execution(* com.study.proxy.anno.*.*(..))")
    public void pointcut(){

    }
}

5.配置applicationContext-anno.xml

<?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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
   <!--组件扫描-->
   <context:component-scan base-package="com.study.proxy.anno"></context:component-scan>
   <!--自动代理-->
   <aop:aspectj-autoproxy></aop:aspectj-autoproxy>


</beans>

6.测试类

package com.study.proxy.anno;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-anno.xml")
public class AnnoTest {
    @Autowired
    private TargetInterface targetInterface;
    @Test
    public void test(){
        targetInterface.save();

    }

}
/*
环绕前增强.....
前置增强.....
TargetImpl....
后置增强.....
环绕后增强.....
 */

二、注解AOP开发详解

1.注解通知类型

  • 通知的配置语法:

    • @通知注解(“切点表达式")
  • 前置通知

    • @Before
    • 用于配置前置通知。指定增强的方法在切入点方法之前执行
  • 后置通知

    • @AfterReturning
    • 用于配置后置通知。指定增强的方法在切入点方法之后执行
  • 环绕通知

    • @Around
    • 用于配置环绕通知。指定增强的方法在切入点方法之前和之后都执行
  • 异常抛出通知

    • @AfterThrowing
    • 用于配置异常抛出通知。指定增强的方法在出现异常时执行
  • 最终通知

    • @After
    • 用于配置最终通知。无论增强方式执行是否有异常都会执行
@Before("execution(* com.study.proxy.anno.*.*(..))")

2.切点表达式抽取

  • 同xml配置aop一样,我们可以将切点表达式抽取。抽取方式是在切面内定义方法,在该方法上使用@Pointcut注解定义切点表达式,然后在在增强注解中进行引用。

1.定义切点表达式

 //定义切点表达式
    @Pointcut("execution(* com.study.proxy.anno.*.*(..))")
    public void pointcut(){

    }

2.配置通知

 @Before("pointcut()")

3.注意

注解aop开发步骤
使用@Aspect标注切面类
使用@通知注解标注通知方法
在配置文件中配置aop自动代理<aop :aspectj-autoproxy/>通知注解类型

相关文章
|
6月前
|
Java 编译器 数据安全/隐私保护
自定义注解与AOP结合使用
自定义注解与AOP结合使用
59 0
|
7月前
|
Java 数据库连接 数据库
MyBatis与Spring集成&常用注解以及AOP和PageHelper分页插件整合
MyBatis与Spring集成&常用注解以及AOP和PageHelper分页插件整合
53 0
|
7月前
|
Java Spring
11Spring - 基于AspectJ的AOP开发(注解的方式)
11Spring - 基于AspectJ的AOP开发(注解的方式)
26 0
|
7月前
|
Java Spring
【注解】Spring AOP 面向切面编程之@Around的详细用法
【注解】Spring AOP 面向切面编程之@Around的详细用法
318 0
|
7天前
|
缓存 Java Sentinel
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
|
2天前
|
安全
自定义注解,aop实现注解锁
自定义注解,aop实现注解锁
|
2天前
|
Java 测试技术 开发者
【亮剑】如何通过自定义注解来实现 Spring AOP,以便更加灵活地控制方法的拦截和增强?
【4月更文挑战第30天】通过自定义注解实现Spring AOP,可以更灵活地控制方法拦截和增强。首先定义自定义注解,如`@MyCustomAnnotation`,然后创建切面类`MyCustomAspect`,使用`@Pointcut`和`@Before/@After`定义切点及通知。配置AOP代理,添加`@EnableAspectJAutoProxy`到配置类。最后,在需拦截的方法上应用自定义注解。遵循保持注解职责单一、选择合适保留策略等最佳实践,提高代码可重用性和可维护性。记得测试AOP逻辑。
|
7天前
|
存储 消息中间件 Java
Java多线程实战-异步操作日志记录解决方案(AOP+注解+多线程)
Java多线程实战-异步操作日志记录解决方案(AOP+注解+多线程)
|
20天前
|
Java Spring
代码优雅的转变:基于注解的AOP编程在Spring中的实践
代码优雅的转变:基于注解的AOP编程在Spring中的实践
17 0
|
23天前
|
存储 关系型数据库 MySQL
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)