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**
您面临的问题是由于您提供的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>
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
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;
}
以下是例外情况: 我的代码: 我评论了setCacheable(true),因为它给出了一个错误。有人能帮忙吗?
正在尝试测试我的。。。编译器请求注入,但在为创建bean后,它返回通过字段 代码: 错误:
无法解决测试Spring Boot应用程序的问题: 主控制器: 应用程序属性:
大家好,每个人都是spring的新手,正在尝试为CRUD创建一个简单的应用程序,但是我得到了下面的错误。我正在使用STS 创建名为“Client ServiceImpli”的bean时出错:通过字段“Client Repository”表示的不满足的依赖关系;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“clie
错误:-->org.springframework.beans.factory.unsatisfieddependencyexception:创建名为“home controller”的bean时出错:通过字段“user tokenservice”表示的不满足的依赖项;嵌套异常为org.springframework.beans.factory.unsatisfieddependencyexcep
我有以下代码和结构。我得到以下错误,这是很长的错误消息。 创建名称为'departmentController'的bean时出错:通过字段'departmentService'表示的不满意的依赖项;嵌套异常org.springframework.beans.factory.不满意依赖异常: 实体类 存储库接口 服务等级 控制器类 主课 项目结构 完整错误堆栈跟踪: