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

Recently I’m working on a small module using Spring MVC + MyBites + Spring Shiro

When making Shiro’s Realm auto-inject, it always gets an error.
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

Entangled for a long time, finally found a solution on the Internet
Shiro’s Realm is a Filte, so when it is loaded in web.xml, it will load Filete before Spring, so @Autowired will never be able to find the bean in the Realm. In fact, after the container is started, the Web.xml configuration load order is
ServletContext – context – param – the listener – filter – servlet
Therefore, simply load the Spring configuration file in advance, i.e., place the Spring configuration file before the Shiro configuration file in web.xml, as shown below:

After the above configuration changes, restart the service!
The rest of the injection configuration remains the same, there is no need to set any @Resource, the previous @Autowired will be used.

Read More: