本文主要是介绍springboot shiro Realm 无法自动注入,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘rabbitConnectionFactoryMetricsPostProcessor’ defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/amqp/RabbitMetricsAutoConfiguration.class]: Unsatisfied dependency expressed through method ‘rabbitConnectionFactoryMetricsPostProcessor’ parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘shiroFilterFactory’ defined in class path resource [com/common/config/ShiroConfig.class]: Unsatisfied dependency expressed through method ‘shiroFilterFactory’ parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘securityManager’ defined in class path resource [com/common/config/ShiroConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.shiro.mgt.SecurityManager]: Factory method ‘securityManager’ threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘shiroRealm’: Unsatisfied dependency expressed through field ‘sysRoleService’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘sysRoleServiceImpl’: Unsatisfied dependency expressed through field ‘baseMapper’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.core.dao.SysRoleDao’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
解决方法
原因1: 在ShiroConfiguration中要使用@Bean在ApplicationContext注入MyRealm,不能直接new对象。
道理和Controller中调用Service一样,都要是SpringBean,不能自己new。
错误演示:
@Bean(name = "securityManager")
public SecurityManager securityManager() {DefaultWebSecurityManager manager = new DefaultWebSecurityManager();MyRealm myRealm = new MyRealm();manager.setRealm(myRealm);return manager;}
正确操作:
@Bean(name = "myRealm")
public MyRealm myAuthRealm() {MyRealm myRealm = new MyRealm();return myRealm;}
@Bean(name = "securityManager")
public SecurityManager securityManager(@Qualifier("myRealm")MyRealm myRealm) {DefaultWebSecurityManager manager = new DefaultWebSecurityManager();manager.setRealm(myRealm);return manager;}
原因2:Application添加注释@MapperScan(“com.**.mapper”) //mybatis扫描Mapper所在包
这篇关于springboot shiro Realm 无法自动注入的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!