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

来源:互联网 发布:c语言字符串指针传递 编辑:程序博客网 时间:2024/06/06 04:37


最近在弄个小模块,用的是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。





阅读全文
1 0