UnsatisfiedDependencyException NoSuchBeanDefinitionException

本文分析了在Spring MVC框架中出现的依赖注入错误,详细解释了UnsatisfiedDependencyException和NoSuchBeanDefinitionException异常的原因,并提供了排查问题的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址:点击打开链接

[org.springframework.web.context.support.XmlWebApplicationContext] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'blogController': Unsatisfied dependency expressed through field 'categoryService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.ssm.blog.service.CategoryService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.ssm.blog.service.CategoryService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

上面这段异常说创建blogController的时候失败了,UnsatisfiedDependencyException是依赖不满足抛出的异常,因为我在Controller中注入了categoryService。接着NoSuchBeanDefinitionException表示没有这样一个bean,也就是说我的categoryService没有生成bean。

遇到这种情况,要检查一下以下配置:

1) service接口实现类上有没有加@Service注解,注解是不是引用的spring的类?不要导错包

2) 接口有没有写实现类,实现类是实现的对应接口么?比如CategoryServiceImpl implementsCategoryDAO 一不小心根据自动提示,本来应该实现CategoryService,结果实现了CategoryDAO

3) 有没有扫描Service所在的包

    <!-- 启动自动扫描 -->  
        <context:component-scan base-package="com.ssm.blog.*">  
            <!-- 制定扫包规则 ,不扫描@Controller注解的JAVA类 -->  
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
        </context:component-scan>  

注意这段配置要加在spring父容器中而不是springMVC子容器。因为父容器无法访问自容器的对象。
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ODataConnectorImpl': Unsatisfied dependency expressed through field 'messages'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'weibaobiaoMessages': Unsatisfied dependency expressed through field 'securityService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'amsSecurityService': Unsatisfied dependency expressed through field 'securityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityManager' defined in class path resource [security-service.xml]: Cannot resolve reference to bean 'amsAuthorizationRealm' while setting bean property 'realm'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'amsAuthorizationRealm': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'customerService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customerServiceImpl': Unsatisfied dependency expressed through field 'metadataService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'defaultMetadataServiceImpl': Unsatisfied dependency expressed through field 'zxtbTask'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'zxtbTask': Unsatisfied dependency expressed through field 'redisDistributeLock'; nested exception is org.springframework.beans.f
最新发布
07-09
2025-03-27 15:47:51.709 ERROR 24032 - [restartedMain] [o.s.b.web.embedded.tomcat.TomcatStarter,onStartup,61] : Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'registration' defined in class path resource [com/cloudweb/oa/security/BeanConfig.class]: Unsatisfied dependency expressed through method 'registration' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jwtFilter': Unsatisfied dependency expressed through field 'jwtUtil'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jwtUtil': Unsatisfied dependency expressed through field 'userCache'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userCache': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'userGroupService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'groupServiceImpl': Unsatisfied dependency expressed through field 'departmentService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'departmentServiceImpl': Unsatisfied dependency expressed through field 'deptUserService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deptUserServiceImpl': Unsatisfied dependency expressed through field 'userSetupService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userSetupServiceImpl': Unsatisfied dependency expressed through field 'roleService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'roleServiceImpl': Unsatisfied dependency expressed through field 'rolePrivService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'rolePrivServiceImpl': Unsatisfied dependency expressed through field 'userOfRoleService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userOfRoleServiceImpl': Unsatisfied dependency expressed through field 'syncUtil'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.cloudweb.oa.api.ISyncUtil' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
03-28
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'coreAccountRangeSearchAdvisor': Unsatisfied dependency expressed through method 'autowired' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'initConfigDao': Unsatisfied dependency expressed through field 'myBatisDaoManagerFactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myBatisDaoManagerFactory' defined in class path resource [com/risen/base/frame/boot/config/dao/MybatisDaoManagerAutoConfiguration.class]: Unsatisfied dependency expressed through method 'myBatisDaoManagerFactory' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myBatisDaoManager' defined in class path resource [com/risen/base/frame/boot/config/dao/MybatisDaoManagerAutoConfiguration.class]: Unsatisfied dependency expressed through method 'myBatisDaoManager' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$d21507]: Constructor threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cacheSqlSessionFactoryBeanCustomizer' defined in class path resource [com/risen/base/frame/boot/config/dao/MybatisDaoManagerAutoConfiguration.class]: Unsatisfied dependency expressed through method 'cacheSqlSessionFactoryBeanCustomizer' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating b
03-19
<think>嗯,用户遇到了Spring Boot项目中与MyBatis相关的依赖注入异常,具体是UnsatisfiedDependencyException,错误信息是创建名为'coreAccountRangeSearchAdvisor'的bean时失败,涉及MyBatis的依赖注入问题。我需要根据提供的引用内容来找出可能的原因和解决方案。 首先,根据引用[1]和[2],出现这个异常通常是因为Spring容器找不到需要的bean,比如UserMapper或Service实现类。可能的原因包括Mapper接口没有被正确扫描到,或者没有使用合适的注解进行注册。 接下来,引用[3]和[4]提到了一些解决方案,比如确保Mapper接口有@Mapper注解或者在启动类上添加@MapperScan来指定扫描路径。另外,手动创建Mapper实例或者检查组件扫描的包是否正确也是可行的方法。 用户的问题涉及到MyBatis的依赖注入,特别是与MyBatis的工厂类有关。可能的错误点在于MyBatis的配置是否正确,比如是否缺少必要的配置类或依赖。另外,检查MyBatis的SqlSessionFactoryBean是否配置正确,确保数据源和mapper locations正确设置。 可能需要检查以下几个方面: 1. Mapper接口是否正确扫描,是否有@Mapper注解或配置了@MapperScan。 2. 是否缺少MyBatis的依赖,比如mybatis-spring-boot-starter。 3. 配置文件中是否正确配置了mapper.xml的位置和数据库连接信息。 4. 检查是否有多个数据源配置导致冲突,或者事务管理器配置不正确。 5. 使用构造器注入时,是否在Controller或Service中正确使用了@Autowired或@RequiredArgsConstructor。 另外,用户提到的coreAccountRangeSearchAdvisor可能与AOP或自定义的Advisor有关,需要确认相关类是否正确配置为Spring管理的bean,是否有依赖的bean未被正确注入。 可能的解决方案步骤包括: - 检查Mapper接口的注解和扫描配置。 - 确认依赖是否正确引入。 - 检查配置文件的正确性。 - 确保所有依赖的bean都被正确注册。 - 使用构造器注入代替字段注入,确保依赖关系明确。 需要综合考虑这些因素,逐步排查问题所在,并根据引用中的建议进行相应的调整。</think>针对Spring Boot项目中出现的$UnsatisfiedDependencyException$异常,特别是涉及MyBatis依赖注入的问题,以下是分步解决方案: ### 1. 确认Mapper接口扫描 确保Mapper接口添加了$@Mapper$注解或在启动类配置全局扫描: ```java @SpringBootApplication @MapperScan("com.example.mapper") // 替换为实际包路径 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 若未配置扫描路径,会导致$NoSuchBeanDefinitionException$[^1][^4] ### 2. 检查依赖注入方式 推荐使用构造器注入代替字段注入: ```java @Service @RequiredArgsConstructor // Lombok自动生成构造器 public class UserServiceImpl { private final UserMapper userMapper; // final字段确保依赖不可变 } ``` 强制依赖通过构造器注入能更早发现配置错误 ### 3. 验证MyBatis配置 在$application.yml$中确认数据源和XML映射路径: ```yaml mybatis: mapper-locations: classpath*:mapper/**/*.xml configuration: map-underscore-to-camel-case: true spring: datasource: url: jdbc:mysql://localhost:3306/dbname username: root password: 123456 ``` 错误的XML路径会导致Mapper方法无法绑定[^1] ### 4. 检查依赖冲突 在$pom.xml$中确认包含必要依赖: ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> ``` 版本冲突会导致$MyBatisDaoManagerFactory$初始化失败 ### 5. 排查事务管理器配置 在配置类中显式声明事务管理器: ```java @Configuration public class MyBatisConfig { @Bean public PlatformTransactionManager transactionManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } } ``` 缺少事务管理器会导致$@Transactional$注解失效 ### 6. 验证Bean生命周期 通过启动日志检查Bean加载顺序: ``` DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'userMapper' ``` 若Mapper接口未被实例化,需检查是否被AOP代理拦截导致创建失败[^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值