首页 文章

Spring启动(带jpa的mysql):没有名为'entityManagerFactory'的bean可用

提问于
浏览
7

开始构建(第一个)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 靴之后是否有必要?

如果那件事,我的回购看起来像这样

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

. ____ _ __ _ _ / \ /'__ () __ __ _ \ \ \ \(()_ |'_ |'| |'/`| \ \ \ \ \ /)| |)| | | | | || (||))))'| ____ | . || ||| | __,| / / / / ========= | _ | ============== | ___ / = /// _ / :: Spring Boot ::(v1.5.6 . 发布)2017-09-13 12:48:38.808 INFO 15361 --- [main] com.rightlink.RightLinkApplication:在Admins-MacBook-Pro.local上使用PID 15361启动RightLinkApplication(/ Users / sureshatta / Documents / workspace-sts -3.9.0.RELEASE / RightLink / target / classes由sureshatta在/Users/sureshatta/Documents/workspace-sts-3.9.0.RELEASE/RightLink启动)2017-09-13 12:48:38.811 INFO 15361 --- [main] com.rightlink.RightLinkApplication:没有活动的配置文件集,回退到默认配置文件:默认2017-09-13 12:48:38.865 INFO 15361 --- [main] ationConfigEmbeddedWebApplicationContext:刷新org.springframework.boot.context . embedded.AnnotationConfigEmbeddedWebApplicationContext@120d6fe6:启动日期[Wed Sep 13 12:48:38 IST 2017];上下文层次结构2017-09-13 12:48:40.380 INFO 15361 --- [main] sbcetTomcatEmbeddedServletContainer:用端口初始化的Tomcat:8181(http)2017-09-13 12:48:40.402 INFO 15361 - - [main] o.apache.catalina.core.StandardService:启动服务[Tomcat] 2017-09-13 12:48:40.403 INFO 15361 --- [main] org.apache.catalina.core.StandardEngine:启动Servlet引擎:Apache Tomcat / 8.5.16 2017-09-13 12:48:40.566 INFO 15361 --- [ost-startStop-1] oaccC [Tomcat] . [localhost] . [/]:初始化Spring嵌入式WebApplicationContext 2017-09 -13 12:48:40.567 INFO 15361 --- [ost-startStop-1] osweb.context.ContextLoader:Root WebApplicationContext:初始化在1705 ms完成2017-09-13 12:48:40.743 INFO 15361 --- [ ost-startStop-1] osbwservlet.ServletRegistrationBean:映射servlet:'dispatcherServlet'到[/] 2017-09-13 12:48:40.752 INFO 15361 --- [ost-startStop-1] osbwservlet.FilterRegistrationBean:映射过滤器:'characterEncodingFilter'为:[/] 2 017-09-13 12:48:40.753 INFO 15361 --- [ost-startStop-1] osbwservlet.FilterRegistrationBean:映射过滤器:'hiddenHttpMethodFilter'到:[/] 2017-09-13 12:48:40.753 INFO 15361 --- [ost-startStop-1] osbwservlet.FilterRegistrationBean:映射过滤器:'httpPutFormContentFilter'到:[/] 2017-09-13 12:48:40.753 INFO 15361 --- [ost-startStop-1] osbwservlet .FilterRegistrationBean:映射过滤器:'requestContextFilter'到:[/] 2017-09-13 12:48:40.844 WARN 15361 --- [main] ationConfigEmbeddedWebApplicationContext:在上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans . factory.UnsatisfiedDependencyException:创建名为'registerController'的bean时出错:通过字段'regServices'表示的不满意的依赖关系;嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'regService'的bean时出错:通过字段'registrationRepo'表示的不满意依赖;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'registrationRepo'的bean时出错:设置bean时无法创建[org.springframework.orm.jpa.SharedEntityManagerCreator]类型的内部bean'(内部bean)#43c67247' property'entalManager';嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'(内部bean)#43c67247'的bean时出错:在设置构造函数参数时无法解析对bean'entalManagerFactory'的引用;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为'entityManagerFactory'的bean可用2017-09-13 12:48:40.849 INFO 15361 --- [main] o.apache.catalina.core.StandardService:停止服务[Tomcat] 2017-09-13 12:48:40.903 INFO 15361 --- [main] utoConfigurationReportLoggingInitializer:启动ApplicationContext时出错 . 要显示自动配置报告,请在启用“debug”的情况下重新运行应用程序 . 2017-09-13 12:48:40.975 ERROR 15361 --- [main] o.s.b.d.LoggingFailureAnalysisReporter:APPLICATION FAILED TO START描述:com.rightlink.services.RegistrationService中的字段registrationRepo需要一个名为'entityManagerFactory'的bean无法找到 . 操作:考虑在配置中定义名为“entityManagerFactory”的bean .

application.properties

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 注释 . 什么都没有帮助 .

1 回答

  • 3

    问题不在于直接启动版本,问题在于staled 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文件夹,现在看来还可以 .

相关问题