AOP之切面与通知的一般写法

本文介绍AOP编程中的切面和通知概念,通过示例展示接口、实现类、切面类以及测试类的创建和配置,探讨如何在实际应用中设置切面的优先级。

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

文件结构与jar包

接口类MathI

package com.atguigu.spring.aop;

public interface MathI {
	public int add(int i,int j);
	public int sub(int i,int j);
	public int mul(int i,int j);
	public int div(int i,int j);
	public void Descript();

}

接口实现类MathImpl

package com.atguigu.spring.aop;

import org.springframework.stereotype.Component;

@Component
public class MathImpl implements MathI {

	@Override
	public int add(int i, int j) {
		// TODO Auto-generated method stub
		int result=i+j;
		
		return result;

	}

	@Override
	public int sub(int i, int j) {
		// TODO Auto-generated method stub
		int result=i-j;
		return result;

	}

	@Override
	public int mul(int i, int j) {
		// TODO Auto-generated method stub
		int result=i*j;
		return result;

	}

	@Override
	public int div(int i, int j) {
		// TODO Auto-generated method stub
		int result=i/j;
		return result;

	}

	@Override
	public void Descript() {
		System.out.println("加减乘除");
		// TODO Auto-generated method stub
		
	}

}

切面类MyLoggerAspect

package com.atguigu.spring.aop;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
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.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component  //交于Spring管理
@Aspect    //标注当前类为切面
@Order(1)  //确定切面作用的优先级,值越小优先级越高  默认值为INT最大值
public class MyloggerAspect {
	//类里的方法叫做通知
	
	@Pointcut(value="execution(* com.atguigu.spring.aop.*.*(..))")
	public void test(){
		
	}
	/**
	 * @Before:将方法指定为前置通知
	 * 前置通知:作用于方法执行之前
	 * 必须设置value,其值为切入点表达式
	 * execution()为方法  根据表达式确定作用位置
	 */
//	@Before(value = "execution(public int com.atguigu.spring.aop.MathImpl.add(int, int))") 绝对指定
//	@Before(value="execution(public int com.atguigu.spring.aop.MathImpl.*(int,int))")     泛修饰符 返回类型指定
//	@Before(value="execution(* com.atguigu.spring.aop.MathImpl.*(int,int))")             
//	@Before(value="execution(* com.atguigu.spring.aop.*.*(int,int))")                    
	@Before(value="execution(* com.atguigu.spring.aop.*.*(..))")
	public void beforeMethod(JoinPoint joinPoint){
		Object []args=joinPoint.getArgs();                      //获取方法的参数
		String methodName=joinPoint.getSignature().getName();   //获取方法名 
		System.out.println("前置通知:method:"+methodName+",arguments:"+Arrays.toString(args));
	}
	/**
	 * @After将方法标注为后置通知
	 * 后置通知:作用于方法的finally语句块  即不管有没有异常都会执行
	 * */
	@After(value=("execution(* com.atguigu.spring.aop.*.*(..))"))
	public void afterMethod(){
		System.out.println("后置通知");
	}
	
	/*
	 * AfterReturning:将方法标注为返回通知
	 * 返回通知也叫最后通知:作用于方法执行之后
	 * 可通过returning设置接受方法返回值的变量名
	 * 要想在方法中使用,必须在方法的形参中设置和变量名相同的参数名的参数
	 * 写在try语句块的最后一行  try语句块如果出现异常的话则不会执行
	 * 这样的话就无法获取函数返回值
	 * reurning=result获取一个Object对象(对应函数的返回结果)
	 * */
//	@AfterReturning(value=("execution(* com.atguigu.spring.aop.*.*(..))"))
	@AfterReturning(value=("execution(* com.atguigu.spring.aop.*.*(..))"),returning="result")
	public void afterReturning(JoinPoint joinPoint,Object result){
	    String methodName=joinPoint.getSignature().getName();
	    
		System.out.println("返回通知:method:"+methodName+",result:"+result);
	}
	/*
	 * @AfterThrowing:将方法标注为异常通知(例外通知)
	 * 异常通知(例外通知):作用于方法抛出异常时
	 * 可通过throwing设置接收方法返回的异常信息
	 * 在参数列表中通过具体的异常类型来对指定的异常信息进行操作
	 * exception接受到异常
	 * ArithmeticException为具体的异常类型
	 * */
	@AfterThrowing(value=("execution(* com.atguigu.spring.aop.*.*(..))"),throwing="exception")
	public void afterThrowingMethod(ArithmeticException exception){
		
		System.out.println("有异常了,message:"+exception);
	}
	
	/*
	 * ProcedingJoinPoint接口继承JoinPoint接口 功能更多
	 * */
	@Around(value=("execution(* com.atguigu.spring.aop.*.*(..))"))
	public Object aroundMethod(ProceedingJoinPoint joinPoint){
		Object result=null;
		try {                      
			//前置通知
//			System.out.println("前置通知");
			result =joinPoint.proceed();   //执行方法
			//返回通知
//			System.out.println("返回通知");
			return result;
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//异常通知
//			System.out.println("异常通知");
		}finally{
			//后置通知
//			System.out.println("后置通知");
		}
		return -1;
	}
}

 切面类MyLoggerAspectTest(测试切面的优先级)

package com.atguigu.spring.aop;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
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.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component  //交于Spring管理
@Aspect    //标注当前类为切面
@Order(2)
public class MyloggerAspectTest {
	//类里的方法叫做通知
	                 
	@Pointcut(value="execution(* com.atguigu.spring.aop.*.*(..))")
	public void test(){}
	/**
	 * @Before:将方法指定为前置通知
	 * 前置通知:作用于方法执行之前
	 * 必须设置value,其值为切入点表达式
	 * execution()为方法  根据表达式确定作用位置
	 */
//	@Before(value = "execution(public int com.atguigu.spring.aop.MathImpl.add(int, int))") 绝对指定
//	@Before(value="execution(public int com.atguigu.spring.aop.MathImpl.*(int,int))")     泛修饰符 返回类型指定
//	@Before(value="execution(* com.atguigu.spring.aop.MathImpl.*(int,int))")             
//	@Before(value="execution(* com.atguigu.spring.aop.*.*(int,int))")                    
//	@Before(value="execution(* com.atguigu.spring.aop.*.*(..))")
    @Before(value="test()")
	public void beforeMethod(JoinPoint joinPoint){
		Object []args=joinPoint.getArgs();                      //获取方法的参数
		String methodName=joinPoint.getSignature().getName();   //获取方法名 
		System.out.println("test()的前置通知:method:"+methodName+",arguments:"+Arrays.toString(args));
	}
	/**
	 * @After将方法标注为后置通知
	 * 后置通知:作用于方法的finally语句块  即不管有没有异常都会执行
	 * */
//	@After(value=("execution(* com.atguigu.spring.aop.*.*(..))"))
    @After(value="test()")
	public void afterMethod(){
		System.out.println("test()的后置通知");
	}
	
	/*
	 * AfterReturning:将方法标注为返回通知
	 * 返回通知也叫最后通知:作用于方法执行之后
	 * 可通过returning设置接受方法返回值的变量名
	 * 要想在方法中使用,必须在方法的形参中设置和变量名相同的参数名的参数
	 * 写在try语句块的最后一行  try语句块如果出现异常的话则不会执行
	 * 这样的话就无法获取函数返回值
	 * reurning=result获取一个Object对象(对应函数的返回结果)
	 * */
//	@AfterReturning(value=("execution(* com.atguigu.spring.aop.*.*(..))"))
//	@AfterReturning(value=("execution(* com.atguigu.spring.aop.*.*(..))"),returning="result")
    @AfterReturning(value="test()",returning="result")  //后面该加的还是要加
    public void afterReturning(JoinPoint joinPoint,Object result){
	    String methodName=joinPoint.getSignature().getName();
	    
		System.out.println("test()的返回通知:method:"+methodName+",result:"+result);
	}
	/*
	 * @AfterThrowing:将方法标注为异常通知(例外通知)
	 * 异常通知(例外通知):作用于方法抛出异常时
	 * 可通过throwing设置接收方法返回的异常信息
	 * 在参数列表中通过具体的异常类型来对指定的异常信息进行操作
	 * exception接受到异常
	 * ArithmeticException为具体的异常类型
	 * */
//	@AfterThrowing(value=("execution(* com.atguigu.spring.aop.*.*(..))"),throwing="exception")
	@AfterThrowing(value="test()",throwing="exception")
    public void afterThrowingMethod(ArithmeticException exception){
		
		System.out.println("有异常了,message:"+exception);
	}
	
	/*
	 * ProcedingJoinPoint接口继承JoinPoint接口 功能更多
	 * */
//	@Around(value=("execution(* com.atguigu.spring.aop.*.*(..))"))
	@Around(value="test()")
	public Object aroundMethod(ProceedingJoinPoint joinPoint){
		Object result=null;
		try {                      
			//前置通知
//			System.out.println("前置通知");
			result =joinPoint.proceed();   //执行方法
			//返回通知
//			System.out.println("返回通知");
			return result;
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//异常通知
//			System.out.println("异常通知");
		}finally{
			//后置通知
//			System.out.println("后置通知");
		}
		return -1;
	}
}

测试类Test

package com.atguigu.spring.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	public static void main(String[] args){
		
//		MathI math=new MathImpl();
		ApplicationContext ac=new ClassPathXmlApplicationContext("aop.xml");
		MathI math = ac.getBean("mathImpl",MathI.class);
		System.out.println(math.add(1, 2));
		System.out.println(math.sub(1, 2));
		System.out.println(math.mul(1, 2));
		System.out.println(math.div(2, 2));
//		math.Descript();
		
//		TestHandler bean=ac.getBean("testHandler",TestHandler.class);
//		bean.test();
		
	}

}

xml配置文件aop.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
   <!-- 自动代理  开启aspectJ的自动代理功能 -->
    <aop:aspectj-autoproxy />
    
   <context:component-scan base-package="com.atguigu.spring.aop"></context:component-scan>
  
  </beans>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值