通过非注解的方式获取rpc服务对象

手头的项目是一个外部用spring维护,内部对象全部靠new的项目,但是又用到了rpc通信,所以想出了这种折中的方案在内部去调用rpc的service

一、motan配置
根据官方文档去配置
需要做的就是要在xml中将bean配置进去


<-- 服务端-->
<!-- service implemention bean -->
<bean id="serviceImpl" class="quickstart.FooServiceImpl" />
<!-- exporting service by Motan -->
<motan:service interface="quickstart.FooService" ref="serviceImpl" export="8002" />

<-- 客户端-->
<motan:referer id="remoteService" interface="quickstart.FooService" directUrl="localhost:8002"/>

二、需要实现ApplicationContextAware接口,通过静态方法去获取bean

@Component
public class ApplicationContextHolder implements ApplicationContextAware {
	private static ApplicationContext applicationContext;

	static {
		ApplicationContextHolder.applicationContext = null;
	}

	public static ApplicationContext getApplicationContext() {
		return ApplicationContextHolder.applicationContext;
	}

	public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
		ApplicationContextHolder.applicationContext = applicationContext;
	}
}

三、在调用时直接使用

public class MotanClientProvider() {
	
	public FooService getClient() {
	
		return (FooService) ApplicationContextHolder.getApplicationContext().getBean("remoteService")
	}
}

就可以返回可以使用的motan服务对象了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值