SpringMVC拦截Controller方法

来源:互联网 发布:pokemon go挂机软件 编辑:程序博客网 时间:2024/05/21 17:28

spring配置:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"   xmlns:context="http://www.springframework.org/schema/context"   xmlns:aop="http://www.springframework.org/schema/aop"   xsi:schemaLocation="        http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-3.2.xsd        http://www.springframework.org/schema/aop      http://www.springframework.org/schema/aop/spring-aop-3.2.xsd        http://www.springframework.org/schema/mvc      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd              ">

<!-- 【启用springmvc注解驱动】 --><mvc:annotation-driven />

<!-- 使用cglib代理 --><aop:aspectj-autoproxy proxy-target-class="true"/>

<bean id="runAdvisor"     class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">   <property name="advice">      <bean class="com.**.api.web.interceptor.MyInterceptor"></bean>   </property>   <property name="mappedNames">      <list>         <value>getDetails</value><!-- 要拦截的方法,用*号匹配多个 -->      </list>   </property></bean><!-- 使用ProxyFactoryBean 产生代理对象 --><bean      class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">   <property name="beanNames">      <list>               <!-- 注意:如果是Controller,首字母不要小写了 -->         <value>MyController</value><!-- 要拦截的bean,可以用*号匹配多个。-->      </list>   </property>   <property name="interceptorNames">      <list>         <value>runAdvisor</value>      </list>   </property></bean>


拦截处理类:

public class MyInterceptor implements MethodInterceptor {    @Override    public Object invoke(MethodInvocation methodInvocation) throws Throwable {        if (条件判断) {            JSONObject jsonObject = new JSONObject();            jsonObject.put("error", "失败返回");            return jsonObject;        }        return methodInvocation.proceed();    }}

阅读全文
0 0
原创粉丝点击