gpt4 book ai didi

java - Spring 启动(带 jpa 的 mysql): No bean named 'entityManagerFactory' available

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:00:22 25 4
gpt4 key购买 nike

开始构建(第一个)spring boot 应用程序,这是我的 spring boot 主类 ( Full code on github )

@EnableAutoConfiguration
@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

这是 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>

我不断收到以下错误。我想补充什么?为什么我需要添加一个名为“entityManagerFactory”的 bean bean??并且即使在使用 spring boot 之后也有必要吗??

如果重要的话,我的仓库看起来像这样

public interface RegistrationRepo extends JpaRepository<User, Long> {
User findByEmail(String email);
}

. ____ _ __ _ _ /\ / ' __ _ ()_ __ __ _ \ \ \ \ ( ( )_ | '_ | '| | ' / ` | \ \ \ \ \/ )| |)| | | | | || (| | ) ) ) ) ' |____| .|| ||| |__, | / / / / =========|_|==============|___/=///_/ :: Spring Boot :: (v1.5.6.RELEASE)

2017-09-13 12:48:38.808 INFO 15361 --- [ main] com.rightlink.RightLinkApplication : Starting RightLinkApplication on Admins-MacBook-Pro.local with PID 15361 (/Users/sureshatta/Documents/workspace-sts-3.9.0.RELEASE/RightLink/target/classes started by sureshatta in /Users/sureshatta/Documents/workspace-sts-3.9.0.RELEASE/RightLink) 2017-09-13 12:48:38.811 INFO 15361 --- [ main] com.rightlink.RightLinkApplication : No active profile set, falling back to default profiles: default 2017-09-13 12:48:38.865 INFO 15361 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@120d6fe6: startup date [Wed Sep 13 12:48:38 IST 2017]; root of context hierarchy 2017-09-13 12:48:40.380 INFO 15361 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8181 (http) 2017-09-13 12:48:40.402 INFO 15361 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2017-09-13 12:48:40.403 INFO 15361 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.16 2017-09-13 12:48:40.566 INFO 15361 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2017-09-13 12:48:40.567 INFO 15361 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1705 ms 2017-09-13 12:48:40.743 INFO 15361 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2017-09-13 12:48:40.752 INFO 15361 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/] 2017-09-13 12:48:40.753 INFO 15361 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/] 2017-09-13 12:48:40.753 INFO 15361 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/] 2017-09-13 12:48:40.753 INFO 15361 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/] 2017-09-13 12:48:40.844 WARN 15361 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'registerController': Unsatisfied dependency expressed through field 'regServices'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'regService': Unsatisfied dependency expressed through field 'registrationRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registrationRepo': Cannot create inner bean '(inner bean)#43c67247' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#43c67247': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available 2017-09-13 12:48:40.849 INFO 15361 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2017-09-13 12:48:40.903 INFO 15361 --- [ main] utoConfigurationReportLoggingInitializer :

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 2017-09-13 12:48:40.975 ERROR 15361 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :


APPLICATION FAILED TO START


Description:

Field registrationRepo in com.rightlink.services.RegistrationService required a bean named 'entityManagerFactory' that could not be found.

Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

应用程序属性

spring.datasource.url=jdbc:mysql://localhost:3306/Test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
server.port=8181

尝试为 repo 添加 @Repository 注解,并在主类中添加 @enableJpaRepositories 注解。没有任何帮助。

最佳答案

问题不直接与引导版本有关,而与陈旧的 hibernate jar 有关。由于我向项目添加了 JPA 支持,因此在内部使用(??)Hibernate 作为 JPA 实现启动并尝试使用(??)那些 jar 。由于我的本地存储库损坏了 hibernate-entity-* jars,因此发生了这个奇怪的错误。

他们的 comment in the code确认他们在内部使用 hibernate 。

 <!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

从我的 Maven 本地存储库中删除了完整的 hibernate 文件夹,现在看起来没问题了。

关于java - Spring 启动(带 jpa 的 mysql): No bean named 'entityManagerFactory' available,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46190317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com