通过字段 '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);

    }

神不在的星期二
浏览 128回答 1
1回答

慕斯王

您面临的问题是由于HibernateUtilsConfig.java您提供的配置类引起的。在您的 EmployeeDao 类中,您正在自动装配sessionfactorybean。因此,当 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?因为entityManagerFactorybean 不可用。由于您使用的是 spring-boot ,因此您可能无法手动配置所有内容。您可以通过添加以下依赖项来使用 spring-boot 的默认自动配置:<dependency>&nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; <artifactId>spring-boot-starter-data-jpa</artifactId></dependency>然后,您可以在 application.properties 或 application.yml 中提供适当的键,spring-boot 将为您配置所有内容。application.propertiesspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.username=mysqluserspring.datasource.password=mysqlpassspring.datasource.url=jdbc:mysql://localhost:3306myDb?createDatabaseIfNotExist=true如果您仍想手动设置所有内容,请尝试创建实体管理器 bean,例如:&nbsp; &nbsp;@Bean&nbsp; &nbsp;public LocalContainerEntityManagerFactoryBean entityManagerFactory() {&nbsp; &nbsp; &nbsp; LocalContainerEntityManagerFactoryBean em&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; = new LocalContainerEntityManagerFactoryBean();&nbsp; &nbsp; &nbsp; em.setDataSource(dataSource());&nbsp; &nbsp; &nbsp; em.setPackagesToScan(new String[] { "com.example.persistence.model" });&nbsp; &nbsp; &nbsp; JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();&nbsp; &nbsp; &nbsp; em.setJpaVendorAdapter(vendorAdapter);&nbsp; &nbsp; &nbsp; em.setJpaProperties(additionalProperties());&nbsp; &nbsp; &nbsp; return em;&nbsp; &nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java