org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'categoryController': Unsatisfied dependency expressed through field 'categoryService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'categoryServiceImpl': Unsatisfied dependency expressed through field 'categoryMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'categoryMapper' defined in file [D:\desktop\software\back\sky-take-out\sky-server\target\classes\com\sky\mapper\CategoryMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested e
时间: 2025-06-08 18:33:23 浏览: 15
<think>嗯,用户遇到了Spring Boot中的UnsatisfiedDependencyException问题,具体涉及到categoryController、categoryService、categoryMapper这几个组件的依赖注入失败。我需要帮他分析可能的原因,并提供解决方案。
首先,根据引用[2]和[4],这个异常通常是因为某个bean没有被正确创建或注入。可能的原因包括缺少注解、包扫描问题、依赖配置错误或MyBatis配置问题。
用户提到categoryController、Service和Mapper,所以首先应该检查这些类是否添加了正确的注解。Controller层应该有@RestController或@Controller,Service层用@Service,Mapper接口可能需要@Mapper或者在启动类上用@MapperScan。引用[1]中用户可能错误地排除了数据源自动配置,导致MyBatis无法初始化,这也是需要注意的地方。
接下来,包扫描的问题。如果这些组件不在启动类所在的包或其子包下,可能需要手动配置@ComponentScan或@MapperScan。例如,引用[1]中的用户使用了@ComponentScan("com.example.ecas.persistence"),这可能没有覆盖到Mapper接口所在的包,导致无法生成bean。
然后检查依赖冲突。引用[3]提到同时存在mybatis-plus和mybatis-spring-boot-starter可能会导致问题,需要确保只使用其中一个。用户应该检查pom.xml,排除多余的依赖。
另外,MyBatis的配置是否正确也很重要。比如,是否在application.properties中配置了数据源,以及是否正确指定了mapper.xml的位置。如果使用MyBatis-Plus,可能需要配置@EnableTransactionManagement和MapperScan。
还有可能是接口实现问题,比如Mapper接口是否被正确实现,或者是否有对应的XML文件。如果使用注解方式,确保SQL语句正确;如果使用XML,检查路径和配置。
最后,建议用户检查启动类是否排除了必要的自动配置,如引用[1]中的exclude可能导致数据源未加载,从而影响Mapper的创建。
总结下来,解决方案包括检查注解、包扫描、依赖配置、MyBatis设置,以及启动类配置。逐步排查这些方面应该能解决问题。</think>### 逐步解决方案:Spring Boot依赖注入失败问题分析
#### 1. **检查组件注解是否缺失**
- **Controller层**:确保`categoryController`类添加了`@RestController`或`@Controller`注解。
- **Service层**:`categoryService`类需要添加`@Service`注解。
- **Mapper层**:若使用MyBatis或MyBatis-Plus,`categoryMapper`接口需添加`@Mapper`注解,或在启动类添加`@MapperScan("com.example.mapper")`指定扫描路径[^3][^4]。
```java
// 示例代码
@RestController
public class CategoryController {
@Autowired
private CategoryService categoryService;
}
@Service
public class CategoryServiceImpl implements CategoryService {
@Autowired
private CategoryMapper categoryMapper;
}
@Mapper
public interface CategoryMapper {
// SQL方法
}
```
#### 2. **验证包扫描范围**
- **启动类路径**:确保`@SpringBootApplication`所在包包含所有组件(Controller/Service/Mapper)。例如,若启动类在`com.example`包下,其他组件应位于`com.example.controller`、`com.example.service`等子包中。
- **手动指定扫描路径**:若组件不在子包中,需在启动类添加`@ComponentScan`或`@MapperScan`:
```java
@SpringBootApplication
@MapperScan("com.example.mapper") // 指定Mapper接口路径
@ComponentScan({"com.example.controller", "com.example.service"})
public class Application { ... }
```
#### 3. **排除依赖冲突**
- **检查`pom.xml`依赖**:若同时引入`mybatis-plus-boot-starter`和`mybatis-spring-boot-starter`,需移除后者以避免冲突[^3]。
```xml
<!-- 仅保留其一 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
```
#### 4. **验证MyBatis配置**
- **数据源配置**:在`application.properties`中配置数据库连接:
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/dbname
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
```
- **Mapper XML路径**:若使用XML映射文件,需指定其位置:
```properties
mybatis.mapper-locations=classpath:mapper/*.xml
```
#### 5. **检查自动配置排除项**
- **避免错误排除**:若启动类中排除了`DataSourceAutoConfiguration`或`JpaRepositoriesAutoConfiguration`,可能导致数据源未初始化[^1]:
```java
// 错误示例:排除数据源配置
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
// 正确做法:移除不必要的exclude
@SpringBootApplication
```
#### 6. **验证SQL会话工厂**
- **`SqlSessionFactory`注入**:若手动配置`SqlSessionFactory`,需确保其Bean定义正确:
```java
@Configuration
public class MyBatisConfig {
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
return sessionFactory.getObject();
}
}
```
---
### 关键总结
| 问题类型 | 解决方法 |
|-------------------------|------------------------------------------|
| 注解缺失 | 添加`@Controller`/`@Service`/`@Mapper` |
| 包扫描路径错误 | 使用`@ComponentScan`或`@MapperScan` |
| 依赖冲突 | 移除冗余依赖(如`mybatis-spring-boot-starter`) |
| 数据源未配置 | 检查`application.properties`文件 |
| 自动配置错误排除 | 移除`@SpringBootApplication(exclude=...)`|
---
阅读全文
相关推荐







