SpringBoot整合Aop进行方法增强

一.AOP知识点

1.切入点表达式(表明需要增强的方法)

execution(权限修饰符 返回值类型 包名.类名.方法名(参数类型))

-- 完整写法
execution(public void com.atguigu.service.impl.UserServiceImpl.updateUser(Integer,String))

-- 省略权限修饰符
execution( void com.atguigu.service.impl.UserServiceImpl.updateUser(Integer,String))

-- 不限定返回值类型
execution( * com.atguigu.service.impl.UserServiceImpl.updateUser(Integer,String))

-- 不限定包名
execution( * *..UserServiceImpl.updateUser(Integer,String))

-- 不限定类名
execution( * *..*ServiceImpl.updateUser(Integer,String))

-- 不限定方法名
execution( * *..*ServiceImpl.*(Integer,String))

-- 不限定参数
execution( * *..*ServiceImpl.*(..))

-- 不推荐
execution( * *..*.*(..))
2.通知类别(掌握)
通知类名标签说明
前置通知aop:before在切入点之前执行
正常后置通知aop:after-returning在切入点之后执行,切入点没有异常才执行
异常后置通知aop:after-throwing在切入点之后执行,切入点有异常才执行
后置通知aop:after在切入点之后执行,不管切入点是否有异常都执行
环绕通知aop:around在切入点之前执行,在切入点之后执行
/**
 * 通知类
 */
@Aspect
@Component
public class MyAdvice1 {


    /**
     * 前置通知
     */
    @Before("execution(* *..*Service.*(..))")
    public void before(){
        System.out.println("MyAdvice1 .. before");
    }


    /**
     * 正常后置通知
     */
    @AfterReturning("execution(* *..*Service.*(..))")
    public void afterReturing(){
        System.out.println("MyAdvice1 .. afterReturing");

    }


    /**
     * 异常后置通知
     */
    @AfterThrowing("execution(* *..*Service.*(..))")
    public void afterThrowing(){
        System.out.println("MyAdvice1 .. afterThrowing");

    }


    /**
     * 后置通知
     */
    @After("execution(* *..*Service.*(..))")
    public void after(){
        System.out.println("MyAdvice1 .. after");
    }


    @Around("execution(* *..*Service.*(..))")
    public void around(ProceedingJoinPoint pjp){
        System.out.println("around之前");
        try {
            pjp.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        System.out.println("around之后");

    }
}
  • AOP注解获取切入点输入参数(掌握)
@Aspect
@Component
public class MyAdvice2 {


    /**
     * 前置通知
     */
    @Before("execution(* *..*Service.*(..))")
    public void before(JoinPoint jp){
        System.out.println("MyAdvice1 .. before 输入参数 = " + Arrays.toString(jp.getArgs()));
    }



    @Around("execution(* *..*Service.*(..))")
    public void around(ProceedingJoinPoint pjp){
        System.out.println("MyAdvice2 around 输入参数 = " + Arrays.toString(pjp.getArgs()));
        try {
            pjp.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }
}
  • AOP注解获取切入点异常(掌握)
@Aspect
@Component
public class MyAdvice4 {


    /**
     * 异常后置通知
     */
    @AfterThrowing( value = "execution(* *..*Service.*(..))" ,throwing = "e")
    public void afterThrowing(Exception e){
        System.out.println("MyAdvice4 .. afterThrowing 异常 = " + e);
    }



    @Around("execution(* *..*Service.*(..))")
    public void around(ProceedingJoinPoint pjp){
        try {
            pjp.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("MyAdvice4 .. around 异常 = " + throwable);
            
        }
    }
}
  • AOP注解获取切入点返回值(掌握)
@Aspect
@Component
public class MyAdvice3 {

    /**
     * 正常后置通知
     */
    @AfterReturning(value = "execution(* *..*Service.*(..))",returning = "result")
    public void afterReturing(Object result){
        System.out.println("MyAdvice1 .. afterReturing 返回值 = " + result);
    }


    @Around("execution(* *..*Service.*(..))")
    public void around(ProceedingJoinPoint pjp){
        try {
            Object result = pjp.proceed();
            System.out.println("MyAdvice3 .. around 返回值 = " + result);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }


    }
}

二,操作流程

1.导入依赖
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
2.编写Aop配置类

在类上加注解@Aspect标志为切面类,@Component将类放入spring容器,@Before表明为前置通知

@Component
@Aspect
public class AopAdvice {

    @Before("execution(* *..HospitalSetController.lockHospSet(..))")//切入点表达式
    public void before(){
        System.out.println("切入成功!!!!!!!!!!!!!");
    }
    
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 基础教程(基于1.3.x-1.5.x) 快速入门 chapter1:基本项目构建(可作为工程脚手架),引入web模块,完成一个简单的RESTful API 使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程 工程配置 chapter2-1-1:配置文件详解:自定义属性、随机数、多环境配置等 chapter2-1-2:2.0 新特性(一):配置绑定全解析 chapter2-2-1:2.0 新特性(二):新增事件ApplicationStartedEvent Web开发 chapter3-1-1:构建一个较为复杂的RESTful API以及单元测试 chapter3-1-2:使用Thymeleaf模板引擎渲染web视图 chapter3-1-3:使用Freemarker模板引擎渲染web视图 chapter3-1-4:使用Velocity模板引擎渲染web视图 chapter3-1-5:使用Swagger2构建RESTful API chapter3-1-6:统一异常处理 chapter3-1-7:使用Java 8中LocalDate等时间日期类的问题解决 chapter3-1-8:扩展XML请求和响应的支持 数据访问 chapter3-2-1:使用JdbcTemplate chapter3-2-2:使用Spring-data-jpa简化数据访问层(推荐) chapter3-2-3:多数据源配置(一):JdbcTemplate chapter3-2-4:多数据源配置(二):Spring-data-jpa chapter3-2-5:使用NoSQL数据库(一):Redis chapter3-2-6:使用NoSQL数据库(二):MongoDB chapter3-2-7:整合MyBatis chapter3-2-8:MyBatis注解配置详解 chapter3-2-9:使用Flyway来管理数据库版本 chapter3-2-10:使用LDAP来统一管理用户信息 chapter3-2-11:Spring Boot中增强对MongoDB的配置(连接池等) 事务管理 chapter3-3-1:使用事务管理 chapter3-3-2:[分布式事务(未完成)] 其他内容 chapter4-1-1:使用@Scheduled创建定时任务 chapter4-1-2:使用@Async实现异步调用 chapter4-1-3:使用@Async实现异步调用:自定义线程池 chapter4-1-4:使用@Async实现异步调用:资源优雅关闭 chapter4-1-5:使用@Async实现异步调用:使用Future以及定义超时 日志管理 chapter4-2-1:默认日志的配置 chapter4-2-2:使用log4j记录日志 chapter4-2-3:对log4j进行多环境不同日志级别的控制 chapter4-2-4:使用AOP统一处理Web请求日志 chapter4-2-5:使用log4j记录日志到MongoDB chapter4-2-6:Spring Boot 1.5.x新特性:动态修改日志级别] 安全管理 chapter4-3-1:使用Spring Security chapter4-3-2:[使用Spring Session(未完成)] 缓存支持 chapter4-4-1:注解配置与EhCache使用 chapter4-4-2:使用Redis做集中式缓存 邮件发送 chapter4-5-1:实现邮件发送:简单邮件、附件邮件、嵌入资源的邮件、模板邮件 消息服务 chapter5-1-1:[JMS(未完成)] chapter5-2-1:Spring Boot中使用RabbitMQ 其他功能 chapter6-1-1:使用Spring StateMachine框架实现状态机 Spring Boot Actuator监控端点小结 在传统Spring应用中使用spring-boot-actuator模块提供监控端点 Spring Boot应用的后台运行配置 Spring Boot自定义Banner Dubbo进行服务治理 chapter9-2-1:Spring Boot中使用Dubbo进行服务治理 chapter9-2-2:Spring Boot与Dubbo中管理服务依赖

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值