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所在包
该博客文章讨论了在Spring Boot应用中遇到的Shiro配置问题,具体表现为 UnsatisfiedDependencyException。错误源于在ShiroConfig类中直接new MyRealm对象而非通过@Bean注解注入。解决方案是确保所有Shiro相关组件都是Spring管理的Bean,并使用@Autowired和@Qualifier注解进行依赖注入。同时,文章提醒读者检查MyRealm类的依赖,如SysRoleService,确保它们的Mapper也被正确扫描和注入。
2083

被折叠的 条评论
为什么被折叠?



