java - 无法在 springboot 中自动装配 sessionFactory
问题描述
请在我的daoImpl
班级下面找到。
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class EmployeeDaoImpl implements EmployeeDao {
@Autowired
private SessionFactory sessionFactory;
// all my setter and other methods
}
pom.xml
:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
我的 springBoot 应用程序代码:
package com.sagarp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class EmployeeHibernateApplication {
public static void main(String[] args) {
SpringApplication.run(EmployeeHibernateApplication.class, args);
}
}
我的application.properties
文件:
server.port=8080
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url = jdbc:mysql://localhost:3306/sagarp?useSSL=false
spring.datasource.username = root
spring.datasource.password = password_123
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
#spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
场景:当我启动我的 springboot 应用程序时,它无法自动装配sessionFactory
对象。
PS:我知道我可以使用“spring-jpa and
crud repository”而不是hibernate。事情是我必须使用休眠。将其视为一种限制。
错误:
说明:com.sagarp.employee.EmployeeDaoImpl 中的字段 sessionFactory 需要一个无法找到的“org.hibernate.SessionFactory”类型的 bean。
谁能在这里帮我一点忙
解决方案
如果您想知道版本低于 5.2 的 Hibernate 中 SessionFactory 的代码
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
推荐阅读
- c++ - 遍历整个图的最小节点数
- java - 我可以重载Java中的接口吗?
- mysql - 将数据从 s3 加载到 mysql aurora
- angular - Angular 6 - 登录错误以返回 http 状态 403
- vue.js - 正确设置 prerender-spa-plugin
- html - 引导文本中的文本-井不会居中
- java - Java 和 Xerces:找不到属性 XMLConstants.ACCESS_EXTERNAL_DTD
- javascript - 使用 jquery 中的每个循环的 Next 和 Prev 按钮功能
- c - C 和 MATLAB:在 C 中创建空数组调用 Matlab 函数来填充它以错误结尾
- c++ - 如何在 C++ DLL 项目中使用第 3 方 DLL?