【发布时间】:2019-12-05 05:02:08
【问题描述】:
我正在使用 Spring boot 并在此错误中运行:
一个组件需要一个找不到的“javax.persistence.EntityManagerFactory”类型的bean。
行动:
考虑在您的配置中定义一个 javax.persistence.EntityManagerFactory 类型的 bean。
我不知道我应该做什么,因为我做的一切都是基于课程
我使用@PersistanceUnit 注入EntityManagerFactory
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>pl.javastart</groupId>
<artifactId>spring-jpa-boot2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-jpa-boot2</name>
<description>Spring boot app</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package pl.javastart.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Repository;
import pl.javastart.model.Book;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceUnit;
@Repository
public class BookDaoImpl implements BookDao {
@PersistenceUnit
private EntityManagerFactory emFactory;
public BookDaoImpl(){
}
@Override
public void save(Book book) {
EntityManager entityManager = emFactory.createEntityManager();
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
entityManager.persist(book);
tx.commit();
entityManager.close();
}
@Override
public Book get(Long id) {
EntityManager entityManager = emFactory.createEntityManager();
Book book = entityManager.find(Book.class, id);
entityManager.close();
return book;
}
}
package pl.javastart;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import pl.javastart.dao.BookDao;
import pl.javastart.model.Book;
@Configuration
@ComponentScan
public class SpringJpaApplication {
public static void main(String[] args) throws InterruptedException {
ConfigurableApplicationContext ctx = SpringApplication.run(SpringJpaApplication.class, args);
BookDao dao = ctx.getBean(BookDao.class);
Book book = new Book("1234567890468", "Spring is so cool", "Javastart");
dao.save(book);
Thread.sleep(5000);
}
}
我该如何解决这个问题?
【问题讨论】:
-
您没有 Spring Boot 应用程序;您没有使用
@EnableAutoConfiguration或@SpringBootApplication(包含它)。另请注意,您正在重新发明 Spring Data;它会自动为您创建整个BookDaoImpl(请参阅CrudRepository)。 -
我添加了
@SpringBootApplication,但现在查看org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] -
您包含了 MySQL 驱动程序;您是否为数据库添加了 Spring Boot 属性?
-
在资源中是 -> application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/library?useSSL=false spring.datasource.username=root spring.datasource.password=admin spring.jpa.hibernate.ddl-auto=create spring.datasource.tomcat.initial-size=5
标签: java mysql spring hibernate spring-boot