5.23
在spring boot中dubbo与shiro整合,出现问题,自定义MyRealm始终无法整合进dubbo,一旦@Reference,就报null。研究了许久,在不断的看源码和找资料的路程,终于看明白了一些,原来是dubbo和shiro的优先顺序不对,shiro优先度高于dubbo,因此shiro中自定义MyRealm,无法获取到user接口。UserService 所以一直报null。
解决方案:
1.先引用dubbo bean(也就是@Reference)在Controller引用
@Reference(version = "1", check = false)
private UserService UserService;
2.在Controller中 把dubbo bean 转为spring bean
@Bean(name = "UserService")
public UserService getIAccountService(){
return UserService;
}
3.写spring上下文工具( 这个要放在消费者项目里面)
@Component
public class SpringBeanFactoryUtils implements ApplicationContextAware {
private static ApplicationContext context = null;
public static <T> T getBean(Class<T> type) {
return context.getBean(type);
}
public static <T> T getBean(String name, Class<T> type) {
return context.getBean(name, type);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.err.println("SpringBeanFactoryUtils be inited...");
if (SpringBeanFactoryUtils.context == null) {
SpringBeanFactoryUtils.context = applicationContext;
}
}
}
4.最后在自定义MyRealm的方法中加入(注意是方法中加入)
`UserService UserService = SpringBeanFactoryUtil.getBean("UserService",UserService.class)`;
PS:还有一种方法是写dubbo的xml文件,在此我只研究了一种方法。