Spring注解开发:实现AOP

定义切面类

使用@Aspect定义切面类

@Aspect: 告诉Spring当前类是一个切面类

用法示例:

在这里插入图片描述

定义Pointcut

使用@Pointcut指定Advice的作用范围

@Pointcut修饰的方法不能被调用,不能有返回值和参数,只是一个标识

通过方法名可以引用Pointcut

用法示例:

在这里插入图片描述
该pointcut指定的范围为:spring.annotation.aop.MathCalculator类中的所有方法

定义Advice

定义横切性关注点的实现,及Advice的织入位置

  • @Before:在目标方法执行之前织入Advice;
  • @After:在目标方法执行之后织入Advice;
  • @AfterReturning:在目标方法执行之后织入Advice,自动将目标方法的运行结果注入Advice中;
  • @AfterThrowing:目标方法出现异常后织入Advice。

注:
使用@After时,如果目标方法出现异常,Advice依旧正常织入
使用@AfterReturning时,如果目标方法出现异常,Advice将不再织入

用法示例:

在这里插入图片描述

配置IOC容器

  • 使用@EnableAspectJAutoProxy 在配置类中启动AOP注解的支持
  • 将目标类和切面类注册到配置类中

用法示例:

在这里插入图片描述

代码演示:

代码示例

LogAspects类:

  
package spring.annotation.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;
  /**
   * 切面类
   * @author admin
   * @Aspect: 告诉Spring当前类是一个切面类
   */
   @Aspect
   public class LogAspects {

	     //抽取公共的切入点表达式
		//1、本类引用
		//2、其他的切面引用
        //该方法不能调用,只是一个标识,此方法不能有返回值和参数,当需要引用Pointcut 时使用方法名即可
		@Pointcut("execution(public int spring.annotation.aop.MathCalculator.*(..))")
		public void pointCut(){};


//		定义advise,标识在那个切入点何处织入此方法,四种标识方式:
		 
		//@Before在目标方法之前切入;切入点表达式(指定在哪个方法切入)
		@Before("pointCut()")
		public void logStart(JoinPoint joinPoint){
			Object[] args = joinPoint.getArgs();
			System.out.println(""+joinPoint.getSignature().getName()+"方法运行前。。。@Before:参数列表是:{"+Arrays.asList(args)+"}");
		}

		//目标方法执行之后切入 目标方法产生异常时也会执行
		@After("pointCut()")
		public void logEnd(JoinPoint joinPoint){
			System.out.println(""+joinPoint.getSignature().getName()+"方法运行后。。。@After");
		}
		
		//JoinPoint一定要出现在参数表的第一位 目标方法执行之后切入获取执行结果 目标方法产生异常时不会执行
		@AfterReturning(value="pointCut()",returning="result")
		public void logReturn(JoinPoint joinPoint,Object result){
			System.out.println(""+joinPoint.getSignature().getName()+"方法正常返回。。。@AfterReturning:运行结果:{"+result+"}");
		}
		//出现异常时织入advise
		@AfterThrowing(value="pointCut()",throwing="exception")
		public void logException(JoinPoint joinPoint,Exception exception){
			System.out.println(""+joinPoint.getSignature().getName()+"方法异常。。。异常信息:{"+exception+"}");
		}
  
  }

MainConfigOfAOP类:

package spring.annotation.aop;

  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.EnableAspectJAutoProxy;
  
  @EnableAspectJAutoProxy //启用对AOP注解的支持
  @Configuration //配置类
  public class MainConfigOfAOP {
	//业务逻辑类加入容器中
		@Bean
		public MathCalculator calculator(){
			return new MathCalculator();
		}

		//切面类加入到容器中
		@Bean
		public LogAspects logAspects(){
			return new LogAspects();
		}
  }

MathCalculator类:


  package spring.annotation.aop;

  public class MathCalculator {
	  
	  public int div(int i,int j){
			System.out.println("目标对象MathCalculator的 div方法 执行");
			return i/j;	
		}
  }

IOCTest_AOP类:

  
  package spring.annotation.aop;

  import org.junit.Test;
  import org.springframework.context.annotation.AnnotationConfigApplicationContext;

  public class IOCTest_AOP {
	  @Test
		public void test01(){
			AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAOP.class);
			
			//不要自己创建对象
//			MathCalculator mathCalculator = new MathCalculator();
//			mathCalculator.div(1, 1);
			MathCalculator mathCalculator = applicationContext.getBean(MathCalculator.class);
			
			mathCalculator.div(10, 2);
			
			applicationContext.close();
		}
  }

运行结果

正常情况下运行:

在这里插入图片描述

抛出异常情况下运行:

在这里插入图片描述

说明:本文仅用作学习笔记,无其他用途,如有冒犯可联系本人删除

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值