我是靠谱客的博主 冷艳大叔,这篇文章主要介绍实现dubbo的Filter接口,如何注入spring容器里的bean,现在分享给大家,希望可以做个参考。

最近项目里用到dubbo的filter来做一些预处理的业务,但发现继承了Filter接口的类,无法通过@Autowired或者@Resource来注入spring容器里的对象,比如有个TestService,可以通过下面两种方式注入进来

1.dubbo通过setter方式自动注入

复制代码
1
2
3
4
5
6
private TestService testService; public void setTestService(TestService testService) { this.testService = testService; }

2.第二种,通过ApplicationContext的方式来获取

可以自己先实现一个SpringUtil实现ApplicationContextAware接口,如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Component public class SpringUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if (SpringUtil.applicationContext == null) { SpringUtil.applicationContext = applicationContext; } } // 获取applicationContext public static ApplicationContext getApplicationContext() { return applicationContext; } // 通过name获取 Bean. public static Object getBean(String name) { return getApplicationContext().getBean(name); } }

然后调用这个工具类来获取对象

复制代码
1
TestService testService = (TestService) SpringUtil.getBean("testService");

 

最后

以上就是冷艳大叔最近收集整理的关于实现dubbo的Filter接口,如何注入spring容器里的bean的全部内容,更多相关实现dubbo内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部