Spring框架:AOP切面的优先级
我们可以使用@Order(value)指定切面的优先级
其中value的值为:1、2、3...
值越小切面的优先级越高
举个例子
新建一个包:com.yorkmass.spring.aop
新建4个类,一个测试类(Main),一个spring配置文件applicationContext.xml
如下图所示,lib里面为需要导入的jar包
ArithmeticCalculator接口:
package com.yorkmass.spring.aop;
public interface ArithmeticCalculator {
int add(int i,int j);
int sub(int i,int j);
int mul(int i,int j);
int div(int i,int j);
}
ArithmeticCalculatorImpl类:
package com.yorkmass.spring.aop;
import org.springframework.stereotype.Component;
@Component("arithmeticCalculator")
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
@Override
public int add(int i, int j) {
int result=i+j;
// System.out.println("-->"+result);
return result;
}
@Override
public int sub(int i, int j) {
int result=i-j;
// System.out.println("-->"+result);
return result;
}
@Override
public int mul(int i, int j) {
int result=i*j;
// System.out.println("-->"+result);
return result;
}
@Override
public int div(int i, int j) {
int result=i/j;
// System.out.println("-->"+result);
return result;
}
}
LoggingAspect类:
package com.yorkmass.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.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Order(2)
@Aspect
@Component
public class LoggingAspect {
/**
* 在com.yorkmass.spring.aop.ArithmeticCalculator 接口的每一个实现类的每一个方法开始之前执行一段代码
*/
@Before("execution(public int com.yorkmass.spring.aop.ArithmeticCalculator.*(..))")
public void beforeMethod(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.yorkmass.spring.aop.ArithmeticCalculator.*(..))")
public void afterMethod(JoinPoint joinPoint){
String methodName=joinPoint.getSignature().getName();
System.out.println("The method "+methodName+" ends with ");
}
/**
* 在方法正常结束后执行的代码
* 返回通知是可以访问到方法的返回值的!
* @param joinPoint
*/
@AfterReturning(value="execution(public int com.yorkmass.spring.aop.ArithmeticCalculator.*(..))",
returning="result")
public void afterReturning(JoinPoint joinPoint,Object result){
String methodName=joinPoint.getSignature().getName();
System.out.println("The method "+methodName+" ends with "+result);
}
/**
* 在方法出现异常时会执行的代码
* 可以访问到异常对象,且可以指定在出现特定异常时再执行通知代码
* @param joinPoint
* @param ex
*/
@AfterThrowing(value="execution(public int com.yorkmass.spring.aop.ArithmeticCalculator.*(..))",
throwing="ex")
// public void afterThrowing(JoinPoint joinPoint,NullPointerException ex){
// String methodName=joinPoint.getSignature().getName();
// System.out.println("The method "+methodName+" Throwing:"+ex);
// }
public void afterThrowing(JoinPoint joinPoint,Exception ex){
String methodName=joinPoint.getSignature().getName();
System.out.println("The method "+methodName+" Throwing:"+ex);
}
/**
* 环绕通知需要携带ProceedingJoinPoint类型的参数
* 环绕通知相当于动态代理的全过程,ProceedingJoinPoint类型的参数可以决定是否执行目标方法。
* 且环绕通知必须有返回值,返回值即为目标方法的返回值
*/
@Around("execution(public int com.yorkmass.spring.aop.ArithmeticCalculator.*(..))")
public Object aroundMethod(ProceedingJoinPoint pjd){
Object result=null;
String methodName=pjd.getSignature().getName();
//执行目标方法
try {
//前置通知
System.out.println("The method "+methodName+" begin with: "+Arrays.asList(pjd.getArgs()));
result=pjd.proceed();
//返回通知
System.out.println("The method "+methodName+" end with:"+result);
} catch (Throwable e) {
// TODO: handle exception
//异常通知
System.out.println("The method "+methodName+" occurs exception:"+e);
throw new RuntimeException(e);
}
//后置通知
System.out.println("The method "+methodName+" end with");
return result;
}
}
VlidationAspect类:
package com.yorkmass.spring.aop;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* 可以使用@Order注解指定切面的优先级,值越小优先级越高
* @author Administrator
*
*/
@Order(1)
@Aspect
@Component
public class VlidationAspect {
@Before("execution(public int com.yorkmass.spring.aop.ArithmeticCalculator.*(..))")
public void validateArgs(JoinPoint joinPoint){
System.out.println("-->validate"+Arrays.asList(joinPoint.getArgs()));
}
}
applicationContext.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:aop="http://www.springframework.org/schema/aop"
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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.yorkmass.spring.aop"></context:component-scan>
<!-- 配置自动为匹配 aspectJ注解的Java类生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
Main类:
package com.yorkmass.spring.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
ArithmeticCalculator arithmeticCalculator=(ArithmeticCalculator)ctx.getBean("arithmeticCalculator");
int result=arithmeticCalculator.add(1, 5);
System.out.println("result:"+result);
result=arithmeticCalculator.div(1000, 20);
System.out.println("result:"+result);
}
}
运行结果:
一月 19, 2019 3:26:01 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1c2faae: startup date [Sat Jan 19 15:26:01 CST 2019]; root of context hierarchy
一月 19, 2019 3:26:01 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
-->validate[1, 5]
The method add begin with: [1, 5]
The method add begins with [1, 5]
The method add end with:6
The method add end with
The method add ends with
The method add ends with 6
result:6
-->validate[1000, 20]
The method div begin with: [1000, 20]
The method div begins with [1000, 20]
The method div end with:50
The method div end with
The method div ends with
The method div ends with 50
result:50
我想给她买兰博基尼