springmvc使用aop心得

时间:2021-04-27 14:38:05

第一步:创建aop拦截类:

 @Component
@Aspect
public class ControllerSelectorInterceptor {
@Before("execution(* com.my.test..*.*Controller.*(..))")
public void doBefore(JoinPoint jp) {
StringBuffer method = new StringBuffer();
method.append(jp.getTarget().getClass().getName()).append(".").append(jp.getSignature().getName());
Object[] args = jp.getArgs(); HttpServletRequest request = null;
for(Object arg : args){
if(arg instanceof HttpServletRequest){
request = (HttpServletRequest)arg;
}
}
} @After("execution(* com.my.test..*.*Controller.*(..))")
public void doAfter(JoinPoint jp) {
Object[] args = jp.getArgs();
StringBuffer method = new StringBuffer();
method.append(jp.getTarget().getClass().getName()).append(".").append(jp.getSignature().getName()); HttpServletRequest request = null;
for(Object arg : args){
if(arg instanceof HttpServletRequest){
request = (HttpServletRequest)arg;
}
}
String account = request.getParameter("accountName");
}
}

第二步:在配置文件中加入以下配置:

     <aop:aspectj-autoproxy  proxy-target-class="true" />

注:aop的详细介绍可以参考:http://www.360doc.com/content/11/1103/09/4280915_161262623.shtml