SomeService需要一个名为' EntityManagerFactory'的豆子。找不到
我遇到这个问题。当我尝试运行Spring Boot应用程序时,我会收到此消息:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.todolist.service.TaskService required a bean named 'entityManagerFactory' that could not be found.
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
我使用的是Spring Data JPA,如果正确理解,EntityManager将在我使用例如JParepository接口时自动实现。 我想念什么吗?即使我正在使用JParepository,我也应该为EntityManager定义bean吗?我尝试将其更改为crudrepository,添加了Hibernate Entity Manager等的依赖关系,试图将基本软件包指定为我的主要应用程序类中的注释,试图在线找到解决方案,但似乎没有什么是正确的。
这是我的存储库接口:
@Repository
public interface TaskRepository extends JpaRepository<Task, UUID> {
}
使用此存储库的服务:
@Service
public class TaskService {
private final TaskRepository taskRepository;
private final ModelMapper modelMapper;
private final TaskMapper taskMapper;
public TaskService(TaskRepository taskRepository, ModelMapper modelMapper, TaskMapper taskMapper) {
this.taskRepository = taskRepository;
this.modelMapper = modelMapper;
this.taskMapper = taskMapper;
}
@Transactional
public TaskDTO addTask(CreateTaskDTO createTaskDTO) {
Task task = taskMapper.mapToEntity(createTaskDTO);
taskRepository.save(task);
return taskMapper.mapToDto(task);
}
@Transactional
public List<TaskDTO> fetchAll() {
List<Task> all = taskRepository.findAll();
return taskMapper.mapToDto(all);
}
@Transactional
public TaskDTO findById(UUID id) {
Optional<Task> task = taskRepository.findById(id);
return task.map(taskMapper::mapToDto).orElse(null);
}
}
@Component
public class TaskMapper {
public TaskDTO mapToDto(Task task) {
TaskDTO taskDTO = new TaskDTO();
taskDTO.setCategory(task.getCategory());
taskDTO.setDeadline(task.getDeadline());
taskDTO.setDescription(task.getDescription());
taskDTO.setId(task.getId());
taskDTO.setPriority(task.getPriority());
return taskDTO;
}
public List<TaskDTO> mapToDto(List<Task> taskList) {
if (Optional.ofNullable(taskList).isEmpty()) {
return new ArrayList<>();
} else {
return taskList.stream()
.map(this::mapToDto)
.collect(Collectors.toList());
}
}
public Task mapToEntity(CreateTaskDTO createTaskDTO) {
Task task = new Task();
task.setCategory(createTaskDTO.getCategory());
task.setDeadline(createTaskDTO.getDeadline());
task.setDescription(createTaskDTO.getDescription());
task.setPriority(createTaskDTO.getPriority());
return task;
}
}
ENTITY类:
@Entity
@NoArgsConstructor
@Getter
@Setter
@Table(name = Task.TABLE_NAME)
public class Task {
public static final String TABLE_NAME = "task";
public static final String COLUMN_PREFIX = "t_";
public Task(String description, Category category, int priority, LocalDate deadline) {
this.id = id;
this.description = description;
this.category = category;
this.priority = priority;
this.deadline = deadline;
}
@Id
@GeneratedValue
@Type(type = "uuid-char")
private UUID id;
@Size(min = 4, max = 20, message = "{task.validation.description}")
@NotNull(message = "{task.validation.null}")
@Column(name = COLUMN_PREFIX + "id")
private String description;
@NotNull(message = "{task.validation.null}")
@Column(name = COLUMN_PREFIX + "category")
private Category category;
@Min(value = 1, message = "{task.validation.priority}")
@Max(value = 5, message = "{task.validation.priority}")
@Column(name = COLUMN_PREFIX + "priority")
private int priority;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column(name = COLUMN_PREFIX + "deadline")
private LocalDate deadline;
}
DTOS:
@Data
public class CreateTaskDTO {
private String description;
private Category category;
private int priority;
private LocalDate deadline;
}
@Data
@NoArgsConstructor
public class TaskDTO {
private UUID id;
private String description;
private Category category;
private int priority;
private LocalDate deadline;
}
注入Beans的类:
@Configuration
public class Beans {
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
}
POM.XML文件:
<?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 https://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.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>todolist</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>todolist</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<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.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.22.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.6.6</version>
</dependency>
<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.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.4.5</version>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>3.0.6</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.16.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.16.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<version>1.16.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.1</version>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
App class:app class:class:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class }, scanBasePackages = "com.todolist")
@EntityScan
@EnableJpaRepositories
public class TodolistApplication {
public static void main(String[] args) {
SpringApplication.run(TodolistApplication.class, args);
}
}
I am stuck with this problem. When I try to run my spring boot app I get this message:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.todolist.service.TaskService required a bean named 'entityManagerFactory' that could not be found.
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
I am using Spring Data JPA and if I understand it correctly, the entityManager is automatically implemented to my project when I use for example JpaRepository interface.
Am I missing something? Should I define a bean for entityManager even though I am using JpaRepository? I tried changing it to CrudRepository, added dependencies for hibernate entity manager etc., tried to specify base packages to annotations in my main app class, tried to find solution online but nothing seems to be the right one.
Here is my repository interface:
@Repository
public interface TaskRepository extends JpaRepository<Task, UUID> {
}
Service that is using this repository:
@Service
public class TaskService {
private final TaskRepository taskRepository;
private final ModelMapper modelMapper;
private final TaskMapper taskMapper;
public TaskService(TaskRepository taskRepository, ModelMapper modelMapper, TaskMapper taskMapper) {
this.taskRepository = taskRepository;
this.modelMapper = modelMapper;
this.taskMapper = taskMapper;
}
@Transactional
public TaskDTO addTask(CreateTaskDTO createTaskDTO) {
Task task = taskMapper.mapToEntity(createTaskDTO);
taskRepository.save(task);
return taskMapper.mapToDto(task);
}
@Transactional
public List<TaskDTO> fetchAll() {
List<Task> all = taskRepository.findAll();
return taskMapper.mapToDto(all);
}
@Transactional
public TaskDTO findById(UUID id) {
Optional<Task> task = taskRepository.findById(id);
return task.map(taskMapper::mapToDto).orElse(null);
}
}
@Component
public class TaskMapper {
public TaskDTO mapToDto(Task task) {
TaskDTO taskDTO = new TaskDTO();
taskDTO.setCategory(task.getCategory());
taskDTO.setDeadline(task.getDeadline());
taskDTO.setDescription(task.getDescription());
taskDTO.setId(task.getId());
taskDTO.setPriority(task.getPriority());
return taskDTO;
}
public List<TaskDTO> mapToDto(List<Task> taskList) {
if (Optional.ofNullable(taskList).isEmpty()) {
return new ArrayList<>();
} else {
return taskList.stream()
.map(this::mapToDto)
.collect(Collectors.toList());
}
}
public Task mapToEntity(CreateTaskDTO createTaskDTO) {
Task task = new Task();
task.setCategory(createTaskDTO.getCategory());
task.setDeadline(createTaskDTO.getDeadline());
task.setDescription(createTaskDTO.getDescription());
task.setPriority(createTaskDTO.getPriority());
return task;
}
}
Entity class:
@Entity
@NoArgsConstructor
@Getter
@Setter
@Table(name = Task.TABLE_NAME)
public class Task {
public static final String TABLE_NAME = "task";
public static final String COLUMN_PREFIX = "t_";
public Task(String description, Category category, int priority, LocalDate deadline) {
this.id = id;
this.description = description;
this.category = category;
this.priority = priority;
this.deadline = deadline;
}
@Id
@GeneratedValue
@Type(type = "uuid-char")
private UUID id;
@Size(min = 4, max = 20, message = "{task.validation.description}")
@NotNull(message = "{task.validation.null}")
@Column(name = COLUMN_PREFIX + "id")
private String description;
@NotNull(message = "{task.validation.null}")
@Column(name = COLUMN_PREFIX + "category")
private Category category;
@Min(value = 1, message = "{task.validation.priority}")
@Max(value = 5, message = "{task.validation.priority}")
@Column(name = COLUMN_PREFIX + "priority")
private int priority;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column(name = COLUMN_PREFIX + "deadline")
private LocalDate deadline;
}
Dtos:
@Data
public class CreateTaskDTO {
private String description;
private Category category;
private int priority;
private LocalDate deadline;
}
@Data
@NoArgsConstructor
public class TaskDTO {
private UUID id;
private String description;
private Category category;
private int priority;
private LocalDate deadline;
}
Class for injecting beans:
@Configuration
public class Beans {
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
}
pom.xml file:
<?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 https://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.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>todolist</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>todolist</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<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.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.22.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.6.6</version>
</dependency>
<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.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.4.5</version>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>3.0.6</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.16.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.16.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<version>1.16.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.1</version>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
App class:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class }, scanBasePackages = "com.todolist")
@EntityScan
@EnableJpaRepositories
public class TodolistApplication {
public static void main(String[] args) {
SpringApplication.run(TodolistApplication.class, args);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将此依赖性添加到pom.xml
org.hibernate Hibernate-entityManager 5.2.3.final
Add this dependency to pom.xml
org.hibernate hibernate-entitymanager 5.2.3.Final
您忘了在TaskService类中添加@Autowire在构造函数的顶部
You forgot to add @Autowire on the top of the Constructor in TaskService class