您的位置:首页 > 编程语言 > Java开发

shiro错误 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type

2017-08-16 17:09 846 查看
最近在弄个小模块,用的是spring mvc + mybites + spring shiro



在弄shiro 的Realm自动注入的时候总是报错,
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.sam.project.service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:618)
... 44 more

纠结许久,终于在网上查询到了解决方案
Shiro的Realm 是属于Filte,因此在web.xml里面加载的时候会先加载Filete,然后才加载到Spring,所以Realm中@Autowired总是找不到bean,其实容器启动后,web.xml的配置加载顺序是
ServletContext--context-param--listener--filter--servlet 

因此,只要把Spring配置文件提前加载即可,即:在web.xml中把spring配置文件放在Shiro配置文件之前,如下图:





按照上面的配置修改后,重启服务即可!
其他的注入配置不变,不需要设置什么@Resource,还是用之前的@Autowired。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐