我是 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);
}
}
我已经在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/