美文网首页
SpringMVC和Hessian配置MethodInterce

SpringMVC和Hessian配置MethodInterce

作者: xin_5457 | 来源:发表于2023-10-22 11:35 被阅读0次

MethodInterceptor 是通过AOP实现方法级的拦截,常用于打日志,异常拦截等操作。
使用步骤

  1. 创建一个拦截器类实现MethodInterceptor接口
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class CustomMethodInterceptor implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        // 在这里实现您的逻辑
        System.out.println("Before invoking method: " + methodInvocation.getMethod().getName());
        Object result = methodInvocation.proceed();
        System.out.println("After invoking method: " + methodInvocation.getMethod().getName());
        return result;
    }
}
  1. 在xml中声明拦截器,并指定应用到的切面
<bean id="customInterceptor" class="com.example.CustomMethodInterceptor" />

<aop:config>
    <aop:advisor advice-ref="customInterceptor" pointcut="execution(* com.example.YourController.*(..))" />
</aop:config>

以上就可以实现Bean的拦截了。
注意:hessian的服务有所不同,因为hessian是通过HessianServiceExporter来暴露服务的,直接用pointcut无法拦截到对应请求。可以通过ProxyFactoryBean代理实现服务实现拦截。

    <bean id="myHessianServiceImpl" class="com.example.impl.MyHessianServiceImpl" /> 
    <bean id="customInterceptor" class="com.example.CustomMethodInterceptor" /> 
    <bean name="/myHessianService" class="org.springframework.remoting.caucho.HessianServiceExporter">
        <property name="service">
            <bean class="org.springframework.aop.framework.ProxyFactoryBean">
                <property name="target" ref="myHessianServiceImpl" />
                <property name="interceptorNames">
                    <list>
                        <value>customInterceptor</value>
                    </list>
                </property>
            </bean>
        </property>
        <property name="serviceInterface" value="com.example.MyHessianService"/>
    </bean>

相关文章

网友评论

      本文标题:SpringMVC和Hessian配置MethodInterce

      本文链接:https://www.haomeiwen.com/subject/hbwridtx.html