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

在我开始之前,请假设它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 packages

public 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;


@Service

public 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以便它扫描并实例化我可能拥有的所有依赖项。


但它没有,因此问题。


任何人都可以帮助我解决上述详细信息的错误。如果需要更多详细信息,请告诉我。


三国纷争
浏览 4709回答 3
3回答

MMTTMM

组件扫描搜索使用 Spring Stereotype 注释注释的类。为了使类有资格进行自动装配,它必须具有这些注释之一。解决方案是使用@Component、@Service 或@Repository 注释EmployeeDaoImpl。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java