我是Spring Boot的新手...遇到问题时,运行控制器时,


  描述:
  
  com.springboot.todoController.TodoController中的字段todoService
  需要类型为“ com.springboot.todo.TodoService”的bean,
  找不到。
  
  行动:
  
  考虑在其中定义类型为“ com.springboot.todo.TodoService”的bean
  您的配置。


下面是我的代码

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);
    }
}


java - com.springboot.todoController.TodoController中的字段todoService需要找不到“com.springboot.todo.TodoService”类型的Bean-LMLPHP
我已经在TodoService中添加了@Service注释,以告诉它是一个bean,但是它仍然无法识别,有人可以告诉我如何解决这个问题吗?谢谢

最佳答案

由于您的应用程序未在扫描TodoService,因此生成了错误。
提到的代码有几个问题:


请使所有软件包都小写-Java约定
请把主体移到另一个类中,并用@SpringBootApplication对其进行注释


例如

@SpringBootApplication
public class Application{

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


3.默认情况下,spring boot应用程序将扫描定义了2类的软件包中包含的bean。您可以将2中的类放入服务和控制器的通用包中。

09-25 19:29