20Spring切面的优先级
2024-09-03 15:07:59
通过使用@order注解指定切面的优先级,值越小,优先级越高
代码:
package com.cn.spring.aop.impl;
//加减乘除的接口类
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);
}
package com.cn.spring.aop.impl; import org.springframework.stereotype.Component; //实现类
@Component
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
@Override
public int add(int i, int j) {
int result = i + j;
return result;
} @Override
public int sub(int i, int j) {
int result = i - j;
return result;
} @Override
public int mul(int i, int j) {
int result = i * j;
return result;
} @Override
public int div(int i, int j) {
int result = i / j;
return result;
}
}
package com.cn.spring.aop.impl; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; import java.util.Arrays;
import java.util.List; //把这个类声明为一个切面:首先需要把该类放入到IOC容器中,在声明为一个切面
//可以使用@order注解指定切面的优先级,值越小,优先级越高
@Order(2)
@Aspect
@Component
public class LoggingAspect { //声明该方法是一个前置通知:在目标方法开始之前执行
@Before("execution(public int ArithmeticCalculator.*(int, int))")
public void beforeMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("The method " + methodName + " begins with " + args);
} //后置通知:在目标方法执行后(无论是否发生异常),执行的通知
//在后置通知中还不能访问目标方法执行的结果
@After("execution(public int ArithmeticCalculator.*(int, int))")
public void afterMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("The method " + methodName + " ends with " + args);
} /**
* 在方法正常结束后执行的代码
* 返回通知是可以访问到方法的返回值
* @param joinPoint
*/
@AfterReturning(value = "execution(public int ArithmeticCalculator.*(int, int))",
returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result) {
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("The method ends witd " + result);
} //在目标方法出现异常时会执行的代码
//可以访问到异常对象;且可以指定在出现特定异常时再执行通知代码
@AfterThrowing(value = "execution(public int ArithmeticCalculator.*(int, int))",
throwing = "ex")
public void afterReturning(JoinPoint joinPoint, Exception ex) {
String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " occures exception with: " + ex);
} /**
* 环绕通知需要携带ProceedingJoinPoint类型的参数
* 环绕通知类似于动态代理的全过程:ProceedingJoinPoint类型的参数可以决定是否执行目标方法
* 且环绕通知必须有返回值,返回值为目标方法的返回值
* @param proceedingJoinPoint
*/
@Around("execution(public int ArithmeticCalculator.*(int, int))")
public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint) {
Object result = null;
String methodName = proceedingJoinPoint.getSignature().getName();
//执行目标方法
try {
//前置通知
System.out.println("The method " + methodName + " begins with " + Arrays.asList(proceedingJoinPoint.getArgs()));
result = proceedingJoinPoint.proceed();
//返回通知
System.out.println("The method ends with " + result);
} catch (Throwable throwable) {
//异常通知
System.out.println("The method " + methodName + " occures exception with: " + throwable);
throw new RuntimeException(throwable);
}
//后置通知
System.out.println("The method " + methodName + " ends");
return result;
}
}
package com.cn.spring.aop.impl; 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; import java.util.Arrays; /**
* Created by jecyhw on 2015/6/21.
*/
@Order(1)
@Aspect
@Component
public class ValidationAspect { @Before("execution(public int ArithmeticCalculator.*(..))")
public void validateArgs(JoinPoint joinPoint) {
System.out.println("validate:" + Arrays.asList(joinPoint.getArgs()));
}
}
package com.cn.spring.aop.impl; 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("17-1.xml"); //2.从IOC容器中huo获取bean的实例
ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class); //3.使用bean
int result = arithmeticCalculator.add(3, 6); System.out.println("result:" + result);
//result = arithmeticCalculator.div(3, 0);
// System.out.println("result:" + result);
}
}
<?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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.cn.spring.aop.impl">
</context:component-scan> <!--使AspjectJ注解起作用:自动为匹配的类生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
最新文章
- iOS开发中的http浅析
- [CF752D]Santa Claus and a Palindrome(优先队列,贪心乱搞)
- 如何获取Flickr图片链接地址作为外链图片
- Oracle游标示例
- Python模块 (psutil)
- java 解决JFrame不能设置背景色的问题 分类: Java Game 2014-08-15 09:48 119人阅读 评论(0) 收藏
- Java内存分配和GC
- Mysql动态多条件查询
- 华硕A450c详细清灰拆机教程
- 《HelloGitHub》第 20 期
- poj-3185-开关问题
- java Hibernate 处理 oracle xmltype类型
- python3基本数据类型
- [Aaronyang紫色博客] 写给自己的WPF4.5-Blend5公开课系列 3 - 再来一发
- JS获取节点的兄弟,父级,子级元素
- CentOS中zip压缩和unzip解压缩命令详解
- Linux下卸载安装mysql
- 使用python解决烦人的每周邮件汇总!
- Extending_and_embedding_php翻译
- 深入浅出:了解JavaScript中的call,apply,bind的差别
热门文章
- bzoj3265: 志愿者招募加强版(线性规划+单纯形法)
- ______________从时间超限到800ms 到200ms——————2098
- SwipeLayou与ScrollerView滑动冲突
- Matlab实现Butterworth滤波器 分类: 图像处理 2014-06-02 00:05 527人阅读 评论(0) 收藏
- Optimizing Downloads for Efficient Network Access
- Spirng MVC +Velocity 表单绑定命令对象
- SharePoint2013升级SP1后,运行配置向导报错:未注册sharepoint服务
- RabbitMQ四:生产者--队列--消费者
- poj3662 Telephone Lines
- css标签及属性