SpringAOP 环绕通知,AOP应用以及基于注解的AOP


应用

1.日志 , 并将日志保存进数据库
2.代替filter拦截器(权限验证/安全检查)
3.事务控制(回滚,提交关闭)

一、环绕通知是什么?

环绕通知是SpringAOP最强大的通知 , 底层和编写逻辑都类似于Java原生的动态代理 , 相当于是将前面的四个通知做了合并

二、环绕通知使用步骤

1.准备工作

打上Around标签,向方法传入ProceedingJoinPoint参数并获取目标方法执行的参数列表

2.执行

使用Object result = pjp.proceed(args);执行方法 , 用result接受返回值

3.加上try-catch和throws抛出异常

4.代码

@Around("constPoint()")
	public static void logAround(ProceedingJoinPoint pjp) throws Throwable {
//		System.out.println("["+joinPoint.getSignature().getName() + "]" + "方法执行完成 ");
		
		Object[] args = pjp.getArgs(); //获取参数列表
		String methodName = pjp.getSignature().getName();
		
		try {
			System.out.println("环绕["+methodName+ "]" + "方法即将开始执行,参数如下 : "+args);
			Object result = pjp.proceed(args); //方法执行 
			System.out.println("环绕["+methodName+ "]" + "方法正常执行完成,结果如下 : "+result);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("环绕["+methodName+ "]" + "方法执行异常,异常如下 : "+e.getCause());
		}finally {
			System.out.println("环绕["+methodName+ "]" + "方法执行完成 ");
		}

三、环绕通知与普通通知的执行顺序

1.如果同时存在环绕通知和普通通知 , 当环绕通知捕获到了异常时 , 
	普通通知就不会执行了
2.同时存在二者时 , 环绕通知先执行	

在这里插入图片描述

四、基于配置的AOP以及使用场景

1.原则 :

重要的目标方法使用配置文件的形式进行AOP切面 , 普通方法使用注解形式配置

2.配置文件

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
	">
		
	
	<aop:aspectj-autoproxy/>
	<context:component-scan base-package="com.hzyc.lesson">
	
	</context:component-scan>
	
	<!-- 将类注册进容器 -->
	<!-- 首字母注意小写 -->
	<bean id="goodDaoImpl1" class="com.hzyc.lesson.mapper.GoodDaoImpl1"></bean>
	<bean id="logUtils" class="com.hzyc.lesson.service.LogUtils"></bean>
	
	<!-- 指定切面 -->
	<aop:config>
		
		<aop:aspect ref="logUtils">
			<!-- 抽取可重用的切入点表达式 -->
			<aop:pointcut expression="execution(* lesson.mapper.*.*(..))" id="myPointcut"/>
			
			<aop:before method="logStart" pointcut-ref="myPointcut"/>
			<aop:after method="logEnd" pointcut-ref="myPointcut"/>
			<aop:after-returning method="logReturn" pointcut-ref="myPointcut" returning="result"/>
			<aop:after-throwing method="logException" pointcut-ref="myPointcut" throwing="exception"/>
		</aop:aspect>
		
		
	</aop:config>
	
</beans>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,使用环绕通知可以实现对方法的拦截和增强。环绕通知AOP的一种类型,它可以在目标方法的执行前后进行额外的操作。 要使用环绕通知,首先需要在Spring Boot项目中引入`spring-boot-starter-aop`依赖。然后,定义一个切面类,并在该类上使用`@Aspect`注解进行标记。 接下来,在切面类中定义一个环绕通知方法,使用`@Around`注解进行标记。环绕通知方法的定义如下: ```java @Aspect @Component public class MyAspect { @Around("execution(* com.example.demo.service.*.*(..))") public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable { // 在方法执行之前做一些操作 System.out.println("Before method execution"); // 执行目标方法 Object result = joinPoint.proceed(); // 在方法执行之后做一些操作 System.out.println("After method execution"); return result; } } ``` 在上述代码中,`@Around`注解的参数是一个切点表达式,用于指定需要拦截的方法。在`aroundAdvice`方法中,我们可以在目标方法执行之前和之后执行自定义的操作。 需要注意的是,环绕通知方法的参数类型是`ProceedingJoinPoint`,它提供了对目标方法的访问和控制。通过调用`joinPoint.proceed()`方法,可以执行目标方法。 最后,确保切面类被Spring Boot扫描到,可以使用`@ComponentScan`注解或在启动类上使用`@SpringBootApplication`注解。这样,切面类就会被自动装配到Spring Boot应用中。 希望对你有所帮助!如果还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值