Springboot项目:dubbo用@Reference注入Realm失败

最近做项目用SpringBoot+Dubbo+Shiro做项目框架,在调试shiro自定义Realm权限的时候发现,@Reference注入的dubboServicenull;网上查了好多,但都没有完美解决问题,问题的原因是shirofilter加载顺序引起的。

解决问题的方法为:将dubbo bean转为spring bean,再用spring上下(ApplicationContext)转为dubbo bean 就可以了。

spring上下文转换工具:

package com.test.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringBeanFactoryUtil implements ApplicationContextAware {

    private static ApplicationContext context = null;

    public static <T> T getBean(String name, Class<T> type) {
        return context.getBean(name, type);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (SpringBeanFactoryUtil.context == null) {
            SpringBeanFactoryUtil.context = applicationContext;
        }
    }
}

将dubbo bean转为spring bean。先用@Reference引入,再用@Bean转换

package com.test.security.util;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dubbo.service.sys.SysUserDubboService;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class BeanFactoryUtil {

    @Reference
    SysUserDubboService sysUserDubboService;

    @Bean(name = "sysUserDubboService")
    public SysUserDubboService  getSysUserDubboService() {
        return sysUserDubboService;
    }

}

最后是在Realm中调用。

//定义一个dubboservice
 private SysUserDubboService sysUserDubboService;

调用。

//在用dubbboservice的方法调用
if (this.sysUserDubboService == null) {
            this.sysUserDubboService = SpringBeanFactoryUtil.getBean("sysUserDubboService",SysUserDubboService.class);
  }

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值