简体   繁体   English

通过“ employeeDao”字段表示不满意的依赖关系;

[英]Unsatisfied dependency expressed through field 'employeeDao';

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: Error creating bean with name springExampleApplication: Unsatisfied dependency expressed through field 'employeeDao'; org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为springExampleBean的bean时出错:通过字段'employeeDao'表示的不满足的依赖关系; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error` creating bean with name 'employeeDao': Unsatisfied dependency expressed through field 'sessionfactory'; 嵌套的异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“ employeeDao”的bean时出错:通过字段“ sessionfactory”表示的不满足的依赖关系; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hibernateUtilsConfig': Unsatisfied dependency expressed through field 'entityManagerFactory'; 嵌套的异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ hibernateUtilsConfig”的bean时出错:通过字段“ 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? 嵌套的异常是org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名称为'getSessionFactoty'的bean时出错:当前正在创建请求的bean:是否存在不可解析的循环引用? at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapable 在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] at 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.AbstractAutowireCapable BeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.context.support.AbstractApplicationCont BeanFactory.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:在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)〜[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]中找到5.1.8.RELEASE]。 springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845)〜[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]在org.springframework.context.support.AbstractApplicationCont ext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE] at com.imran.works.SpringExampleApplication.main(SpringExampleApplication.java: ext.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 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389)的[.6.RELEASE] [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE],位于org.springframework.boot.SpringApplication。在org.springframework.boot.SpringApplication.run(SpringApplication.java:1213)上运行(SpringApplication.java:311)[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE] [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] Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeDao': Unsatisfied dependency expressed through field 'sessionfactory'; 18)[classes /:na]原因:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为'employeeDao'的bean时出错:通过字段'sessionfactory'表示的不满足的依赖关系; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hibernateUtilsConfig': Unsatisfied dependency expressed through field 'entityManagerFactory'; 嵌套的异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ hibernateUtilsConfig”的bean时出错:通过字段“ 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? 嵌套的异常是org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名称为'getSessionFactoty'的bean时出错:当前正在创建请求的bean:是否存在不可解析的循环引用? at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at 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 $ 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]

Please help me, 请帮我,

Thank you! 谢谢!

The issue that you are facing is due to the HibernateUtilsConfig.java configuration class that you have provided.In your EmployeeDao class you are autowiring the sessionfactory bean. 您面临的问题归因于您提供的HibernateUtilsConfig.java配置类。在EmployeeDao类中,您正在自动sessionfactory bean。 So , when springboot tries to autowire the bean , it fails with the following error : 因此,当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?

becase entityManagerFactory bean is not available. entityManagerFactory bean不可用。

Since you are using spring-boot , you may not configure everything manually. 由于您使用的是spring-boot,因此可能无法手动配置所有内容。 You can use the default auto configurations from spring-boot by adding the following dependency : 您可以通过添加以下依赖项来使用spring-boot中的默认自动配置:

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

then , you could provide the appropriate keys in the application.properties or application.yml and spring-boot will configure everything for you. 然后,您可以在application.properties或application.yml中提供适当的密钥,并且spring-boot将为您配置所有内容。

application.properties 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

If you still want to set everything up manually try creating the entity manager bean like : 如果您仍然想手动设置所有内容,请尝试创建实体管理器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;
   }

Reference documentation . 参考文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过字段“ sv”表示的不满意依赖性 - Unsatisfied dependency expressed through field 'sv' 通过字段“ freemarkerConfig”表示的不满意依赖性 - Unsatisfied dependency expressed through field 'freemarkerConfig' 通过“ productDao”字段表示的不满意依赖性 - Unsatisfied dependency expressed through field 'productDao' 通过字段“ clientRepository”表示不满意的依赖关系; - Unsatisfied dependency expressed through field 'clientRepository'; 获取:通过字段“ userRepository”表达的不满意的依赖性 - Getting: Unsatisfied dependency expressed through field 'userRepository' 通过字段和映射问题表达的不满意依赖性 - Unsatisfied dependency expressed through field and mapping issues 通过字段“springSecurityFilterChain”表达的不满意依赖 - Unsatisfied dependency expressed through field 'springSecurityFilterChain' Spring web应用+hibernate问题:不满足的依赖通过字段表达 - Spring web application + hibernate problem: Unsatisfied dependency expressed through field 初始化SolrRepository时出错:通过字段表示的不满意依赖性 - Error while initialising SolrRepository: Unsatisfied dependency expressed through field JAVA:创建带有名称的 bean 时出错; 通过字段表达的不满意依赖 - JAVA: Error creating bean with name; Unsatisfied dependency expressed through field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM