我尝试了一个简单的 Spring-boot 项目来学习结构。当我添加一个简单的方法来测试邮递员时,但出现此错误。 (com.ex.bul.controller.UserController 中构造函数的参数 0 需要一个类型为“com.ex.bul.service.UserService”的 bean,但无法找到)。
用户控制器类
@RestController
@RequestMapping("/user")
public class UserController {
private final UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@RequestMapping(value = "/list", method = RequestMethod.GET)
public ResponseEntity<List<User>> getUsers(){
List<User> users = userService.getUsers();
return ResponseEntity.ok(users);// ResponseEntity.ok() HTTP 200e denk geliyor
}
用户服务
public interface UserService {
User createUser(User users);
List<User> getUsers();
}
UserServiceImpl
@Service
public class UserServiceImpl implements UserService {
private final UserRepository userRepository;
@Autowired
public UserServiceImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public List<User> getUsers() {
return userRepository.findAll();
}
用户存储库
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
应用程序.class
@SpringBootApplication
@ComponentScan(basePackages={"com.ex.bul.controller"})
public class BulApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(BulApplication.class, args);
}
错误:
*****************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.ex.bul.controller.UserController required a bean of type 'com.ex.bul.service.UserService' that could not be found.
Action:
Consider defining a bean of type 'com.ex.bul.service.UserService' in your configuration.
**
我可以做什么来修复?
这是 pom.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ex.bul</groupId>
<artifactId>bul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>bul</name>
<description>bul</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>3.2.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
您的代码至少存在两个问题。
目前您仅扫描
com.ex.bul.controller
包裹。但其他包中也有 Spring 组件。最好的方法(Spring Boot 文档推荐)是从主应用程序类所在的包开始扫描所有包。
解决方案
删除注释
@ComponentScan(basePackages={"com.ex.bul.controller"})
pom.xml
目前您正在将新版本的 JPA 与 Spring Boot 2 中的旧版本混合。
解决方案
删除这部分:
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>3.2.3</version>
</dependency>
并且只使用这个:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
如果由于缺少包
javax.persistence
而出现编译错误,只需将其替换为jakarta.persistence
。