注解方式实现AOP

举报
不会压弯的小飞侠 发表于 2022/08/08 23:46:41 2022/08/08
【摘要】 ✨博客主页:👉不会压弯的小飞侠 ✨欢迎关注:👉点赞👍收藏⭐留言✒ ✨系列专栏:👉spring专栏 ✨如果觉得博主的文章还不错的话,请三连支持一下博主。 ✨欢迎大佬指正,一起学习!一起加油...

在这里插入图片描述

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


一、注解实现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/>通知注解类型

文章来源: blog.csdn.net,作者:不会压弯的小飞侠,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq_43514330/article/details/125462604

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。