当接口被调用时使用Spring拦截器注入运行时数据
来源:互联网 发布:临床医学网络教育 编辑:程序博客网 时间:2024/05/21 15:07
开发背景
使用CXF暴露与调用接口,为了方便追踪错误,所以想要在接口被调用时将一些运行时数据记录起来,所以就想到了拦截器。
CXF自带拦截器,但是据我初步了解,自带的拦截器都是经过封装,用来打印日志什么的,好像没有提供给开发者定制功能的方式(没有深入了解,如果有说错请方便斧正)
流程
其实Spring的拦截器使用挺方便的,实现org.aopalliance.intercept.MethodInterceptor接口中的invoke方法,在方法中实现想要的逻辑。然后在Spring配置文件中注入它就了事。
代码
拦截器实现方法
@Override public Object invoke(MethodInvocation invocation) throws Throwable { //获取当前被调用的方法 Method method = invocation.getMethod(); String methodName = method.getDeclaringClass().getSimpleName() + "." + method.getName(); //执行被拦截器拦截下来的方法,获得其返回值 Object object = invocation.proceed(); //做自己的逻辑,我这里将被调用的方法信息放入返回值中。 if (object instanceof SimpleResult) { SimpleResult result = (SimpleResult) object; result.createMsg(methodName); } return object; }
将拦截器注入IOC容器中
<bean id="resultLogInterceptor" class="com.elin4it.demo.interceptor.ResultLogInterceptor"/>
配置拦截器
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <!--配置拦截器名--> <property name="interceptorNames"> <list> <value>resultLogInterceptor</value> </list> </property> <!--要拦截的接口,可以使用通配符--> <property name="beanNames"> <list> <value>*Facade</value> </list> </property></bean>
0 0
- 当接口被调用时使用Spring拦截器注入运行时数据
- Spring运行时注入
- 当项目使用SpringMvc时拦截器的注意事项
- Spring Mvc 公共Controller 使用拦截器注入请求信息
- spring boot 拦截器 注入dao 操作数据库时dao 为null
- Spring拦截器使用
- Spring使用注解注入接口实现类
- CXF整合spring框架实现动态调用,找不到函数接口, 添加拦截器无法初始化
- spring 方法拦截器 MethodInterceptor接口
- spring Ioc 接口注入时的问题分析
- spring 拦截器无法拦截类内部的方法调用
- Spring AOP拦截器调用实现
- 运行时注入值
- Spring注入对象调用时为空的异常
- Spring 接口注入
- Spring 接口注入
- Spring的接口注入
- Spring 拦截器的使用
- /usr/bin/ld: cannot find -lmysqlclient
- ReactNative.createElement is deprecated. Use React.createElement from the "react" package instead.
- Android货币单位获取方法
- using写在命名空间外面和里面,有什么区别与讲究?
- 常用的正则表达式
- 当接口被调用时使用Spring拦截器注入运行时数据
- while(cin>>string)的使用方法
- php编译(apache)
- spring readonly
- JS报错,但是语法没有写错
- ElasticSearch数据导入Hive
- BAT脚本编写教程(比较易懂和全面)
- Android图片加载神器之Fresco-加载图片基础[详细图解Fresco的使用](秒杀imageloader)
- matlab libsvm支持向量机工具箱安装与使用