Spring之AOP——@Before,@After,@AfterReturning、@AfterThrowing和@Around

本文介绍了Spring AOP在代码复用中的作用,详细讲解了如何创建Spring AOP项目,以及@Aspect注解的查找过程。同时,解释了@Before、@After、@AfterReturning、@AfterThrowing和@Around这五种通知类型,强调了@Around通知的全面性,可以替代前四种。

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

aop作用:为实现代码的复用 故使用SpringAOP

一、创建项目

 根据项目需求引包:

application.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

<context:component-scan base-package="com.jd"></context:component-scan>
<!-- autoproxy自动注入:自动给匹配execution表达式的目标类生成代理类并创建代理对象 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

aspectj-autoproxy执行过程  ①Spring寻找含有@Aspect注解的类;

                                             ②接着寻找该类中的所有方法;

                                             ③从而获取到该类中所有方法的注解;

                                             ④然后获取注解中的表达式(execution(int mul(int , int ));

                                             ⑤然后再此检查Spring能扫描到的所有类,找到与表达式能够匹配的方法所对应的类;

                                             ⑥最后为匹配到的类创建动态对象。

勾选aop,beans,context 

 

 

二、详细说明

测试类Test.java

package com.jd.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jd.computer.service.IComputerService;

public class Test {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
		IComputerService computerService = applicationContext.getBean(IComputerService.class);
		System.out.println(computerService.getClass().getName());
		int result = computerService.div(1, 1);
		System.out.println(result);
		applicationContext.close();
	}

}

CumputerAOP.java 

    //前置增强:在目标方法之前执行
	@Before("execution(public int com.jd.computer.service.ComputerService.*(..))")
	public void beford(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]+"]");
	}

 

    //后置增强:在目标方法执行之后执行,无论目标方法运行期间是否出现异常。(后置增强无法获取目标方法执行结果,可以在返回增强中获取)
	@After(value="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.");
	}

 

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

 

 1.@Before对应于方法开始执行之前;@After对应于方法结束;@AfterThrowing对应于抛出异常后;@AfterReturning在出现异常时不执行。

2.@AfterReturning是在返回成功时调用;@After是在方法结束之后、执行return之前调用

 

@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、付费专栏及课程。

余额充值