基于Annotation的Spring AOP: @Around

本文详细介绍了Spring AOP中@Around注解的使用,指出其结合了Before和AfterReturning的功能,允许在方法执行前后进行增强,并能够控制方法的执行时机和方式。同时,提到了在何种情况下应该考虑使用@Around,如需要改变参数或返回值、阻止方法执行等。还强调了使用时需要注意线程安全,并提供了 ProceedingJoinPoint 的用法示例,通过调用其proceed方法来执行目标方法。
部署运行你感兴趣的模型镜像

@Around 增强处理是功能比较强大的增强处理,它近似等于Before AfterReturning的总和。@Around既可在执行目标方法之前织入增强动作,也可在执行目标方法之后织入增强动作。@Around甚至可以决定目标方法在什么时候执行,如何执行,更甚者可以完全阻止目标方法的执行。

@Around可以改变执行目标方法的参数值,也可以改变执行目标方法之后的返回值。

@Around功能虽然强大,但通常需要在线程安全的环境下使用。因此,如果使用普通的Before、AfterReturning就能解决的问题,就没有必要使用Around了。如果需要目标方法执行之前和之后共享某种状态数据,则应该考虑使用Around。尤其是需要使用增强处理阻止目标的执行,或需要改变目标方法的返回值时,则只能使用Around增强处理了。

当定义一个Around增强处理方法时,该方法的第一个形参必须是 ProceedingJoinPoint 类型,在增强处理方法体内,调用ProceedingJoinPoint的proceed方法才会执行目标方法------这就是@Around增强处理可以完全控制目标方法执行时机、如何执行的关键;如果程序没有调用ProceedingJoinPoint的proceed方法,则目标方法不会执行。

调用ProceedingJoinPoint的proceed方法时,还可以传入一个Object[ ]对象,该数组中的值将被传入目标方法作为实参。如果传入的Object[ ]数组长度与目标方法所需要的参数个数不相等,或者Object[ ]数组元素与目标方法所需参数的类型不匹配,程序就会出现异常。


Person.java :

public interface Person {
	public String sayHello(String name);
	public void eat(String food);
	public void divide();
}
Chinese.java :

@Component
public class Chinese implements Person {

	@Override
	public void divide() {
		int a=5/0;
		System.out.println("divide执行完成!");
	}

	@Override
	public String sayHello(String name) {
		System.out.println("sayHello方法被调用...");
		return name+" Hello,Spring AOP";
	}

	@Override
	public void eat(String food) {
		System.out.println("我正在吃:"+food);
	}

	
}
AroundAdviceTest.java :

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class AroundAdviceTest {
	
	@Around("execution(* com.bean.*.*(..))")
	public Object processTx(ProceedingJoinPoint jp) throws Throwable{
		System.out.println("执行目标方法之前,模拟开始事务...");
		Object rvt=jp.proceed(new String[]{"被改变的参数"});
		System.out.println("执行目标方法之后,模拟结束事务...");
		return rvt+"新增的内容";
	}
}
bean.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:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/context 
                http://www.springframework.org/schema/context/spring-context-2.5.xsd
                http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    
    
    <context:component-scan base-package="com.bean">
        <context:include-filter type="annotation" 
                 expression="org.aspectj.lang.annotation.Aspect"/>
    </context:component-scan>
    
    <aop:aspectj-autoproxy/>
    
 </beans>
Test.java :

public class Test {
	public static void main(String[] args) {
		
		ApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml");
		Person p=(Person) ctx.getBean("chinese");
		System.out.println(p.sayHello("张三"));
		p.eat("西瓜");
		p.divide();
	}
}

运行程序,控制台输出:





您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值