SpringAOP动态拦截方法并重写
来源:互联网 发布:雾炮机 知乎 编辑:程序博客网 时间:2024/06/07 03:18
需求理解:当我们在对某个方法进行重写的时候,我们不想修改源代码,希望通过读取配置文件,获取标识符,判断是否需要重写,在不修改源代码的基础上实现动态的修改了方法。
在这里我们用到了SpringAOP的Aspect注解,通过添加@Around注解实现拦截指定切点的方法。
下面代码举例:
@Aspect
@Componentpublic class AroundAop {//创建标识符private static String flag;//在静态代码块中获取配置文件中的标识符值static {//封装的通过ResourceLoader实现的读取指定路径下配置文件的工具类PropertiesLoader propertiesLoader = new PropertiesLoader("classpath:config.properties");flag = propertiesLoader.getProperty("httpsflag");}//指定切点位置@Around("execution(* getSum(String,String))")public String aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println("从配置文件中获取的标志符为: "+flag);//获取当前方法的参数Object[] args = joinPoint.getArgs();//修改当前方法的参数args[0] = "在环绕方法中拦截当前方法,并修改的参数一";args[1] = " success";//更改参数后执行方法/*String result = (String) joinPoint.proceed(args);*///如果不想执行切点方法就将上面的proceed注释,执行你需要执行的代码String result = "这是我自定义的方法执行体";System.out.println("自定义的方法内容: "+result);return null;}}我们需要理清的思路是:
我们需要拦截切点的方法,然后无论是重写还是修改参数都可以随意操作。
需要做的点:
1、引入约束
<dependency><groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId></dependency><dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId></dependency><dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId></dependency>2、编写AOP类,在里面指定切点与重写的方法
注意:1、这里只能使用@Around注解
2、在方法中的参数为:ProceedingJoinPoint
3、如果需要重写 就调用proceed的方法,否者直接进行替换
阅读全文
0 0
- SpringAOP动态拦截方法并重写
- SpringAOP MethodInterceptor方法拦截器
- js 继承并重写方法示例
- java:继承并重写的方法
- SpringAOP拦截Controller总结
- 验证static的作用并重写toString方法
- SpringAOP动态代理小结
- jdk动态代理拦截方法
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)里面的坑之 同一个controller部分方法可以拦截,部分方法拦截不了
- 实现Comparable接口和Comparator接口,并重写compareTo方法和compare方法
- java之继承并重写
- IE浏览器html post拦截并重定向
- 4、创建产品分类的业务类并重写删除方法
- 让EasyUI的DataGrid直接内嵌的JSON对象,并重写formload方法
- MVC继承Controller类并重写OnException方法实现全局错误日志
- 多线程实现方式1:自定义一个类,继承Thread类,并重写run方法。
- Java:创建继承于类Person的类Student并重写方法
- 创建继承于类person的类student并重写方法
- laravel基础
- HQL多表查询
- 在Eclipse 中设置JVM 内存
- leetcode[Third Maximum Number]
- 9265:取数游戏
- SpringAOP动态拦截方法并重写
- Kotlin系统入门与进阶(一)
- Java中使用TCP协议的简单连接
- 利用ES6中的模板字符串轻松实现多行和字符串值的插入
- maven exclude 排除文件夹
- cross entropy
- intell IDEA 代码自动提示功能没有LE ---File >Power Save Mode 被勾选了 去掉勾选就可以了
- 简单排序——选择排序
- equals与"==" 同时判断时前后位置不同的后果