文章目录
spring 切面笔记
切面AOP
使用AOP 的好处 :
- 每个事物逻辑位于一个位置,代码不分散,便于维护和升级
- 业务模块更简洁,只包含核心业务代码
例子 :
3. 验证参数
4. 前置日志
5. 后置日志
切面术语
- 切面AOP : 横切关注点(跨越应用程序多个模块的功能)被模块化的特殊对象
- 通知Adivice : 切面必须完成的工作
- 目标Target :被通知的对象
- 代理Proxy : 向目标对象应用通知之后创建的对象
- 连接点Joinpoint : 程序执行的某个特定位置,如类某个方法调用前后,异常等。连接点由两个信息确定:方法表示的程序执行点表示的方位
- 切点pointcut : 每个类都拥有多个连接点,即 连接点是程序类中客观存在的事务。AOP通过切点定位到特定的连接点。类比 : 连接点相当于数据库中的记录,切点相当于查询
spring AOP 使用的都是AspectJ(两种模式注解或配置文件)
注解
注解支持
- 包,(不用说明)
- 配置
<!-- 使Aspjectj 注解起作用, :自动匹配的类生成代理对象 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
注解声明切面
- 要在Spring中声明AspectJ切面,只需要在IOC容器中将切面声明Bean实例
- 在AspectJ的注解中,切面只是一个带有@Aspect注解的java类
- 通知是标注有某种注解的简单的java方法
- AspectJ支持5种类型的通知注解:具体的说明在下面的代码中
- @Before 前置通知
- @After 后置通知
- @AfterRunning 返回通知,方法返回结果之后执行
- @AfterThrowing 异常通知
- @Around 环绕通知,(一般不使用,仅了解)
package com.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.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
//@Order 值越小,优先级越高
/**
* @author Firewine
*/
@Order(1)//表示优先级的意思,越小,优先级越高
@Aspect
@Component
public class LoggingAspect {
/*
* 定义一个方法:用于声明切入点表达式,一般地,该方法种再不需要填入其他的代码
* 使用@Pointcut 来声明切入点表达式
* 后面的其他直接使用方法名来引用当前的切入点表达式
*/
@Pointcut("execution(public int com.spring.aop.ArithmeticCalculator.*(..))")
public void declareJoinPointException() {}
/*
* 在com.spring.aop.ArithmeticCalculator 接口的每一个实现类的每一个方法开始之前执行一段代码
*
*/
@Before("declareJoinPointException()")
public void beforeMethd(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
Object [] args = joinPoint.getArgs();
System.out.println("The method "+ methodName+"begins with " + Arrays.asList(args));
}
/*
* 在方法执行之后执行的代码,无论该方法是否出现异常
* * 号表示,是任意的方法,参数,包,类等等
*/
@After("execution(public int com.spring.aop.ArithmeticCalculator.*(..))")
public void afterMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
System.out.println("The method "+ methodName+"ends " );
}
/*
* 在方法正常结束后执行的代码
* 返回通知是可以访问到方法的返回值的
*
*
*/
@AfterReturning(value="execution(public int com.spring.aop.ArithmeticCalculator.*(..))",returning="result")
public void afterReturning(JoinPoint joinPoint , Object result) {
String methodName = joinPoint.getSignature().getName();
Object [] args = joinPoint.getArgs();
System.out.println("The method "+ methodName+"ends with" + result );
}
/**
* 在方法出现异常时,会执行的代码
* 可以访问到异常对象,且可以指定在出现特定异常在执行通知代码
* @param joinPoint
* @param ex
*/
@AfterThrowing(value="execution(public int com.spring.aop.ArithmeticCalculator.*(..))",
throwing="ex")
public void afterThrowing(JoinPoint joinPoint,Exception ex) {
String methodName = joinPoint.getSignature().getName();
System.out.println("The method "+ methodName+"occurs excetion :" + ex );
}
/**
* 环绕通知需要携带 ProceedingJoinPoint 类型的参数
* 环绕通知类似于动态代理的全过程:ProceedingJoinPoint 这个类型的参数可以决定是否执行目标方法。
* 且环绕通知必须有返回值,返回值即为目标方法的返回值
* @param pjd
*/
//这个不是很常用的
@Around("execution(public int com.spring.aop.ArithmeticCalculator.*(..))")
public Object aroundMethod(ProceedingJoinPoint pjd) {
System.out.println("aroundMethod");
Object result = null;
String methodName = pjd.getSignature().getName();
//执行目标方法
try{
//前置通知
System.out.println("The method "+ methodName + "begins with " + Arrays.asList(pjd.getArgs()));
result = pjd.proceed();
//后置通知
System.out.println("The method "+methodName+"ends with " + result);
}catch (Throwable e) {
//异常通知
System.out.println("the method occurs exception : "+ e);
e.printStackTrace();
}
//后置通知
System.out.println("The method " + methodName + "ends");
return result;
}
}
xml配置文件
**
<!-- 配置切面的bean-->
<bean id="loggingAspect"
class="com.spring.aop.xml.LoggingAspect"></bean>
<bean id="vlidationAspect"
class="com.spring.aop.xml.VlidationAspect"></bean>
<!-- 配置AOP -->
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut expression="execution(* com.spring.aop.xml.ArithmeticCalculator.*(int , int ))" id="pointcut"/>
<!-- 配置切面及通知 -->
<aop:aspect ref="loggingAspect" order="2">
<aop:before method="beforeMethd" pointcut-ref="pointcut"/>
<aop:after method="afterMethod" pointcut-ref="pointcut"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="ex"/>
<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
</aop:aspect>
<aop:aspect ref="vlidationAspect" order="1">
<aop:before method="validateArgs" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
补充
切入点表达式可以通过操作符&& || ! 结合起来,更加的灵活
引入通知
是一种特殊的通知类型,它通过为接口提供实现类,容许对象动态的实现接口,就像对象在运行时扩展了实现 类一样。
实例代码
#### 两种配置方式的优先级
==注解的形式优先于基于XML的生命