美文网首页
解决dubbo + shiro realm不能注入service

解决dubbo + shiro realm不能注入service

作者: 昵称违规 | 来源:发表于2018-11-01 14:29 被阅读0次

    项目 采用dubbo+shiro 进行权限管理,shiro放在消费者里面,在shiro 的realm中药注入用户的service。采用这种方式,

    public class OAuth2Realmextends AuthorizingRealm

    @Reference(version ="1.0")

    private SysUserServicesysUserService;

    @Reference(version ="1.0")

    private SysUserTokenServicesysUserTokenService;

    用refrence 注入,service为空,在网上查找原因为,在shiro执行过滤器时,dubbo还没注入进来,所以获取不到,网上也有很多种方法,

    我采用的是将dubbo的service交给spring管理,通过ApplicationContext去取bean,这样就成功了,就是比较麻烦。

    @Reference(version ="1.0")

    private SysUserServicesysUserService;

    @Reference(version ="1.0")

    private SysUserTokenServicesysUserTokenService;

    /**

    * =============  把service交给spring管理,解决shiro  realm  service注入失败问题==============

    */

    @Bean(name ="sysUserService")

    public SysUserServicegetSysUserService(){

    return sysUserService;

    }

    @Bean(name ="sysUserTokenService")

    public SysUserTokenServicegetSysUserTokenService(){

    return sysUserTokenService;

    }

    /**

    * =============  把service交给spring管理,解决shiro  realm  service注入失败问题==============

    */

    调用:

    this.sysUserTokenService = ApplicationContextHolder.getBean("sysUserTokenService");

    this.sysUserService = ApplicationContextHolder.getBean("sysUserService");

    public static T getBean(String name)throws BeansException {

    return (T)applicationContext.getBean(name);

    }

    解决问题,应该还有更简洁,更好的方法,期待大神解答!!

    相关文章

      网友评论

          本文标题:解决dubbo + shiro realm不能注入service

          本文链接:https://www.haomeiwen.com/subject/kuhxxqtx.html