解决dubbo + shiro realm不能注入service

2018-11-01  本文已影响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);

}

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

上一篇下一篇

猜你喜欢

热点阅读