java - com.springboot.todoController.TodoController 中的字段 todoService 需要类型为 'com.springboot.todo.TodoService' 的 bean,但无法找到

标签 java spring spring-boot

我是 Spring boot 的新手...我在运行 Controller 时遇到问题,

Description:

Field todoService in com.springboot.todoController.TodoController required a bean of type 'com.springboot.todo.TodoService' that could not be found.

Action:

Consider defining a bean of type 'com.springboot.todo.TodoService' in your configuration.

下面是我的代码

Todo.java

package com.springboot.todoBean;

import java.util.Date;

public class Todo {
    private int id;
    private String user;

    private String desc;

    private Date targetDate;
    private boolean isDone;

    public Todo() {}

    public Todo(int id, String user, String desc, Date targetDate, boolean isDone) {
        super();
        this.id = id;
        this.user = user;
        this.desc = desc;
        this.targetDate = targetDate;
        this.isDone = isDone;
    }


    public int getId() {
        return id;
    }


    public String getUser() {
        return user;
    }


    public String getDesc() {
        return desc;
    }


    public Date getTargetDate() {
        return targetDate;
    }


    public boolean isDone() {
        return isDone;
    }

}

TodoService.java

package com.springboot.todo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.springboot.todoBean.Todo;

@Service
public class TodoService {
    private static List<Todo> todos = new ArrayList<Todo>();
    private static int todoCount = 3;


    static {
        todos.add(new Todo(1, "Jack", "Learn Spring MVC", new Date(), false));
        todos.add(new Todo(2, "Jack", "Learn Struts", new Date(), false));
        todos.add(new Todo(3, "Jill", "Learn hibernate", new Date(), false));
    }

    public List<Todo> retrieveTodos(String user){
        List<Todo> filteredTodos = new ArrayList<Todo>();
        for (Todo todo : todos) {
            if(todo.getUser().equals(user))
                filteredTodos.add(todo);
        }
        return filteredTodos;
    }

    public Todo addTodo(String name, String desc,
            Date targetDate, boolean isDone) {
        Todo todo = new Todo(++todoCount, name, desc, targetDate, isDone);
        todos.add(todo);
        return todo;
    }

    public Todo retrievedTodo(int id) {
        for(Todo todo: todos) {
            if(todo.getId() == id)
                return todo;    
        }
        return null;
    }
}

TodoController.java

package com.springboot.todoController;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.springboot.todo.TodoService;
import com.springboot.todoBean.Todo;

@RestController
public class TodoController {

    @Autowired
    private TodoService todoService;

    @GetMapping("/users/{name}/todos")
    public List<Todo> retrieveTodo(@PathVariable String name){
        return todoService.retrieveTodos(name);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(TodoController.class, args);
    }
}

enter image description here 我已经在TodoService中添加了注解@Service来告诉spring boot它是一个bean,但它仍然无法识别,有人可以告诉我如何解决这个问题吗?谢谢

最佳答案

创建一个单独的类,如下所示,用于启动 Spring Boot 应用程序。另外,请注意,下面的类应放置在包层次结构中比其他 Controller 、服务等类更高的级别。

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

关于java - com.springboot.todoController.TodoController 中的字段 todoService 需要类型为 'com.springboot.todo.TodoService' 的 bean,但无法找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52700496/

相关文章:

java - Spring Boot Amazon AWS S3 存储桶文件下载 - 访问被拒绝

spring-boot - 带有企业代理的 Spring Cloud Feign/Ribbon

java.util.concurrent.CyclicBarrier 类的方法 getNumberWaiting() 返回随机输出

java - 每 2 小时执行一次 Java 定时器

java - 如何覆盖 quartz 的属性值

Spring @Transactional 值参数与 SpEL(Spring 表达式语言)

java - Web 应用程序中 Hibernate 对象的典型生命周期 -?

java - 扩展线程以测试连接是否正确终止?

java - Spring依赖注入(inject)不起作用

java - 在java中使用SevenZip.openArchive方法后无法删除文件