SpringAop @Aspect織入不生效,不執(zhí)行前置增強(qiáng)織入@Before方式
SpringAop @Aspect織入不生效,不執(zhí)行前置增強(qiáng)織入@Before
想寫一個(gè)AOP,主要有2個(gè)用意
- 第一個(gè)用意是做后端的防表單重復(fù)提交的token驗(yàn)證。
- 第二個(gè)用意是對(duì)后臺(tái)JSR303 Validator的校驗(yàn)結(jié)果做一個(gè)統(tǒng)一處理,不想把對(duì)校驗(yàn)結(jié)果的處理分散在每個(gè)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() 在每個(gè)表單提交方法中都存在,想單獨(dú)抽出來使用AOP統(tǒng)一處理。
所以寫一個(gè)AOP,如下:
@Aspect @Component public class ParamValidAspect { @Before("@annotation(com.hebao.tech.adm.framework.annotation.ParamValid)") public void paramValid(JoinPoint point) { System.out.println("參數(shù)校驗(yàn)切入方法被調(diào)用了....."); //省略 } }
由于這篇文章主要是記錄AOP不生效的原因,所以,這里不寫具體實(shí)現(xiàn)了。
上面的內(nèi)容定義一個(gè)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 { }
這個(gè)ParamValid的內(nèi)容,僅僅是一個(gè)標(biāo)志性的注解,聲明為方法層的注解,并且是運(yùn)行時(shí)注解。
最后在application.xml中加入AOP動(dòng)態(tài)代理設(shè)置。
<!-- 這個(gè)配置要配置在component-scan以后 --> <aop:aspectj-autoproxy proxy-target-class="true" />
如果spring配置文件沒引入過aop的配置,還需要在加入xml聲明
大功告成,測(cè)試了一下,發(fā)現(xiàn)有點(diǎn)悲劇,根本織入不生效,也不報(bào)錯(cuò),,楞是不執(zhí)行相關(guān)的織入代碼。
最后在網(wǎng)上搜了一下,發(fā)現(xiàn)Spring與SpringMVC是2個(gè)不同的父子容器, @Aspect如果被spring容器加載的話,而@Controller注解的這些類的實(shí)例化以及注入?yún)s是由SpringMVC來完成。 @Aspect如果被spring容器加載的時(shí)候,可能Spring MVC容器還未初始化, Controller類還未初始化,所以無法正??椚搿?。
所以調(diào)整如下:
@Aspect public class ParamValidAspect { @Before("@annotation(com.hebao.tech.adm.framework.annotation.ParamValid)") public void paramValid(JoinPoint point) { System.out.println("參數(shù)校驗(yàn)切入方法被調(diào)用了....."); //省略 } }
去掉@Component注解,然后把 aop:aspectj-autoproxy 移入springmvc配置文件中,并定義bean,如下:
<!-- 這個(gè)配置一定要配置在component-scan以后 --> <aop:aspectj-autoproxy proxy-target-class="true" /> <bean id="paramValidAspect" class="com.hebao.tech.adm.framework.spring.aop.ParamValidAspect"/>
這樣就大功告成了。
使用@Aspect,@Before不被調(diào)用
@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"); } }
調(diào)用方法返回結(jié)果:
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"); } }
調(diào)用返回結(jié)果:
arount before
before
arount after
after
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springBoot+mybaties后端多層架構(gòu)的實(shí)現(xiàn)示例
本文主要介紹了springBoot+mybaties后端多層架構(gòu)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-072種Java刪除ArrayList中的重復(fù)元素的方法
這篇文章主要介紹了2種Java刪除ArrayList中的重復(fù)元素的方法,感興趣的朋友可以參考下2015-08-08Java并發(fā)編程之CountDownLatch原理詳解
這篇文章主要介紹了Java并發(fā)編程之CountDownLatch原理詳解,CountDownLatch類中使用了一個(gè)繼承自AQS的共享鎖Sync對(duì)象,構(gòu)造CountDownLatch對(duì)象時(shí)會(huì)將傳入的線程數(shù)值設(shè)為AQS的state值,需要的朋友可以參考下2023-12-12Spring案例打印機(jī)的實(shí)現(xiàn)過程詳解
這篇文章主要介紹了Spring案例打印機(jī)的實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10利用spring aop實(shí)現(xiàn)動(dòng)態(tài)代理
這篇文章主要為大家詳細(xì)介紹了利用spring aop實(shí)現(xiàn)動(dòng)態(tài)代理的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Java打包之后讀取Resources下的文件失效原因及解決方法
這篇文章主要給大家介紹了Java打包之后讀取Resources下的文件失效的問題分析和解決方法,文中通過代碼示例和圖文結(jié)合給大家講解非常詳細(xì),需要的朋友可以參考下2023-12-12解析Spring事件發(fā)布與監(jiān)聽機(jī)制
本篇文章給大家介紹Spring事件發(fā)布與監(jiān)聽機(jī)制,通過 ApplicationEvent 事件類和 ApplicationListener 監(jiān)聽器接口,可以實(shí)現(xiàn) ApplicationContext 事件發(fā)布與處理,需要的朋友參考下吧2021-06-06