Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的服务中的字段存储库需要一个找不到的bean_Java_Spring_Spring Boot - Fatal编程技术网

Java 我的服务中的字段存储库需要一个找不到的bean

Java 我的服务中的字段存储库需要一个找不到的bean,java,spring,spring-boot,Java,Spring,Spring Boot,这里是错误 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.f

这里是错误

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
控制器是@RestController:

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping(value = "/users")
    public List<User> list() {
        return userService.findAll();
    }
}
Repository是扩展Crudepository的@Repository

@Repository
public interface UserRepository extends CrudRepository<User, Long> {
}
您应该更改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>

YaqariaApplication上的注释是什么?我的第一个问题是您是否使用Spring boot?如果您没有使用Spring boot,您需要@EnableJpaRepositories注释,并可以选择提供要启用这些存储库的包。您能显示您的YaqariaApplication.java类代码吗?您是否将annotation@EnableJpaRepositories设置为此类?您还可以从UserRepository中删除@Repository注释我添加了我的YaqariApplication
@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;
    }
}
@SpringBootApplication
public class YaqariApplication {

    public static void main(String[] args) {
        SpringApplication.run(YaqariApplication.class, args);
    }
}
        <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>