通过字段 'employeeDao' 表示的不满足的依赖关系;

Posted

技术标签:

【中文标题】通过字段 \'employeeDao\' 表示的不满足的依赖关系;【英文标题】:Unsatisfied dependency expressed through field 'employeeDao';通过字段 'employeeDao' 表示的不满足的依赖关系; 【发布时间】:2019-11-09 09:52:18 【问题描述】:
I am fresh to spring boot and currently facing this error in STS

"Error creating bean with name 'employeeDao': Unsatisfied dependency expressed through field 'sessionfactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException"
Entity Class

@Entity
@Table(name = "studenttable")
public class Employee 



@Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    @Column(name = "sname")
    private String sname;
    @Column(name = "scourse")
    private String cname;
    @Column(name = "sfee")
    private Double fee;
Hibernate Utils Class
    @Configuration
    public class HibernateUtilsConfig 

        @Autowired
        private EntityManagerFactory entityManagerFactory;

        @Bean
        public SessionFactory getSessionFactoty() 
            if(entityManagerFactory.unwrap(SessionFactory.class)== null) 
                throw new NullPointerException("Factory Not Found");
            
            return entityManagerFactory.unwrap(SessionFactory.class);
        
DAO Class
@Repository
public class EmployeeDao 

    @Autowired
    private SessionFactory sessionfactory;

    public void createEmployee(Employee employee) 
        Session session = null;
        try 
            session = sessionfactory.openSession();
            session.beginTransaction();
        Integer id=(Integer)    session.save(employee);
        System.out.println("The record is add in the system" + id);
            session.getTransaction().commit();
        catch (Exception e) 
            // TODO: handle exception
            e.printStackTrace();
        
    
Main Class
@SpringBootApplication
public class SpringExampleApplication implements CommandLineRunner 

    @Autowired
    private EmployeeDao employeeDao;

    public static void main(String[] args) 
        SpringApplication.run(SpringExampleApplication.class, args);
    
    @Override
    public void run(String... args) throws Exception 
        Employee employee = getEmployee();
        employeeDao.createEmployee(employee);

    
    private Employee getEmployee() 
        Employee employee = new Employee();
        employee.setSname("Imran");
        employee.setCname("Java");
        employee.setFee(1000d);
        return employee;
    

**Error Log**

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为 springExampleApplication 的 bean 时出错:通过字段 'employeeDao' 表达的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException: Error` Creating bean with name 'employeeDao': Unsatisfied dependency expressed through field 'sessionfactory';嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“hibernateUtilsConfig”的bean时出错:通过字段“entityManagerFactory”表示的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名为“getSessionFactoty”的 bean 时出错:当前正在创建请求的 bean:是否存在无法解析的循环引用? 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] 在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE] 在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE] 在 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:311) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1213) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1202) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE] 在 com.imran.works.SpringExampleApplication.main(SpringExampleApplication.java:18) [classes/:na] 造成的: org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“employeeDao”的bean时出错:通过字段“sessionfactory”表示的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“hibernateUtilsConfig”的 bean 时出错:通过字段“entityManagerFactory”表示不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名为“getSessionFactoty”的 bean 时出错:当前正在创建请求的 bean:是否存在无法解析的循环引用? 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] 在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]

请帮帮我,

谢谢!

【问题讨论】:

你是怎么配置spring的? 我刚刚安装了 Spring tools Suite 和 Java 1.8 除非您在某处配置任何 bean,否则 spring 无法知道它应该实际自动装配哪些 bean 你能分享任何资源吗? tutorialspoint.com/spring/spring_java_based_configuration.htm 【参考方案1】:

您面临的问题是由于您提供的HibernateUtilsConfig.java 配置类。在您的EmployeeDao 类中,您正在自动装配sessionfactory bean。因此,当 springboot 尝试自动装配 bean 时,它会失败并出现以下错误:

Unsatisfied dependency expressed through field 'sessionfactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hibernateUtilsConfig': Unsatisfied dependency expressed through field 'entityManagerFactory'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'getSessionFactoty': Requested bean is currently in creation: Is there an unresolvable circular reference?

因为entityManagerFactory bean 不可用。

由于您使用的是 spring-boot ,因此您可能无法手动配置所有内容。您可以通过添加以下依赖项来使用 spring-boot 中的默认自动配置:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

然后,您可以在 application.properties 或 application.yml 中提供适当的键,spring-boot 将为您配置所有内容。

application.properties

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=mysqluser
spring.datasource.password=mysqlpass
spring.datasource.url=jdbc:mysql://localhost:3306myDb?createDatabaseIfNotExist=true

如果您仍想手动设置所有内容,请尝试创建实体管理器 bean,例如:

   @Bean
   public LocalContainerEntityManagerFactoryBean entityManagerFactory() 
      LocalContainerEntityManagerFactoryBean em 
        = new LocalContainerEntityManagerFactoryBean();
      em.setDataSource(dataSource());
      em.setPackagesToScan(new String[]  "com.example.persistence.model" );

      JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
      em.setJpaVendorAdapter(vendorAdapter);
      em.setJpaProperties(additionalProperties());

      return em;
   

参考documentation。

【讨论】:

以上是关于通过字段 'employeeDao' 表示的不满足的依赖关系;的主要内容,如果未能解决你的问题,请参考以下文章

创建名称为 authenticationTokenFilterBean 的 bean 时出错:通过字段 tokenUtils 表达的不满足的依赖关系

初始化 SolrRepository 时出错:通过字段表示的依赖关系不满足

UnsatisfiedDependencyException:创建名称为“studentConrtroller”的 bean 时出错:通过字段“studentserv”表示不满足的依赖关系;

创建名称为“flightController”的 bean 时出错:通过字段“flightrepos”表示的依赖关系不满足;

出现错误:创建名称为“*”的 bean 时出错:通过字段“repo”表示不满足的依赖关系; n

数据库中数据类型