Java com.springboot.todoController.todoController中的字段todoService需要类型为';com.springboot.todo.TodoService';那是找不到的
我是Spring boot的新手…我在运行控制器时遇到了一个问题 说明: com.springboot.todoController.todoController中的字段todoService 需要类型为“com.springboot.todo.TodoService”的bean,该bean可以 找不到 行动: 考虑在中定义“com.springboot.todo.TodoService”类型的bean 您的配置 下面是我的代码 Todo.javaJava com.springboot.todoController.todoController中的字段todoService需要类型为';com.springboot.todo.TodoService';那是找不到的,java,spring,spring-boot,Java,Spring,Spring Boot,我是Spring boot的新手…我在运行控制器时遇到了一个问题 说明: com.springboot.todoController.todoController中的字段todoService 需要类型为“com.springboot.todo.TodoService”的bean,该bean可以 找不到 行动: 考虑在中定义“com.springboot.todo.TodoService”类型的bean 您的配置 下面是我的代码 Todo.java package com.springboot.
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;
}
}
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;
}
}
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.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;
}
}
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;
}
}
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);
}
}
包com.springboot.todo;
导入java.util.ArrayList;
导入java.util.Date;
导入java.util.List;
导入org.springframework.stereotype.Service;
导入com.springboot.todoBean.Todo;
@服务
公共类TodoService{
private static List todos=new ArrayList();
私有静态int todoCount=3;
静止的{
add(新Todo(1,“Jack”,“LearnSpringMVC”,newDate(),false));
add(新Todo(2,“Jack”,“learnstruts”,newdate(),false));
add(新Todo(3,“吉尔”,“学习休眠”,newdate(),false));
}
公共列表检索路径(字符串用户){
List filteredTodos=new ArrayList();
for(待办事项:待办事项){
if(todo.getUser().equals(user))
filteredTodos.add(todo);
}
返回filteredTodos;
}
公共Todo ADDDTODO(字符串名称、字符串描述、,
日期targetDate,布尔值isDone){
Todo Todo=新Todo(++todoCount、名称、描述、目标日期、isDone);
待办事项。添加(待办事项);
返回待办事项;
}
公共待办事项检索待办事项(int id){
for(待办事项:待办事项){
if(todo.getId()==id)
返回待办事项;
}
返回null;
}
}
TodoController.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;
}
}
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;
}
}
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);
}
}
包com.springboot.todoController;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.boot.SpringApplication;
导入org.springframework.boot.autoconfigure.EnableAutoConfiguration;
导入org.springframework.web.bind.annotation.GetMapping;
导入org.springframework.web.bind.annotation.PathVariable;
导入org.springframework.web.bind.annotation.RestController;
导入com.springboot.todo.TodoService;
导入com.springboot.todoBean.Todo;
@RestController
公共类TodoController{
@自动连线
私有TodoService TodoService;
@GetMapping(“/users/{name}/todos”)
公共列表检索路径(@PathVariable字符串名称){
返回到doService.retrieveTodos(名称);
}
公共静态void main(字符串[]args)引发异常{
run(TodoController.class,args);
}
}
我已经在TodoService中添加了注释@Service来告诉spring boot它是一个bean,但它仍然无法识别,有人能告诉我如何解决这个问题吗?谢谢由于应用程序未扫描到服务,因此生成了错误。 上述代码有几个问题:
三,。默认情况下,spring引导应用程序将扫描包中包含的bean,其中定义了来自2的类。您可以将来自2的类放入服务和控制器的公共包中 生成错误是因为应用程序未扫描到服务。 上述代码有几个问题:
三,。默认情况下,spring引导应用程序将扫描包中包含的bean,其中定义了来自2的类。您可以将来自2的类放入服务和控制器的公共包中 创建一个单独的类,如下所示,用于启动spring引导应用程序。另外,请注意,与其他控制器、服务等类相比,下面的类应该位于包层次结构的更高级别
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
创建一个单独的类,如下所示,用于启动spring引导应用程序。另外,请注意,与其他控制器、服务等类相比,下面的类应该位于包层次结构的更高级别
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
你的主要课程是什么?把它全部寄出去。在源代码布局中,它应该是“com.springboot”包。用@SpringBootApplication注释的类在哪里?它应该在com.springboot.todo的父包中。您的主类在哪个包中?把它全部寄出去。在源代码布局中,它应该是“com.springboot”包。用@SpringBootApplication注释的类在哪里?它应该在com.springboot.todo的父包中。我建议将“Why don't you write”更改为更中性的内容,以使其成为一个好答案。我建议将“Why don't you write”更改为更中性的内容,以使其成为一个好答案。