1. 报错现象与背景分析Error creating bean with name xxxxxxxController: Injection of resource dependencies failed这个报错是Spring Boot项目中典型的依赖注入失败问题。当Spring容器尝试创建Controller bean时发现无法完成对其中某个资源的注入通常是Service或Repository注解的组件导致整个应用启动失败。这个错误的核心在于Spring的依赖注入机制DI遇到了障碍。我最近在一个电商后台系统中就遇到了完全相同的报错当时控制台打印的完整错误信息是org.springframework.beans.factory.BeanCreationException: Error creating bean with name productController: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type com.example.service.ProductService available从错误堆栈可以清晰看出Spring在创建productController时发现需要注入一个ProductService但在容器中找不到符合条件的bean。这种情况在整合Spring Boot和MyBatis的项目中尤为常见特别是在Controller-Service-Dao的分层架构中。2. 常见原因深度排查2.1 组件扫描范围问题Spring Boot默认只会扫描主类所在包及其子包下的组件。如果你的Controller和Service不在同一个包或其子包下Spring将无法自动发现并注册这些bean。典型场景示例com.example ├── Application.java // 主类 ├── controller │ └── ProductController.java └── service └── ProductService.java如果主类在com.example包而Service类被意外放在了com.otherpackage.service下就会导致扫描不到的情况。我建议使用以下两种方式之一解决确保所有组件都在主类所在包或其子包下显式添加ComponentScan注解指定扫描路径SpringBootApplication ComponentScan({com.example, com.otherpackage}) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }2.2 注解缺失或错误在SpringMyBatis整合项目中常见的注解问题包括Service层忘记添加Service注解Mapper接口忘记添加Mapper注解或未在启动类添加MapperScan指定扫描路径Controller层错误使用了Component而非Controller对于MyBatis特别需要注意的是如果你使用的是XML映射文件方式还需要确保XML文件放在了resources下正确的目录通常为mapper/application.properties中配置了mybatis.mapper-locations2.3 循环依赖问题当两个bean互相依赖时Spring可能无法完成依赖注入。例如Service public class ServiceA { Autowired private ServiceB serviceB; } Service public class ServiceB { Autowired private ServiceA serviceA; }这种情况Spring会抛出BeanCurrentlyInCreationException。解决方案包括重构代码消除循环依赖使用Lazy延迟加载其中一个bean改用setter注入而非字段注入2.4 多数据源配置问题在配置多数据源时如果没有正确指定每个Mapper接口对应的SqlSessionTemplate会导致注入失败。例如Mapper public interface UserMapper { //... }如果配置了多个数据源但未指定UserMapper使用哪个就会报错。解决方案是在配置类中明确指定Bean public SqlSessionTemplate sqlSessionTemplate1(Qualifier(dataSource1) DataSource dataSource) { //... } Bean public MapperScannerConfigurer mapperScannerConfigurer1() { MapperScannerConfigurer configurer new MapperScannerConfigurer(); configurer.setBasePackage(com.example.mapper1); configurer.setSqlSessionTemplateBeanName(sqlSessionTemplate1); return configurer; }3. 系统化解决方案3.1 检查Spring Boot启动日志启动时添加--debug参数可以查看更详细的bean注册信息java -jar your-application.jar --debug在日志中搜索以下关键信息Creating shared instance of singleton bean - 成功创建的beanSkipping bean creation - 被跳过的beanNo qualifying bean - 找不到bean的错误3.2 使用Bean验证工具在Application类中添加以下代码可以主动检查bean是否被正确注册public static void main(String[] args) { ConfigurableApplicationContext context SpringApplication.run(Application.class, args); // 检查特定bean是否存在 try { ProductService productService context.getBean(ProductService.class); System.out.println(ProductService bean exists!); } catch (NoSuchBeanDefinitionException e) { System.err.println(ProductService bean is missing!); } // 打印所有已注册的bean名称 System.out.println(All registered beans:); Arrays.stream(context.getBeanDefinitionNames()) .sorted() .forEach(System.out::println); }3.3 MyBatis特定检查清单对于Spring Boot MyBatis项目建议按以下顺序检查确认Mapper接口有Mapper注解或MapperScan扫描检查application.properties中的mybatis配置mybatis.mapper-locationsclasspath:mapper/*.xml mybatis.type-aliases-packagecom.example.model确认XML映射文件路径正确检查SQLSessionFactory配置如果是自定义配置3.4 依赖冲突排查有时不同版本的依赖会导致bean注册失败。使用以下命令查看依赖树mvn dependency:tree重点关注spring-boot-starter-webmybatis-spring-boot-starterspring-context确保它们的版本兼容。Spring Boot官方推荐的版本组合可以在start.spring.io上查询。4. 高级场景与解决方案4.1 条件化Bean注册问题当使用Conditional系列注解时可能因为条件不满足导致bean未被注册。例如Bean ConditionalOnProperty(name feature.enabled, havingValue true) public FeatureService featureService() { return new FeatureServiceImpl(); }如果application.properties中没有feature.enabledtrue这个bean就不会被注册。解决方案检查条件注解的配置使用ConditionalOnMissingBean等注解时要特别注意执行顺序4.2 动态代理问题当使用AOP或Transactional时Spring会创建代理对象。如果代理创建失败也会导致依赖注入失败。常见情况包括类被final修饰无法被CGLIB代理方法被private修饰无法被代理同一个类内部方法调用不会经过代理解决方案确保被代理的类和方法符合要求使用接口JDK动态代理方式通过ApplicationContext获取代理对象Service public class OrderService { Autowired private ApplicationContext context; public void process() { // 获取代理对象 OrderService proxy context.getBean(OrderService.class); proxy.internalMethod(); // 会经过代理 } Transactional public void internalMethod() { // ... } }4.3 多模块项目中的组件扫描在多模块项目中常见的扫描问题包括主类所在的模块没有依赖包含组件的模块组件所在的模块没有正确导出包ComponentScan注解路径配置错误解决方案示例Maven多模块项目parent ├── api (包含Controller) ├── service (包含Service) └── application (主模块)在application模块的pom.xml中dependencies dependency groupIdcom.example/groupId artifactIdapi/artifactId version${project.version}/version /dependency dependency groupIdcom.example/groupId artifactIdservice/artifactId version${project.version}/version /dependency /dependencies在主类上添加SpringBootApplication ComponentScan({com.example.api, com.example.service}) public class Application { // ... }5. 实战案例与经验分享5.1 案例一MyBatis Mapper未被扫描现象项目启动时报错找不到UserMapper的bean。排查过程检查UserMapper接口有Mapper注解检查启动类有MapperScan(com.example.mapper)检查application.properties配置了mybatis.mapper-locations最后发现是pom.xml中mybatis-spring-boot-starter版本与Spring Boot不兼容解决方案!-- 原配置 -- dependency groupIdorg.mybatis.spring.boot/groupId artifactIdmybatis-spring-boot-starter/artifactId version1.3.1/version /dependency !-- 修改为与Spring Boot 2.5.x兼容的版本 -- dependency groupIdorg.mybatis.spring.boot/groupId artifactIdmybatis-spring-boot-starter/artifactId version2.2.0/version /dependency5.2 案例二多数据源配置错误现象系统需要连接两个数据库配置后报错找不到bean。错误配置Configuration public class DataSourceConfig { Bean Primary public DataSource primaryDataSource() { // 配置第一个数据源 } Bean public DataSource secondaryDataSource() { // 配置第二个数据源 } // 缺少针对第二个数据源的SqlSessionFactory配置 }正确配置Configuration MapperScan(basePackages com.example.mapper.primary, sqlSessionFactoryRef primarySqlSessionFactory) public class PrimaryDataSourceConfig { // 主数据源配置 } Configuration MapperScan(basePackages com.example.mapper.secondary, sqlSessionFactoryRef secondarySqlSessionFactory) public class SecondaryDataSourceConfig { // 次数据源配置 }5.3 个人经验总结组件扫描黄金法则保持项目结构清晰所有组件放在主类所在包或其子包下。如果需要跨模块扫描显式配置ComponentScan。MyBatis集成检查清单确认Mapper接口有Mapper或MapperScan检查XML映射文件路径正确验证mybatis.mapper-locations配置确保依赖版本兼容排错技巧使用--debug参数查看详细启动日志在启动时打印所有注册的bean名称对复杂项目分模块逐步验证bean注册情况避免的坑不要在配置类中使用Autowired注入bean可能导致循环依赖谨慎使用PostConstruct其中的依赖可能还未完全初始化多数据源项目要为每个Mapper明确指定SqlSessionTemplate工具推荐Spring Boot Actuator的/beans端点可以查看所有注册的beanIDE的Diagrams功能可以可视化bean依赖关系mvn dependency:tree分析依赖冲突