【问题标题】:Spring Boot exception, error creating beanSpring Boot异常,创建bean时出错
【发布时间】:2017-02-07 11:54:08
【问题描述】:

我有一个 maven spring boot 应用程序;当我从 Eclipse 启动它时它工作得非常好,但是当我从命令行调用它时:

1) mvn 包

2) java –jar target/myapp.jar

正在抛出错误:

11:34:48.418 [main] WARN  o.s.b.c.e.AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [com/myapp/configuration/JpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.IllegalStateException: required key [datasource.sampleapp.hibernate.dialect] not found
11:34:48.421 [main] INFO  o.a.catalina.core.StandardService - Stopping service Tomcat
11:34:48.436 [localhost-startStop-1] WARN  o.a.c.loader.WebappClassLoaderBase - The web application [myapp] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.lang.Object.wait(Native Method)
 java.lang.ref.ReferenceQueue.remove(Unknown Source)
 com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)
11:34:48.441 [main] WARN  o.s.boot.SpringApplication - Error handling failed (Error creating bean with name 'delegatingApplicationListener' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available)
11:34:48.448 [main] ERROR o.s.boot.SpringApplication - Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [com/myapp/configuration/JpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.IllegalStateException: required key [datasource.sampleapp.hibernate.dialect] not found

这里我的 pom.xml 虽然当我从 eclipse 启动应用程序时工作正常,所以我不认为错误在这里:

<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.myapp</groupId>
    <artifactId>MyApp</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name> MyApp </name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <h2.version>1.4.187</h2.version>
    </properties>

    <dependencies>
        …
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                  <configuration>
                    <archive>
                     <manifest>
                       <mainClass>com.playganizer.PlayganizerBackend</mainClass>
                     </manifest>
                   </archive>
                 </configuration>
            </plugin>
        </plugins>
    </build>
</project>

application.properties:

jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/myapp
jdbc.username = myuser
jdbc.password = mypass
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.format_sql = true

有什么想法吗?

【问题讨论】:

    标签: spring maven spring-boot


    【解决方案1】:

    将您的配置更改为此我认为它会起作用:

    # Connection url for the database "myapp"
    spring.datasource.url = jdbc:mysql://localhost:3306/myapp
    # Username and password
    spring.datasource.username = myuser
    spring.datasource.password = mypass
    # Show or not log for each sql query
    spring.jpa.show-sql = true
    spring.jpa.hibernate.ddl-auto = update
    # Naming strategy
    spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
    # Allows Hibernate to generate SQL optimized for a particular DBMS
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
    

    检查你的 pom.xml 中是否有这些依赖项:

           <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>
    

    【讨论】:

    • 嘿,感谢您的回答,我在帖子中添加了 application.properties,它看起来正确吗?
    【解决方案2】:

    如果你再次阅读日志,你会发现 java.lang.IllegalStateException: required key [datasource.sampleapp.hibernate.dialect] not found,你需要定义这个属性来创建bean。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-03
      • 2015-07-23
      • 1970-01-01
      • 2023-02-09
      • 2014-10-28
      • 2019-02-12
      • 2020-11-19
      • 2020-06-05
      相关资源
      最近更新 更多