博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在非 bean 对象中注入 dubbo service
阅读量:2378 次
发布时间:2019-05-10

本文共 797 字,大约阅读时间需要 2 分钟。

首先需要一个spring静态获取bean的工具类

@Componentpublic class SpringContextBeanUtil implements ApplicationContextAware {
private static ApplicationContext context; @Override public void setApplicationContext(ApplicationContext context) throws BeansException {
SpringContextBeanUtil.context = context; } public static
T getBean(Class
beanClass) {
return context.getBean(beanClass); } public static String getMessage(String key) {
return context.getMessage(key, null, Locale.getDefault()); }}

然后创建一个component 用reference注入dubbo的service

@Componentpublic class RedisComponent {
@Reference(version = "1.0") private IRedisService redisService; public IRedisService getRedisService() {
return redisService; }}

最后在非bean对象中调用方法获取service

在这里插入图片描述
即可正常使用

转载地址:http://jpaxb.baihongyu.com/

你可能感兴趣的文章
Linux 安装软件、配置静态IP
查看>>
JavaScript 函数、闭包
查看>>
JavaScript BOM
查看>>
JavaScript Date、Math对象、杂项
查看>>
Bootstrap的使用
查看>>
Java IO
查看>>
Java JDBC
查看>>
JVM 类加载机制、对象的创建过程
查看>>
SpringBoot整合AOP
查看>>
消息中间件 MQ
查看>>
Spring | SpringBoot 理论总结
查看>>
Redis 简介、安装、配置、启动、连接
查看>>
Redis 集群、哨兵、主从同步
查看>>
在Java中操作Redis,使用Redis实现分布式锁
查看>>
MySQL 事务
查看>>
SpringCloud Ribbon 负载均衡
查看>>
SpringCloud Feign 声明式服务调用
查看>>
JVM 垃圾回收机制、垃圾收集器、调优参数
查看>>
JDK自带的JVM工具
查看>>
SpringCloud Hystrix 容错保护
查看>>