介绍切面的优先级。
依赖:
aspectjrt
aspectjweaver
关键代码:
使用注解表示优先级,例如
@Order(1) //数字越小优先级越高
详细:
ArithmeticCalculator.java:
package com.shuoeasy.test; /** * 定义计算器接口 * */ public interface ArithmeticCalculator { double add(double num1, double num2); double sub(double num1, double num2); double mul(double num1, double num2); double div(int num1, int num2); }
ArithmeticCalculatorImpl.java:
package com.shuoeasy.test; import org.springframework.stereotype.Component; /** * 业务类 */ @Component("arithmeticCalculator") public class ArithmeticCalculatorImpl implements ArithmeticCalculator{ public double add(double num1, double num2) { double result = num1 + num2; return result; } public double sub(double num1, double num2) { double result = num1 - num2; return result; } public double mul(double num1, double num2) { double result = num1 * num2; return result; } public double div(int num1, int num2) { double result = num1 / num2; return result; } }
LogAspect.java
package com.shuoeasy.test; 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(2) //数字越小优先级越高 @Aspect @Component public class LogAspect { // 前置通知。声明该方法是一个前置通知:在目标方法之前执行 @Before("execution(public double com.shuoeasy.test.ArithmeticCalculator.add(double, double))") public void beforeMethod(JoinPoint joinPoint){ System.out.println("日志-前置通知"); } }
VerificationAspect.java
package com.shuoeasy.test; 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(1) //数字越小优先级越高 @Aspect @Component // 验证类 public class VerificationAspect { // 前置通知。声明该方法是一个前置通知:在目标方法之前执行 @Before("execution(public double com.shuoeasy.test.ArithmeticCalculator.add(double, double))") public void beforeMethod(JoinPoint joinPoint){ System.out.println("验证-前置通知"); } }
<?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:p="http://www.springframework.org/schema/p" 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-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <!-- 自动扫描的包 --> <context:component-scan base-package="com.shuoeasy.test"></context:component-scan> <!-- 使用Aspjectj 注解起作用:自动为匹配的类生成代理对象 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
Main.java:
package com.shuoeasy.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { // 1.创建spring的IOC容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml"); // 2.从IOC容器中获取bean的实例 ArithmeticCalculator arithmeticCalulator = (ArithmeticCalculator)ctx.getBean("arithmeticCalculator"); // 3.使用bean double result = arithmeticCalulator.add(2, 3); System.out.println("result=" + result); } }
输出:
验证-前置通知 日志-前置通知 result=5.0