SpringMVC中使用AOP

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>:作用:使註解生效

發佈了212 篇原創文章 · 獲贊 71 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章