使用 aop拦截 springMVC的controller并获取请求参数及返回结果
2016-06-08 15:16
736 查看
有人说使用aop拦截不到springMVC的controller,一般出现此种情况大多是由于配置错误造成,不废话直接进入主题:
1、applicationContext.xml 配置扫描 除@controller外的bean
<context:component-scan base-package="XXX" scoped-proxy="targetClass">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
2、 applicationContext-mvc.xml 配置扫描 @controller bean
<mvc:annotation-driven />
<aop:aspectj-autoproxy proxy-target-class="true"/>
<context:component-scan base-package="xxxxx.controller" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
3、编写 aop相关bean
①、拦截指定方法
@Pointcut("execution(* XXX.gatewayDelFromUser(..))")
public void deleGateway(){
}
@AfterReturning(pointcut ="deleGateway() && args(req,request)",returning="result")
public void afterReturnExcute(GatewayDelFromUserRequestMsg req,HttpServletRequest request,
BaseResponseMsg result) {
logger.info ("*******************************respMsg is :[{}],the result[{}]",req,result);
}
注意:此处args中的参数个数需要与拦截的方法个数相同,否则会拦截不到
②、自定义注解
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface AttributeChange {
String value() default "";
}
@Pointcut("@annotation(com.sengled.cloud.zigbee.aop.openapi.AttributeChange)")
public void arrChange(){
}
@Before("arrChange() && args(message)")
public void beforExcute(Object message) {
}
在需要拦截的方法上添加 @AttributeChange 注解
1、applicationContext.xml 配置扫描 除@controller外的bean
<context:component-scan base-package="XXX" scoped-proxy="targetClass">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
2、 applicationContext-mvc.xml 配置扫描 @controller bean
<mvc:annotation-driven />
<aop:aspectj-autoproxy proxy-target-class="true"/>
<context:component-scan base-package="xxxxx.controller" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
3、编写 aop相关bean
①、拦截指定方法
@Pointcut("execution(* XXX.gatewayDelFromUser(..))")
public void deleGateway(){
}
@AfterReturning(pointcut ="deleGateway() && args(req,request)",returning="result")
public void afterReturnExcute(GatewayDelFromUserRequestMsg req,HttpServletRequest request,
BaseResponseMsg result) {
logger.info ("*******************************respMsg is :[{}],the result[{}]",req,result);
}
注意:此处args中的参数个数需要与拦截的方法个数相同,否则会拦截不到
②、自定义注解
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface AttributeChange {
String value() default "";
}
@Pointcut("@annotation(com.sengled.cloud.zigbee.aop.openapi.AttributeChange)")
public void arrChange(){
}
@Before("arrChange() && args(message)")
public void beforExcute(Object message) {
}
在需要拦截的方法上添加 @AttributeChange 注解
相关文章推荐
- (Java)LeetCode-27. Remove Element
- ES2.x版本的javaAPI使用实例
- Java 数据0小数点前无显示
- 汤阳光OA系统记录-----Hibernate实体映射
- [转]IntelliJ IDEA像Eclipse一样打开多个项目
- Spring定时服务QuartZ
- spring整合Ehcache缓存框架
- jre jdk区别
- java线程池实现批处理的简单demo
- 解决Mybatis框架java.sql.SQLException: 数据大小超出此类型的最大值的问题
- (Java)LeetCode-26. Remove Duplicates from Sorted Array
- 13 java.io.*包 和输入、输出类构成。
- java 通过url下载文件到浏览
- 优秀JAVA第三方库
- 关于java的一点成长
- spring mvc DispatcherServlet详解之一---处理请求深入解析
- Eclipse自动补全编辑时出现闪退
- Spring Annotation Processing: How It Works--转
- 转:java服务器安全指南
- java编码之BASE64