简体   繁体   中英

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

I'm currently following John Thompson Spring course Spring Framework 5: Beginner to Guru. Trying to build the first basic app, but I keep getting an error. I've tried searching the web for the solution, but nothing really works.

Here's the pom.xml code:

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>

The whole file was generated based on what i picked in Spring Initializr. The error I keep getting says:

 [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

If you need any more details, just ask. I'll try my best to deliver.

The error you are ultimately getting is this:

Cannot determine embedded database driver class for database type NONE.

According to the documentation , you need to define some properties so Spring knows what database you want to talk to. Out of the box, Spring Boot does not have a sensible default for this.

An example of what you will need to define in 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

The driver, url, and credentials will change depending on where you host your database and what type it is. A full list of properties can be found in the Appendix . And if you want, you can embed an H2 database instead of using MySql like the example above.

I believe your spring datasource config might be incomplete, it's hard to say for sure without the sources.

According to Andy Wilkinson's answer to a similar issue :

You haven't provided Spring Boot with enough information to auto-configure a DataSource. To do so, you'll need to add some properties to application.properties with the spring.datasource prefix. Take a look at DataSourceProperties to see all of the properties that you can set.

You'll need to provide the appropriate url and driver class name:

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

However, if your application simply doesn't have a datasouce by design, you might have to disable the autoconfigurer as explained by Stephan Isele :

It is possible to run a spring boot application without datasource. You must disable the auto configuration for the datasource and may be for JPA also :

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

Look at your stack trace

nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException

Cannot determine embedded database driver class for database type NONE

you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active)

So try fix your code.

  1. check your datasource bean defination, Is it being attached to specific profile.I assume you have all datasource, url,driver name provided in your property file. Else 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. If above is true, then you should tell spring container which profile to use (eg spring.profiles.active=dev), unless its specified in your property file. Read more here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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