1.定義一個AOP:
package org.yang.aop;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class ControllerAfterAdvice
{
@AfterReturning("@annotation(org.springframework.web.bind.annotation.RequestMapping) && (args(response, userName,..) || args(..,response, userName))")
public void after(HttpServletResponse response, String userName) throws IOException
{
response.getWriter().write("");
System.out.println("userName:" + userName);
System.out.println("執行了After Advice");
}
}
2.在spring的上下文配置中定義一個<Bean>
3.加上:<aop:aspectj-autoproxy proxy-target-class="true"/>:作用:允許AspecJ攔截Controller
4.再加上:<context:component-scan base-package="org.yang.aop"></context:component-scan>:作用:使註解生效