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

你可能感兴趣的:(spring,AOP,bean,String,Class)