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:创建名为“ 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] 在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]
请帮助我
谢谢!
答案 0 :(得分:0)
您面临的问题归因于您提供的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;
}