org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘XXX
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘leaseAgreementController’: Unsatisfied dependency expressed through field ‘service’: Error creating bean with name ‘leaseAgreementServiceImpl’: Unsatisfied dependency expressed through field ‘baseMapper’: No qualifying bean of type ‘com.miaow.lease.web.app.mapper.LeaseAgreementMapper’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessorAutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:713) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessorAutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:713) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessorAutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:713) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessorAutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:693) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:133) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:482) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1416) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:597) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean0(AbstractBeanFactory.java:326) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:917) [spring−context−6.0.7.jar:6.0.7]atorg.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584) [spring−context−6.0.7.jar:6.0.7]atorg.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) [spring−boot−3.0.5.jar:3.0.5]atorg.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) [spring−boot−3.0.5.jar:3.0.5]atorg.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) [spring−boot−3.0.5.jar:3.0.5]atorg.springframework.boot.SpringApplication.run(SpringApplication.java:310) [spring−boot−3.0.5.jar:3.0.5]atorg.springframework.boot.SpringApplication.run(SpringApplication.java:1304) [spring−boot−3.0.5.jar:3.0.5]atorg.springframework.boot.SpringApplication.run(SpringApplication.java:1293) [spring−boot−3.0.5.jar:3.0.5]atcom.miaow.lease.web.app.AppWebApplication.main(AppWebApplication.java:17) [classes/:na]Causedby:org.springframework.beans.factory.UnsatisfiedDependencyException:Errorcreatingbeanwithname′leaseAgreementServiceImpl′:Unsatisfieddependencyexpressedthroughfield′baseMapper′:Noqualifyingbeanoftype′com.miaow.lease.web.app.mapper.LeaseAgreementMapper′available:expectedatleast1beanwhichqualifiesasautowirecandidate.Dependencyannotations:@org.springframework.beans.factory.annotation.Autowired(required=true)atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:917) ~[spring-context-6.0.7.jar:6.0.7] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584) ~[spring-context-6.0.7.jar:6.0.7] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.0.5.jar:3.0.5] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) ~[spring-boot-3.0.5.jar:3.0.5] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-3.0.5.jar:3.0.5] at org.springframework.boot.SpringApplication.run(SpringApplication.java:310) ~[spring-boot-3.0.5.jar:3.0.5] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1304) ~[spring-boot-3.0.5.jar:3.0.5] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1293) ~[spring-boot-3.0.5.jar:3.0.5] at com.miaow.lease.web.app.AppWebApplication.main(AppWebApplication.java:17) ~[classes/:na] Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'leaseAgreementServiceImpl': Unsatisfied dependency expressed through field 'baseMapper': No qualifying bean of type 'com.miaow.lease.web.app.mapper.LeaseAgreementMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor0(AbstractBeanFactory.java:326) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:917) [spring−context−6.0.7.jar:6.0.7]atorg.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584) [spring−context−6.0.7.jar:6.0.7]atorg.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) [spring−boot−3.0.5.jar:3.0.5]atorg.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) [spring−boot−3.0.5.jar:3.0.5]atorg.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) [spring−boot−3.0.5.jar:3.0.5]atorg.springframework.boot.SpringApplication.run(SpringApplication.java:310) [spring−boot−3.0.5.jar:3.0.5]atorg.springframework.boot.SpringApplication.run(SpringApplication.java:1304) [spring−boot−3.0.5.jar:3.0.5]atorg.springframework.boot.SpringApplication.run(SpringApplication.java:1293) [spring−boot−3.0.5.jar:3.0.5]atcom.miaow.lease.web.app.AppWebApplication.main(AppWebApplication.java:17) [classes/:na]Causedby:org.springframework.beans.factory.UnsatisfiedDependencyException:Errorcreatingbeanwithname′leaseAgreementServiceImpl′:Unsatisfieddependencyexpressedthroughfield′baseMapper′:Noqualifyingbeanoftype′com.miaow.lease.web.app.mapper.LeaseAgreementMapper′available:expectedatleast1beanwhichqualifiesasautowirecandidate.Dependencyannotations:@org.springframework.beans.factory.annotation.Autowired(required=true)atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessorAutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:713) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessorAutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:693) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:133) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:482) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1416) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:597) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.AbstractBeanFactory.lambdaAutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:693) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:133) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:482) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1416) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:597) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.support.AbstractBeanFactory.lambdaAutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:693) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:133) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:482) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1416) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:597) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.AbstractBeanFactory.lambdadoGetBean0(AbstractBeanFactory.java:326) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1417) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1337) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1417) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1337) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor0(AbstractBeanFactory.java:326) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1417) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1337) [spring−beans−6.0.7.jar:6.0.7]atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessorAutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:710) ~[spring-beans-6.0.7.jar:6.0.7] … 20 common frames omitted Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.miaow.lease.web.app.mapper.LeaseAgreementMapper’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1824) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1383) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1337) ~[spring-beans-6.0.7.jar:6.0.7] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:710) ~[spring-beans-6.0.7.jar:6.0.7] … 34 common frames omitted
我的问题
@SpringBootApplication
@EnableAsync
public class AppWebApplication {
public static void main(String[] args) {
SpringApplication.run(AppWebApplication.class,args);
}
}
@Autowired
private LeaseAgreementService service;
@Operation(summary = "获取个人租约基本信息列表")
@GetMapping("listItem")
public Result<List<AgreementItemVo>> listItem() {
List<AgreementItemVo> result = service.listItemByPhone(LoginUserHolder.getLoginUser().getUsername());
return Result.ok(result);
}
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
implements LeaseAgreementService {
@Autowired
private LeaseAgreementMapper leaseAgreementMapper;
@Override
public List<AgreementItemVo> listItemByPhone(String username) {
return leaseAgreementMapper.listItemByPhone(username);
}
.....
}
public interface LeaseAgreementMapper extends BaseMapper<LeaseAgreement> {
List<AgreementItemVo> listItemByPhone(String username);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.miaow.lease.web.app.mapper.LeaseAgreementMapper">
<resultMap id="AgreementItemVoMap" type="com.miaow.lease.web.app.vo.agreement.AgreementItemVo" autoMapping="true">
<id property="id" column="id"/>
<collection property="roomGraphVoList" ofType="com.miaow.lease.web.app.vo.graph.GraphVo" autoMapping="true"/>
</resultMap>
<select id="listItemByPhone" resultMap="AgreementItemVoMap">
.....
</select>
</mapper>
就如上面的,能配置的地方我都配置了,最后的我采取的解决办法是下面的
@MapperScan("com.miaow.lease.web.app.mapper") // 扫描mapper接口的包路径
错误描述和解决办法
这个错误表明Spring无法找到一个符合条件的LeaseAgreementMapper类型的bean。这通常是因为LeaseAgreementMapper没有被正确地注册到Spring的bean工厂中。你可以尝试以下步骤来解决这个问题:
- 确保
LeaseAgreementMapper接口继承了Mapper接口(如果使用MyBatis),并且你的项目中包含了MyBatis的自动配置。 - 确保你的Mapper接口放置在正确的包下,Spring会自动扫描指定包及其子包下的bean。如果
LeaseAgreementMapper接口不在被扫描的包下,你需要在@SpringBootApplication注解上添加@ComponentScan注解,并指定包含LeaseAgreementMapper接口的包路径。 - 如果你使用的是
MyBatis-Spring-Boot-Starter,确保在配置文件中正确配置了数据库连接信息和MyBatis的相关配置。 - 确保你的
LeaseAgreementMapper接口已经被正确地实现了,并且这个实现类被注册为一个Spring bean。你可以使用@MapperScan注解来扫描所有的Mapper接口,并自动创建它们的实现类作为bean。例如:
@SpringBootApplication
@MapperScan("com.miaow.lease.web.app.mapper") // 扫描mapper接口的包路径
public class AppWebApplication {
public static void main(String[] args) {
SpringApplication.run(AppWebApplication.class,args);
}
}
或者,你也可以为每个Mapper接口创建一个实现类,并使用@Component或@Service注解将其实现类注册为一个Spring bean。
- 如果以上步骤都没有解决问题,你可以尝试在
LeaseAgreementServiceImpl中手动创建LeaseAgreementMapper的实例,而不是通过@Autowired自动注入。例如:
@Service
public class LeaseAgreementServiceImpl implements LeaseAgreementService {
private final LeaseAgreementMapper leaseAgreementMapper;
public LeaseAgreementServiceImpl(LeaseAgreementMapper leaseAgreementMapper) {
this.leaseAgreementMapper = leaseAgreementMapper;
}
// ...
}
然后在你的配置类中使用@Bean注解来创建LeaseAgreementMapper的实例:
@Configuration
public class AppConfig {
@Bean
public LeaseAgreementMapper leaseAgreementMapper() {
// 创建LeaseAgreementMapper的实例并返回
// 例如使用MyBatis的SqlSessionFactory来创建Mapper实例
return new LeaseAgreementMapper();
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)