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


最近在弄个小模块,用的是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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值