Springboot-aop(一)

news/2024/7/5 2:10:48

引入依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
public interface MyCalculator {
    int add(int a, int b);
}
public class MyCalculatorImpl implements MyCalculator {
    public int add(int a, int b) {
        return a+b;
    }
}

基于xml 配置
aop.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean class="com.example.springbootaop.aspect.LogAspect" id="logAspect"/>
    <bean class="com.example.springbootaop.service.impl.MyCalculatorImpl" id="myCalculator"/>
    <aop:config>
        <aop:pointcut id="pc1" expression="execution(* com.example.springbootaop.service.*.*(..))"/>
        <aop:aspect ref="logAspect">
            <aop:before method="before" pointcut-ref="pc1"/>
            <aop:after method="after" pointcut-ref="pc1"/>
            <aop:after-returning method="returing" pointcut-ref="pc1" returning="r"/>
            <aop:after-throwing method="afterThrowing" pointcut-ref="pc1" throwing="e"/>
            <aop:around method="around" pointcut-ref="pc1"/>
        </aop:aspect>
    </aop:config>

</beans>

引入xml 配置

@Configuration
@ImportResource(locations = "classpath:aop.xml")
public class AopConfig {
}

启动项目

@SpringBootApplication
public class SpringbootAopApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootAopApplication.class, args);
        MyCalculator myCalculator = (MyCalculator) context.getBean("myCalculator");
        myCalculator.add(1,3);
        
    }
}

基于注解配置

@Component
@Aspect
public class LogAspectAnnotation {

    @Pointcut("execution(* com.example.springbootaop.service.*.*(..))")
    public void pc(){

    }
    @Before("pc()")
    public void before(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println(name + "方法开始执行了...");
    }
    @After("pc()")
    public void after(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println(name + "方法执行结束了...");
    }
    @AfterReturning(value = "pc()",returning = "returnVal")
    public void returing(JoinPoint joinPoint,Object returnVal) {
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();

    }
    @AfterThrowing(pointcut="pc()",throwing = "e")
    public void afterThrowing(JoinPoint joinPoint,Throwable e) {
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();

    }
    @Around("pc()")
    public Object around(ProceedingJoinPoint pjp) {
        Object proceed = null;
        try {
            //这个相当于 method.invoke 方法,我们可以在这个方法的前后分别添加日志,就相当于是前置/后置通知
            System.out.println("around执行了1");
            proceed = pjp.proceed();
            System.out.println("around执行了2");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return proceed;
    }
}

启动项目和xml 有相同的效果

execution 表达式

学习Spring中的aop组装切面时遇到的execution表达式,下面是execution表达式的详解

切入点表达式:

整个表达式可以分为五个部分:

1、execution(): 表达式主体。

2、第一个*号:方法返回类型, *号表示所有的类型。

3、包名:表示需要拦截的包名。

4、第二个*号:表示类名,*号表示所有的类。

5、*(…):最后这个星号表示方法名,*号表示所有的方法,后面( )里面表示方法的参数,两个句点表示任何参数

execution(* 包名..(…))

规范写法:execution(* com.baizhi.service.UserServiceImpl.*(..))重点    最通用的        //执行接口下的所有接口方法

http://lihuaxi.xjx100.cn/news/1231654.html

相关文章

2023夏PAT甲级题解

目录 总结&#xff1a; A-1 题意&#xff1a; 思路&#xff1a; AC代码&#xff1a; A-2 题意&#xff1a; AC代码&#xff1a; A-3 题意&#xff1a; 思路&#xff1a; A-4 Big Number 题意&#xff1a; 思路&#xff1a; AC代码 总结&#xff1a; 第一次打PAT…

AMC12和高考数学哪个更难?知识点有哪些不同?

AMC12和高考数学哪个更难&#xff1f;知识点有哪些不同&#xff1f;今天小编给大家来详细介绍一下&#xff01; 难度对比 从难度上看&#xff0c;高考数学的计算量更大&#xff0c;并且知识点比AMC10/12超前&#xff0c;需要用到极限和微积分的知识。 反观AMC10/12不需要用到…

【MySQL】数据库的查询语言DQL

目录 前言&#xff1a; 一.基本查询 1.1查询多个字段 1.2设置别名 1.3去除字段中重复的值 二.条件查询 2.1条件的种类 2.1.1比较运算符 2.1.2逻辑运算符 三.结尾 前言&#xff1a; 在前面讲完了如何增删改数据表中的记录后&#xff0c;那么如何使用这些数据就成了另一…

有可以在游泳戴的耳机吗?适合游泳佩戴的耳机推荐

1.南卡Runner Pro4S骨传导游泳耳机 作为国内骨传导天花板品牌的南卡其发布的新产品Runner Pro 4S与之前的Pro 3和Pro 4在佩感方面没有太大改变&#xff0c;依旧舒适牢固&#xff0c;不会发生掉落的情况&#xff0c;实测重量31.7g&#xff0c;几乎是无感佩戴&#xff0c;毫无负担…

SpringBoot+Mybatis+Thymeleaf实现的物资管理系统

本系统具体使用的技术是&#xff1a;后端使用SpringBootMybatis&#xff0c;前端使用了Thymeleaf框架&#xff0c;数据库使用的是MySql 8.0。开发工具使用的是IDEA。 本系统前端是使用了前辈的管理系统模板&#xff0c;具体的系统模块功能如下图所示&#xff1a; 一、系统首页…

dubbo流量录制异常(dubbo2.7.3)的问题解决排查

背景 我们自己基于jvm-sandbox-repeater做的流量录制出现了如下的问题, 从这个问题的堆栈信息来看&#xff0c;是在针对dubbo的调用的时候判断这个dubbo的返回是否有异常的时候&#xff0c;报了空指针异常了。 分析 我们看下具体出错的代码地方是怎么样的吧。 Overridepro…

Rust每日一练(Leetday0027) 单词搜索、删除重复项II、搜索旋转排序数组II

目录 79. 单词搜索 Word Search &#x1f31f;&#x1f31f; 80. 删除有序数组中的重复项 II Remove-duplicates-from-sorted-array-II &#x1f31f;&#x1f31f; 81. 搜索旋转排序数组 II Search-in-rotated-sorted-array-II &#x1f31f;&#x1f31f; &#x1f31…

北邮国院物联网Software Engineering软件工程笔记

主要依照课上ppt的面向考试学习。 pdf文件获取&#xff1a;添加文章末尾微信公众号&#xff1a;灰海宽松&#xff0c;后台回复“软件工程”获取文件。 文章目录 Introductionsoftware typesgood software featureswhat is software engineering?4 layersWhy important?Genera…