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
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:
- Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of ty
- Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException
- org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name
- org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘use
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘serviceImpl‘
- Spring configuration error: org.springframework.beans . factory.BeanCreationException : Error creating bean with name ‘m
- org.springframework.beans.factory.UnsatisfiedDependencyException
- appear org.springframework.beans . factory.BeanCreationException Causes and solutions of abnormal
- Caused by: org.springframework.beans.factory.beancreationexception: error creating be
- Error in project operation org.springframework.beans . factory.BeanCreationException : error creating bean with name can be configured by
- No qualifying bean of type ‘javax.sql.DataSource‘ available: expected at least 1
- Org.springframework.beans.factory.xml.xmlbeandefinitionstoreexception: the wildcard matching is comprehensive, but the declaration of element XX: XX XX cannot be found
- Unable to read Scheme Document ‘ http://www.springframework.org/schema/beans/spring-beans-4.1.xsd ‘ [How to Fix]
- Error creating bean with name ‘feignTargeter‘ defined in class path resource [org/springframework/cl
- Error creating bean with name ‘org.springframework.security.oauth2.config.annotation.web.configurati
- Image file upload error org.springframework.beans .ConversionNotSupportedException
- org.springframework.dao.InvalidDataAccessApiUsageException: Bean object must not be null; nested exc
- Spring nested exception is java.lang.NoClassDefFoundError: org/springframework/core/MethodClassKey
- org.springframework.core.NestedIOException: Failed to parse mapping resource: ‘file