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


最近在弄个小模块,用的是spring mvc + mybites + spring shiro

shiro错误 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type_第1张图片

在弄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配置文件之前,如下图:

shiro错误 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type_第2张图片

shiro错误 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type_第3张图片



按照上面的配置修改后,重启服务即可!

其他的注入配置不变,不需要设置什么@Resource,还是用之前的@Autowired。





你可能感兴趣的:(#,SSH)