Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在springboot中自动连接时找不到特定类型的bean_Java_Spring_Spring Boot_Autowired - Fatal编程技术网

Java 在springboot中自动连接时找不到特定类型的bean

Java 在springboot中自动连接时找不到特定类型的bean,java,spring,spring-boot,autowired,Java,Spring,Spring Boot,Autowired,在开始之前,请假设pom.xml是无错误的 话虽如此,让我们继续 我得到的错误如下: 应用程序无法启动 ***************************说明: com.sagarp.employee.EmployeeService中的字段empDao需要一个bean 键入找不到的“com.sagarp.employee.EmployeeDao” 现在,spring boot应用程序的类如下所示: package com.sagarp; import org.springframework

在开始之前,请假设
pom.xml
是无错误的

话虽如此,让我们继续

我得到的错误如下:

应用程序无法启动 ***************************说明:

com.sagarp.employee.EmployeeService中的字段empDao需要一个bean 键入找不到的“com.sagarp.employee.EmployeeDao”

现在,spring boot应用程序的
类如下所示:

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);
    }
}
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
}
public interface EmployeeDao {
    //Oh! so many methods to I have provided
}
EmployeeService
类别如下:

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);
    }
}
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
}
public interface EmployeeDao {
    //Oh! so many methods to I have provided
}
请注意,
EmployeeDao
是一个接口

public class EmployeeDaoImpl implements EmployeeDao {

    @Autowired
    private SessionFactory sessionFactory;

    //Oh!So many methods I had to implement
}
EmployeeDao
界面如下:

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);
    }
}
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
}
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
}
我猜是由于
员工服务
被标注为
@Service
的原因,它是自动连接的。 我在
components
中添加了所有包,这样它将扫描并实例化我可能拥有的所有依赖项

但事实并非如此,因此出现了问题

有人能帮我解决上面的错误吗。
如果需要更多详细信息,请告诉我。

组件扫描搜索用Spring原型注释注释的类。为了使类符合自动连接的条件,它必须具有以下注释之一


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

EmployeeDaoImpl
未注册为bean。有两种方法:XML或注释。由于您已经使用了注释,现在可以执行以下操作:

@Repository
public class EmployeeDaoImpl implements EmployeeDao {

    @Autowired
    private SessionFactory sessionFactory;

    //...
}
注意,您已经将
EmployeeService
注册为
@Service
的bean。在此之后,应该在容器中识别bean并正确注入

为什么要使用DAO的
@Repository
,而不是
@Service
?如何决定?更多信息,请阅读本文

  • @Component
    是任何Spring托管组件的通用原型
  • @Service
    在服务层注释类
  • @Repository
    注释持久层上的类,持久层将充当数据库存储库

还应注释EmployeeDaoImpl

@Repository
public class EmployeeDaoImpl implements EmployeeDao {

   @Autowired
   private SessionFactory sessionFactory;

   //Oh!So many methods I had to implement
}

这应该可以解决问题。

尝试用
@Repository
注释
EmployeeDaoImpl
类。感谢您的建议,它现在可以工作了,但这是sessionFactory的衍生问题。
@sagarpatro将一个答案标记为正确,并用其他人在评论中要求的内容提出另一个问题。
sessionFactory的问题。请随意创建一个新问题。感谢您的建议,它现在可以工作了,但这是
sessionFactory
的垃圾邮件问题。这是另一个与存储库问题无关的问题。为此,您必须提供EmployeeDaoImpl.java类的内容。感谢您的建议,它现在可以工作了,但它会引发
sessionFactory的问题。