繁体   English   中英

尝试运行Spring Boot应用程序时出错。 创建名称为bean的错误

[英]Error trying to run Spring boot app. Error creating bean with name

我目前正在关注John Thompson Spring课程Spring Framework 5:Guru入门。 尝试构建第一个基本应用程序,但是我一直遇到错误。 我尝试在网上搜索解决方案,但没有任何效果。

这是pom.xml代码:

http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.firstspringproject</groupId>
<artifactId>first-spring-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>first-spring-project</name>
<description>First project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.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-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <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.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>

整个文件是根据我在Spring Initializr中选择的内容生成的。 我不断收到的错误说:

 [INFO] ------------------------------------------------------------------------
 [INFO] BUILD FAILURE
 [INFO] ------------------------------------------------------------------------
 [INFO] Total time: 4.648 s
 [INFO] Finished at: 2018-01-20T21:20:05+01:00
 [INFO] Final Memory: 38M/378M
 [INFO] ------------------------------------------------------------------------
 [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-          plugin:1.5.9.RELEASE:run (default-cli) on project first-spring-project: An      exception occurred while running. null: InvocationTargetException: Error      creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active). -> [Help 1]
 [ERROR]
 [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
 [ERROR] Re-run Maven using the -X switch to enable full debug logging.
 [ERROR]
 [ERROR] For more information about the errors and possible solutions, please read the following articles:
 [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

如果您需要更多详细信息,请询问。 我会尽力而为。

您最终遇到的错误是:

无法确定数据库类型NONE的嵌入式数据库驱动程序类。

根据文档 ,您需要定义一些属性,以便Spring知道您要与哪个数据库对话。 开箱即用,Spring Boot没有明智的默认设置。

您需要在application.properties定义的示例:

spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

驱动程序,URL和凭据将根据托管数据库的位置及其类型而变化。 有关属性的完整列表,请参见附录 而且,如果需要,您可以嵌入H2数据库,而不是像上面的示例那样使用MySql。

我相信您的spring数据源配置可能不完整,没有数据源很难确定。

根据安迪·威尔金森(Andy Wilkinson)对类似问题的回答

您没有为Spring Boot提供足够的信息来自动配置DataSource。 为此,您需要使用spring.datasource前缀向application.properties添加一些属性。 查看DataSourceProperties,以查看可以设置的所有属性。

您需要提供适当的url和驱动程序类名称:

 spring.datasource.url = … spring.datasource.driver-class-name = … 

但是,如果您的应用程序设计上根本没有数据源,那么您可能必须禁用自动配置器,如Stephan Isele所述

可以在没有数据源的情况下运行spring boot应用程序。 您必须禁用数据源的自动配置,并且可能也适用于JPA:

 @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class}) 

查看您的堆栈跟踪

嵌套的异常是org.springframework.boot.autoconfigure.jdbc.DataSourceProperties $ DataSourceBeanCreationException

无法确定数据库类型NONE的嵌入式数据库驱动程序类

您有要从特定配置文件加载的数据库设置,您可能需要将其激活(当前没有配置文件处于活动状态)

因此,请尝试修复您的代码。

  1. 检查您的数据源bean定义,它是否已附加到特定配置文件。我假设您在属性文件中提供了所有数据源,URL,驱动程序名称。 其他在这里阅读更多

     @Profile("dev")//This is how you attach your bean to profile. @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return DataSourceBuilder.create().build(); } 
  2. 如果上面的条件是正确的,那么您应该告诉spring容器使用哪个配置文件(例如spring.profiles.active = dev),除非在属性文件中指定了它。 在这里阅读更多

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM