这是错误
Description:
Field userRepository in com.yaqari.service.UserService required a bean of type 'com.yaqari.repository.UserRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.yaqari.repository.UserRepository' in your configuration.
这是我的项目结构:
pom.xml
src
├───main
│ ├───java
│ │ └───com
│ │ └───yaqari
│ │ │ YaqariApplication.java
│ │ │ DataLoader.java
│ │ ├───controller
│ │ │ UserController.java
│ │ ├───model
│ │ │ User.java
│ │ ├───repository
│ │ │ UserRepository.java
│ │ └───service
│ │ UserService.java
│ └───resources
│
└───test
└───java
Controller是@RestController:
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping(value = "/users")
public List<User> list() {
return userService.findAll();
}
}
模型是@Entity:
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long user_id;
protected User() {
}
}
存储库是扩展CrudRepository的@Repository
@Repository
public interface UserRepository extends CrudRepository<User, Long> {
}
服务是@Service
@Service
public class UserService {
@Autowired // <- here seems to be the issue
private UserRepository userRepository;
public List<User> findAll() {
Iterable<User> users = userRepository.findAll();
ArrayList<User> userList = new ArrayList<>();
users.forEach(e -> userList.add(e));
return userList;
}
}
编辑: 我正在使用sringboot应用程序
@SpringBootApplication
public class YaqariApplication {
public static void main(String[] args) {
SpringApplication.run(YaqariApplication.class, args);
}
}
答案 0 :(得分:0)
您应该更改pom.xml 您需要
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>