SpringAop @Aspect织入不生效,不执行前置增强织入@Before方式

作者:苦作舟 时间:2021-09-18 04:00:58 

SpringAop @Aspect织入不生效,不执行前置增强织入@Before

想写一个AOP,主要有2个用意

  • 第一个用意是做后端的防表单重复提交的token验证。

  • 第二个用意是对后台JSR303 Validator的校验结果做一个统一处理,不想把对校验结果的处理分散在每个controller方法中


@ResponseBody
@RequestMapping(value = "add", method = RequestMethod.POST)
public ResponseModel add(@Valid User user, BindingResult br, HttpServletResponse response) {

if(br.hasErrors()) {
return ResponseModel.validFail(getErrorsSplitNewLine(br));
}
accountService.addUser(user);
return ResponseModel.success("保存用户成功");
}

如上面方法中, br.hasErrors() 在每个表单提交方法中都存在,想单独抽出来使用AOP统一处理。

所以写一个AOP,如下:


@Aspect
@Component
public class ParamValidAspect {
   @Before("@annotation(com.hebao.tech.adm.framework.annotation.ParamValid)")
   public void paramValid(JoinPoint point) {
System.out.println("参数校验切入方法被调用了.....");
       //省略
   }
}

由于这篇文章主要是记录AOP不生效的原因,所以,这里不写具体实现了。

上面的内容定义一个Aop织入,在有注解@ParamValid的注释Controller方法上织入。


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ParamValid {

}

这个ParamValid的内容,仅仅是一个标志性的注解,声明为方法层的注解,并且是运行时注解。

最后在application.xml中加入AOP * 设置。


<!-- 这个配置要配置在component-scan以后 -->
<aop:aspectj-autoproxy proxy-target-class="true" />

如果spring配置文件没引入过aop的配置,还需要在加入xml声明

SpringAop @Aspect织入不生效,不执行前置增强织入@Before方式

大功告成,测试了一下,发现有点悲剧,根本织入不生效,也不报错,,楞是不执行相关的织入代码。

最后在网上搜了一下,发现Spring与SpringMVC是2个不同的父子容器, @Aspect如果被spring容器加载的话,而@Controller注解的这些类的实例化以及注入却是由SpringMVC来完成。 @Aspect如果被spring容器加载的时候,可能Spring MVC容器还未初始化, Controller类还未初始化,所以无法正常织入。。

所以调整如下:


@Aspect
public class ParamValidAspect {
   @Before("@annotation(com.hebao.tech.adm.framework.annotation.ParamValid)")
   public void paramValid(JoinPoint point) {
System.out.println("参数校验切入方法被调用了.....");
       //省略
   }
}

去掉@Component注解,然后把 aop:aspectj-autoproxy 移入springmvc配置文件中,并定义bean,如下:


<!-- 这个配置一定要配置在component-scan以后 -->
<aop:aspectj-autoproxy proxy-target-class="true" />
<bean id="paramValidAspect" class="com.hebao.tech.adm.framework.spring.aop.ParamValidAspect"/>

这样就大功告成了。

使用@Aspect,@Before不被调用


@Aspect
@Component
public class LogAspect {
   @Before("pointcut()")
   public void before(){
       System.out.println("before");
   }

@Pointcut("@annotation(com.demo.annotation.Log)")
   public void pointcut(){    
   }

@Around("pointcut()")
   public void around(){
       System.out.println("arount");
   }

@After("pointcut()")
   public void after(){
       System.out.println("after");
   }
}

调用方法返回结果:

arount

after


@Aspect
@Component
public class LogAspect {
   @Before("pointcut()")
   public void before(){
       System.out.println("before");
   }

@Pointcut("@annotation(com.mxy.annotation.Log)")
   public void pointcut(){

}

@Around("pointcut()")
   public void around(ProceedingJoinPoint point){
       System.out.println("arount before");

try {
           point.proceed();
       } catch (Throwable throwable) {
           throwable.printStackTrace();
       }
       System.out.println("arount after");
   }

@After("pointcut()")
   public void after(){
       System.out.println("after");
   }
}

调用返回结果:

arount before

before

arount after

after

来源:https://blog.csdn.net/oKuZuoZhou/article/details/81015310

标签:SpringAop,@Aspect织入,织入@Before
0
投稿

猜你喜欢

  • java反射深入剖析(推荐)

    2022-10-10 18:50:35
  • 有关于整体刷新和局部刷新frameset窗口

    2023-04-21 22:43:40
  • Android Studio设置绘制布局时的视图

    2021-09-05 08:15:46
  • 使用logback实现按自己的需求打印日志到自定义的文件里

    2022-05-12 16:56:49
  • Eclipse下Javassist正确使用方法代码解析

    2021-07-24 18:43:57
  • Java Socket实现多线程通信功能示例

    2022-11-29 03:24:59
  • 详解如何让Spring MVC显示自定义的404 Not Found页面

    2023-12-12 15:48:47
  • 基于Java8实现提高Excel读写效率

    2023-11-25 10:01:37
  • MyBatis中的模糊查询语句

    2022-09-30 03:36:59
  • springmvc参数为对象,数组的操作

    2022-04-20 07:38:03
  • C#在WinForm中使用WebKit传递js对象实现与网页交互的方法

    2021-05-26 00:07:08
  • Android实现长按back键退出应用程序的方法

    2023-03-26 21:49:43
  • c# 通过经纬度查询 具体的地址和区域名称

    2023-08-15 11:41:53
  • c#操作xml帮助类分享(xml增删改查)

    2022-03-02 04:09:21
  • Android 将 android view 的位置设为右下角的解决方法

    2022-02-26 19:01:27
  • springboot开启声明式事务的方法

    2021-06-18 23:50:48
  • MybatisPlus字段类型转换的实现示例

    2022-12-09 22:29:13
  • SpringBoot深入分析讲解监听器模式上

    2022-06-25 21:04:04
  • spring boot结合Redis实现工具类的方法示例

    2023-05-12 09:47:46
  • Eclipse 开发java 出现Failed to create the Java Virtual Machine错误解决办法

    2021-12-24 10:35:50
  • asp之家 软件编程 m.aspxhome.com