为了账号安全,请及时绑定邮箱和手机立即绑定

在springboot中自动装配时无法找到某种类型的bean

在springboot中自动装配时无法找到某种类型的bean

三国纷争 2021-05-31 22:30:02
在我开始之前,请假设它pom.xml是完美无缺的。话虽如此,让我们继续,我得到的错误如下:应用程序无法启动 *************************** 说明:com.sagarp.employee.EmployeeService 中的字段 empDao需要一个无法找到的类型为“com.sagarp.employee.EmployeeDao”的 bean。现在spring boot application类如下:package com.sagarp;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.context.annotation.ComponentScan;@SpringBootApplication@EnableEurekaClient //this is for eureka which not our concern right now.@ComponentScan(basePackages = "com.sagarp.*") //Included all packagespublic class EmployeeHibernateApplication {    public static void main(String[] args) {        SpringApplication.run(EmployeeHibernateApplication.class, args);    }}该EmployeeService班是如下:package com.sagarp.employee;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class EmployeeService {    @Autowired    private EmployeeDao empDao; // interface    public EmployeeDao getEmpDao() {        return empDao;    }    public void setEmpDao(EmployeeDao empDao) {        this.empDao = empDao;    }    //some methods}请注意,这EmployeeDao是一个接口。EmployeeDao 界面如下:public interface EmployeeDao {    //Oh! so many methods to I have provided}EmployeeDaoImpl实现EmployeeDao接口的类。public class EmployeeDaoImpl implements EmployeeDao {    @Autowired    private SessionFactory sessionFactory;    //Oh!So many methods I had to implement}我猜EmployeeService是因为@Service它是自动装配的。我添加了所有包,components以便它扫描并实例化我可能拥有的所有依赖项。但它没有,因此问题。任何人都可以帮助我解决上述详细信息的错误。如果需要更多详细信息,请告诉我。
查看完整描述

3 回答

?
MMTTMM

TA贡献1869条经验 获得超4个赞

组件扫描搜索使用 Spring Stereotype 注释注释的类。为了使类有资格进行自动装配,它必须具有这些注释之一。

解决方案是使用@Component、@Service 或@Repository 注释EmployeeDaoImpl。


查看完整回答
反对 回复 2021-06-02
  • 3 回答
  • 0 关注
  • 4708 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信