Java 在springboot中使用@Autowired时出错

Java 在springboot中使用@Autowired时出错,java,spring,spring-boot,Java,Spring,Spring Boot,我正在创建一个Springboot项目,在该项目中,我将两个服务接口注入到我的控制器中 学生服务 public interface StudentService { void addStudent(Student student); //other functions } 教师服务 public interface TeacherService { void addStudent(Teacher teacher); //other functions }

我正在创建一个Springboot项目,在该项目中,我将两个服务接口注入到我的控制器中

学生服务

public interface StudentService {

    void addStudent(Student student);
    //other functions
}
教师服务

public interface TeacherService {

    void addStudent(Teacher teacher);
    //other functions
}
当我将@Autowired用于StudentService时,它工作正常,但当我在控制器中将@Autowired用于TeacherService时,我遇到了一个错误。我尝试了很多次,但没有找到错误的原因

我的控制器

@Controller
public class StudentController {

    @Autowired
    StudenService studenService;
    ....
    ....
}

@Autowired将用于在容器内的两个对象之间创建链接。 您似乎在StudentService实现中使用了@service或@component,而在TeacherService实现中却没有使用它

请确保在TeacherService实现中使用@component。这样,对象将在容器内创建

@Service or @component
public class TeacherServiceImp implements TeacherService {
//your code
}

@Autowired将用于在容器内的两个对象之间创建链接。 您似乎在StudentService实现中使用了@service或@component,而在TeacherService实现中却没有使用它

请确保在TeacherService实现中使用@component。这样,对象将在容器内创建

@Service or @component
public class TeacherServiceImp implements TeacherService {
//your code
}

用@service annotation注释服务类

@服务 公共接口教师服务{

void addStudent(Teacher teacher);
//other functions

}用@service annotation注释服务类

@服务 公共接口教师服务{

void addStudent(Teacher teacher);
//other functions

}

我们也可以在这里使用@Component为
TeacherServiceImpl
类创建bean。我认为两者都能起到同样的作用

@Service
public class TeacherServiceImp implements TeacherService {
//your codes 
}

我们还可以在这里使用@Component为
TeacherServiceImpl
类创建bean。我认为两者都能起到同样的作用

@Service
public class TeacherServiceImp implements TeacherService {
//your codes 
}

请尝试在ServiceImpl类上使用@Service,然后重试。我认为这应该可以解决您的问题。

尝试在ServiceImpl类上使用@Service,然后重试。我认为这应该可以解决您的问题。

这种类型的错误通常发生在您可能尚未在服务实现中使用@Service或者尚未实现服务接口的情况下

@Service
public class TeacherServiceImp implements TeacherService {
//your codes
}

这种类型的错误通常发生在您可能尚未在服务实现中使用@Service或者尚未实现服务接口的情况下

@Service
public class TeacherServiceImp implements TeacherService {
//your codes
}

你有两个接口的实现都被@Service惹恼了吗?你会遇到什么样的错误?StudentService和StudentService之间有打字错误你有两个接口的实现都被@Service惹恼了吗?你会遇到什么样的错误?StudentService和StudentService之间有打字错误