详述Spring AOP

本文详细介绍了如何在Java中使用面向切面编程(AOP)来简化代码,通过Computer服务的例子,展示了@Before、@After、@AfterReturning、@AfterThrowing和@Around注解的应用,以及如何配置和引用必要的jar包。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为了简化代码 所以使用AOP 

以computer为例  怎么做:

建一个ComputerAop 写上@Aspect、配置一下、写@Before或@After或@AfterReturning或@AfterThrowing或@Around

配置:

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

引用的jar包:

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

 1、@Before 目标方法执行之前

@Before("execution(public int com.jd.cumputer.service.ComputerService.*(..))")
	public void before(JoinPoint jp) {
		Object [] agrs = jp.getArgs();//获取列表的参数
		
		Signature signature = jp.getSignature();//获取名字
		
		String name = signature.getName();
		System.out.println(this.getClass().getName()+":The "+name+" method begins.");
		System.out.println(this.getClass().getName()+":Parameters of the div method: ["+agrs[0]+","+agrs[1]+"]");

	}

 

2、@After目标方法执行完,无论目标方法有无异常都执行

@After("execution(public int com.jd.cumputer.service.ComputerService.*(..))")
	public void after(JoinPoint jp) {
		Signature signature = jp.getSignature();
		
		String name = signature.getName();
		System.out.println(this.getClass().getName()+":The "+name+" method ends.");
	}

 3、@AfterReturning目标方法返回结果,如果目标方法有异常则不执行

	@AfterReturning(value= "execution(public int com.jd.cumputer.service.ComputerService.*(..))" ,returning="result")
	public void afterReturning(JoinPoint jp,Object result) {
		Signature signature = jp.getSignature();
		
		String name = signature.getName();
		System.out.println(this.getClass().getName()+":Result of the "+name+" method :" + result);
	}

4、@AfterThrowing 如果目标方法有异常,抛出异常

@AfterThrowing(value= "execution(public int com.jd.cumputer.service.ComputerService.*(..))" ,throwing="e")
	public void afterThrowing(JoinPoint jp,Exception e) {
		System.out.println(e.getMessage());
	}
	

5、@Around 可以实现以上四种方法

@Around(value= "execution(public int com.jd.cumputer.service.ComputerService.*(..))")
	public Object around(ProceedingJoinPoint pjp) {
		Object [] agrs = pjp.getArgs();
		Signature signature =pjp.getSignature();
		String name = signature.getName();
		System.out.println(this.getClass().getName()+":The "+name+" method begins.");
		System.out.println(this.getClass().getName()+":Parameters of the div method: ["+agrs[0]+","+agrs[1]+"]");
		try {
			Object result =null;
			try {
				Object object = pjp.getTarget();
				result=pjp.proceed();
				
			}finally {
				System.out.println(this.getClass().getName()+":The "+name+" method ends.");
			}
			System.out.println(this.getClass().getName()+":Result of the "+name+" method :" + result);
			return result;
			
		}catch(Throwable e){
			System.out.println(e.getMessage());
		}
		return -1;
		
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值