问题
springboot项目使用了shiro框架,后项目需要连接两个数据库,配置多数据库连接后shiro报错如下:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-10-05 21:44:06.306 INFO 34548 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'shiroConfig' of type [com.**.common.shiro.ShiroConfig$$EnhancerBySpringCGLIB$$a50d5c2b] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-10-05 21:44:06.316 ERROR 34548 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'getShiroFilterFactoryBean' defined in class path resource [com/**/common/shiro/ShiroConfig.class]: Unsatisfied dependency expressed through method 'getShiroFilterFactoryBean' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityManager' defined in class path resource [com/**/common/shiro/ShiroConfig.class]: Unsatisfied dependency expressed through method 'getDefaultWebSecurityManager' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shiroRealm': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.**.user.mapper.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:769) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:509) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
...
其中多数据连接方式在其他项目实践中没有问题,之前shiro在此项目中也可以正常运行
解决
参考了大佬的文章 https://knife.blog.csdn.net/article/details/119704109
原项目在自定义realm中使用了UserMapper,给UserMapper加上@Lazy注解,不再报上面的错误
原:
public class ShiroRealm extends AuthorizingRealm {
@Resource
private UserMapper userMapper;
...
}
后:
public class ShiroRealm extends AuthorizingRealm {
@Lazy
@Resource
private UserMapper userMapper;
...
}