直接看代碼
springmvc中的配置aop對 controller和它的子包進行攔截
springmvc中的配置
<!--xml頭部配置需要有這幾行代碼--> xmlns:aop="http://www.springframework.org/schema/aop" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd <aop:aspectj-autoproxy proxy-target-class="true" /> <bean id="logAopAction" class="com.um.framework.baseware.webadmin.modules.controller.core.LogAopAction"/> <!--aop執行操作的類-->
package com.um.framework.baseware.webadmin.modules.controller.core; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @Aspect public class LogAopAction { //配置接入點,如果不知道怎么配置,可以百度一下規則 @Pointcut("execution(* com.um.framework.baseware.webadmin.modules.controller..*.*(..))") private void anyMethod(){ System.out.println("log記錄------------------------"); }//定義一個切入點 @Before("anyMethod() && args(name)") public void doAccessCheck(String name){ System.out.println(name); System.out.println("前置通知--------------------------"); } @AfterReturning("anyMethod()") public void doAfter(){ System.out.println("后置通知-------------------------"); } @After("anyMethod()") public void after(){ System.out.println("最終通知---------------------------"); } @AfterThrowing("anyMethod()") public void doAfterThrow(){ System.out.println("例外通知-----------------------------------"); } @Around("anyMethod()") public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{ System.out.println("進入環繞通知------------------------------"); Object object = pjp.proceed();//執行該方法 System.out.println("退出方法--------------------"); return object; } }