代码之家  ›  专栏  ›  技术社区  ›  Maciaz

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

  •  0
  • Maciaz  · 技术社区  · 7 年前

    我目前正在学习约翰·汤普森的春季课程《春季框架5:从初学者到大师》。正在尝试构建第一个基本应用程序,但我不断遇到错误。我试着在网上搜索解决方案,但没有任何效果。

    这是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 Initializer中选择的内容生成的。我不断收到的错误是:

     [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
    

    如果你需要更多的细节,尽管问。我会尽我最大的努力。

    3 回复  |  直到 7 年前
        1
  •  2
  •   Todd    7 年前

    您最终会遇到以下错误:

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

    According to the documentation ,您需要定义一些属性,以便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和凭据将根据您托管数据库的位置和类型而变化。可以找到完整的属性列表 in the Appendix . 如果需要,可以嵌入H2数据库,而不是像上面的示例那样使用MySql。

        2
  •  1
  •   Simon Berthiaume    7 年前

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

    根据 Andy Wilkinson's answer to a similar issue :

    您没有为Spring Boot提供足够的信息来自动配置数据源。为此,您需要向应用程序添加一些属性。弹簧的属性。数据源前缀。查看DataSourceProperty以查看可以设置的所有属性。

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

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

    但是,如果您的应用程序在设计上没有datasouce,那么您可能必须禁用自动配置器,如下所述 Stephan Isele :

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

    @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
    
        3
  •  0
  •   Community CDub    4 年前

    查看堆栈跟踪

    嵌套异常为org。springframework。靴子自动配置。jdbc。DataSourceProperties$DataSourceBeanCreationException

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

    您可能需要从特定配置文件加载数据库设置以激活它(当前没有激活的配置文件)

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

    1. 检查您的数据源bean定义,它是否附加到特定的概要文件。我假设您在属性文件中提供了所有数据源、url和驱动程序名称。其他的 read more here

      @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),除非在属性文件中指定了它。 Read more here