Spring的AOP机制

目录

 

1.AOP简介

2.AOP术语

3.Spring AOP

1.在Spring中启用AspectJ注解支持,用AspectJ注解声明切面

2.用基于XML的配置声明切面


1.AOP简介

  • AOP是面向切面的编程,其编程的思想是把散步于不同业务但功能相同的代码从业务逻辑中抽取出来,封装成独立的模块,这些独立的模块被称为切面,切面具体功能方法别称为关注点。在业务逻辑执行过程中,AOP会把分离出来的切面和关注点动态切入到业务流程中,这样做的好处是提高了功能代码的重用性和可维护性。
  • 在应用AOP编程时, 仍然需要定义公共功能, 但可以明确的定义这个功能在哪里, 以什么方式应用, 并且不必修改受影响的类. 这样一来横切关注点就被模块化到特殊的对象(切面)里.
  • AOP的好处:

          ①每个事物逻辑位于一个位置,代码不分散,便于维护和升级

          ②业务模块更简洁,只包含核心业务代码

  • 什么时候使用AOP

      首先,是否会重复在多个地方织入?

      其次,是否方便业务(AOP实现的功能)功能的灵活装载和卸载呢?

2.AOP术语

  • 切面(Aspect):横切关注点(跨越应用程序多个模块的功能)被模块化的特殊对象
  • 通知、增强(Advice):切面必须完成的工作,扩展、增强的功能
  • 目标(Target):被通知的对象
  • 代理(Proxy):向目标对象应用通知之后创建的对象
  • 连接点(Joinpoint):程序执行的某个特定的位置(方法执行前、执行后、抛出异常......)可以理解为类中可以被增强的方法(方法又有执行位置),这些方法成称之为连接点
  • 切入点(pointcut):每个类都有多个连接点,如ArithmethicCalculator 的所有方法实际上都是连接点,即连接点是程序类中客观存在的事务。AOP 通过切入点定位到特定的连接点。即可以理解为实际被增强的方法就是切入点.

 


3.Spring AOP

1.在Spring中启用AspectJ注解支持,用AspectJ注解声明切面

   1.用AspectJ注解声明切面

  • 要在Spring中声明AspectJ切面,只需要再IOC容器中将切面声明为Bean 实例,当在SpringIOC容器中初始化AspectJ切面之后,SpringIOC容器会为那些与AspectJ切面相匹配的Bean创建代理。

       在Aspect注解中,切面只是一个带有@AspectJ注解的java类

      通知是标注有某种主借的简单的java方法‘

  • AspectJ支持5类型的通知注解。

   ① @Before:前置通知,在方法执行之前执行

   ② @After:后置通知,在方法执行之后执行

   ③ @AfterRunning:返回通知,在方法返回结果之后执行

   ④ @AfterThrowing:异常通知,在方法抛出异常后执行

   ⑤@Around:环绕通知,围绕着方法执行

2. 利用方法签名编写AspectJ切入点表达式 

         最典型的切入点表达式是根据方法的签名来匹配各种方法:

   ①exection * com.hbsi.aop.ArithmeticCalculator.*(..):匹配ArithmeticCalculator中声明的所有的方法,第一个*代表任意修饰符及 任 意返回值,第二个*代表任意方法,..匹配任意数量的参数。若目标类与接口与该切面在同一个包中,可以省略包名。

   ②execution public * ArithmeticCalculator.*(..): 匹配ArithmeticCalculator接口的所有公有方法.

   ③ execution public double ArithmeticCalculator.*(..): 匹配ArithmeticCalculator返回double类型数值的公有方法

   ④execution public double ArithmeticCalculator.*(double, ..): 匹配第一个参数为double类型的方法, .. 匹配任意数量任意类型 的参数

   ⑤execution public double ArithmeticCalculator.*(double, double): 匹配参数类型为double, double类型的方法.

代码演示

(1)spring-aop.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-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<context:component-scan base-package="com.hbsi.aop"></context:component-scan>
<!-- 生成自动代理 基于注解配置ASpectJ 
通过aop命名空间的<aop:aspectj-autoproxy 
/>声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面。
-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>

(2)ArithmeticCalculator接口 


public interface ArithmeticCalculator {
	public int add(int a,int b);
	public int sub(int a,int b);
}

(3)ArithmeticCalculatorImpl    实现类

package com.hbsi.aop;

import org.springframework.stereotype.Component;

@Component
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

	@Override
	public int add(int a, int b) {
		// TODO Auto-generated method stub
		int result = a + b;
		System.out.println("a + b = "+result);
		return result;
	}
	@Override
	public int sub(int a, int b) {
		// TODO Auto-generated method stub
		int result = a - b;
		System.out.println("a - b = "+result);
		return result;
	}
}

(4)切面类LogginAspect  (包括前置、后置、返回、异常通知)

package com.hbsi.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
@Aspect
public class LogginAspect {
	@Pointcut(value = "execution(public * com.hbsi.aop.ArithmeticCalculatorImpl.*(..))")
     public void declarePointCut(){
    	 
     }
	// 前置通知 JoinPoint:连接点对象,该对象中包含了与当前目标方法相关的一些信息
	@Before(value = "execution(public * com.hbsi.aop.ArithmeticCalculatorImpl.*(..))")
	public void beforeMethod(JoinPoint joinPoint) {
		// 获取方法的名称
		String methodName = joinPoint.getSignature().getName();
		Object[] args = joinPoint.getArgs();
		// 获取方法的参数
		System.out.println("前置通知  The method:" + methodName + "  begin with" + Arrays.asList(args));
	}

	// 目标方法执行后的通知,后置通知
	/*
	 * 1.后置通知在目标方法执行后执行,不管目标方法有没有抛出异常 2.后置通知获取不到目标方法的返回值
	 */
	@After(value = "execution(public * com.hbsi.aop.ArithmeticCalculatorImpl.*(..))") // 切入点表达式的通用形式
	public void afterMethod(JoinPoint joinPoint) { // 可以匹配多个连接点
		String methodName = joinPoint.getSignature().getName();
		Object[] args = joinPoint.getArgs();
		System.out.println("后置通知  The method:" + methodName + "  end with");
	}

	// returningreturning: 用于指定接收目标方法的返回值, 必须与通知方法的形参名一致

	// AfterReturning返回通知 在方法返回结果之后执行
	@AfterReturning(value = "execution(public * com.hbsi.aop.*.*(..))", returning = "result")
	public void afterRunningMethod(JoinPoint joinPoint, Object result) {
		String methodName = joinPoint.getSignature().getName();
		Object[] args = joinPoint.getArgs();
		System.out.println("返回通知  The method:" + methodName + "  end with  " + result);
	}

	// 目标方法抛出异常后执行,异常通知
	// ArithmeticException 算术异常
	// 异常通知可以设置抛出指定的异常才执行,只需要在通知方法的形参中进行类型的定义即可
	@AfterThrowing(value = "execution(public * com.hbsi.aop.*.*(..))", throwing = "ex")
	public void afterThrowingMethod(JoinPoint joinPoint, ArithmeticException ex) {
		String methodName = joinPoint.getSignature().getName();
		Object[] args = joinPoint.getArgs();
		System.out.println("异常通知  The method:" + methodName + "  end with  " + ex);
	}
}

执行结果展示

 置通知  The method:add  begin with[1, 1]
a + b = 2
后置通知  The method:add  end with
返回通知  The method:add  end with  2
前置通知  The method:sub  begin with[1, 1]
a - b = 0
后置通知  The method:sub  end with
返回通知  The method:sub  end with  0

画图展示

1.前置通知

.

   2.返回通知

 (5)环绕通知   环绕通知类似于动态代理的整个过程,综合了前置 后置 返回 异常四个通知功能 Proceeding 事件 过程

package com.hbsi.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
@Aspect
public class LogginAspect {
	// //环绕着目标方法执行 :环绕通知 ProceedingJoinPoint 是JoinPoint的子类
	// 环绕通知类似于动态代理的整个过程,综合了前置 后置 返回 异常四个通知功能 Proceeding 事件 过程
	@Around("execution(public * com.hbsi.aop.*.*(..))")
	public Object aroundtMethod(ProceedingJoinPoint pjp) {
		String methodName = pjp.getSignature().getName();
		Object[] args = pjp.getArgs();
		try {
			// 前置通知
			System.out.println("前置通知  The method:" + methodName + "  begin with" + Arrays.asList(args));
			// 调用目标方法
			Object result = pjp.proceed();
			//返回通知
			System.out.println("返回通知  The method:" + methodName + "  end with  " + result);
			return result;
		} catch (Throwable e) {
			// TODO: handle exception
			System.out.println("异常通知  The method:" + methodName + "  end with  " + e);
		} finally {
			System.out.println("后置通知  The method:" + methodName + "  end with");
		}
		return null;//永不执行  但是该方法需要有返回值
	}
	
}

环绕通知执行结果展示:

前置通知  The method:add  begin with[1, 1]
a + b = 2
返回通知  The method:add  end with  2
后置通知  The method:add  end with
前置通知  The method:sub  begin with[1, 1]
a - b = 0
返回通知  The method:sub  end with  0
后置通知  The method:sub  end with

3.指定切面优先级:此处我们之定义了一个切面就不进行演示

       切面的优先级可以通过@Order注解指定,序号出现在注解中,序号越小,优先级越高

      使用@Order注解的优先级高于没有加@Order注解的

@Component
@Aspect
@Order(1)
public class ValidateAspect {

 

@Component
@Aspect
@Order(0)
public class LogginAspect {

4.重用切入点定义

   可以通过@Pointcut注解将一个切入点声明成简单的方法.方法体通常是空的, 因为将切入点定义与应用程序逻辑混在一起是不合理的

     

2.用基于XML的配置声明切面

  •  除了使用AspectJ注解声明切面, Spring也支持在Bean配置文件中声明切面. 这种声明是通过aop schema中的XML元素完成的.
  • 正常情况下, 基于注解的声明要优先于基于XML的声明. 通过AspectJ注解, 切面可以与AspectJ兼容, 而基于XML的配置则是Spring专有的. 由于AspectJ得到越来越多的AOP框架支持, 所以以注解风格编写的切面将会有更多重用的机会.
  • Bean配置文件中, 所有的Spring AOP配置都必须定义在<aop:config>元素内部. 对于每个切面而言, 都要创建一个<aop:aspect>元素来为具体的切面实现引用后端Bean实例.

(1)spring-aop-xml.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-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

<!-- 用xml声明切面     声明目标对象-->
<bean id="arithmeticCalculatorImpl" class="com.hbsi.aopxml.ArithmeticCalculatorImpl"></bean>
<!-- 声明切面 -->
<bean id="logginAspect" class="com.hbsi.aopxml.LogginAspect"></bean>
<aop:config><!-- 配置AOP -->
<!-- 配置切入点表达式 -->
<aop:pointcut expression="execution(public * com.hbsi.aopxml.ArithmeticCalculatorImpl.*(..))" id="myPointCut"/>
<!-- 配置切面 -->
<aop:aspect ref="logginAspect">
<aop:before method="beforeMethod" pointcut-ref="myPointCut"/>
<aop:after method="afterMethod" pointcut-ref="myPointCut"/>
<!-- result ex  和通知方法的参数名一致 -->
<aop:after-returning method="afterRunningMethod" pointcut-ref="myPointCut" returning="result"/>
<aop:after-throwing method="afterThrowingMethod" pointcut-ref="myPointCut" throwing="ex"/>
</aop:aspect>
</aop:config>
</beans>

(2)其他的去掉类中注解 ,跟上面的写法是一样的

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值