无法自动配置数据源:"spring.datasource.url" [英] Failed to auto-configure a DataSource: 'spring.datasource.url'

查看:187
本文介绍了无法自动配置数据源:"spring.datasource.url"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的是Spring的新人,特别是Spring Boot.请求求你,尽可能明确,给我文档阅读,这样我就可以很好地理解我该怎么做.

我的项目使用的是MySQL + JavaFX + Spring Boot + Spring Data JPA + Hibernate框架/技术.

这是我的POM文件.

<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.nubeclick</groupId>
<artifactId>pos</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>POSNubeClick</name>
<description>Sistema de punto de venta (Point Of Sale).</description>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.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>
    <slf4j.version>1.7.12</slf4j.version>
    <log4j.version>1.2.17</log4j.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
    <dependency>
        <groupId>com.miglayout</groupId>
        <artifactId>miglayout-javafx</artifactId>
        <version>5.0</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>
    <!-- <dependency> -->
    <!-- <groupId>org.slf4j</groupId> -->
    <!-- <artifactId>slf4j-api</artifactId> -->
    <!-- <version>${slf4j.version}</version> -->
    <!-- </dependency> -->
    <!-- <dependency> -->
    <!-- <groupId>org.slf4j</groupId> -->
    <!-- <artifactId>jcl-over-slf4j</artifactId> -->
    <!-- <version>${slf4j.version}</version> -->
    <!-- </dependency> -->
    <!-- <dependency> -->
    <!-- <groupId>org.slf4j</groupId> -->
    <!-- <artifactId>slf4j-log4j12</artifactId> -->
    <!-- <version>${slf4j.version}</version> -->
    <!-- </dependency> -->
    <!-- <dependency> -->
    <!-- <groupId>log4j</groupId> -->
    <!-- <artifactId>log4j</artifactId> -->
    <!-- <version>${log4j.version}</version> -->
    <!-- </dependency> -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <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>
    <dependency>
        <groupId>com.jfoenix</groupId>
        <artifactId>jfoenix</artifactId>
        <version>1.10.0</version>
    </dependency>
    <dependency>
        <groupId>de.jensd</groupId>
        <artifactId>fontawesomefx</artifactId>
        <version>8.9</version>
    </dependency>
    <dependency>
        <groupId>org.controlsfx</groupId>
        <artifactId>controlsfx</artifactId>
        <version>8.40.13</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

这是我的.properties配置.

spring.main.banner-mode=off

# Datasource connection properties
spring.datasource.url=jdbc:mysql://localhost/posnubeclick
spring.datasource.username=nubeclick
spring.datasource.password=nubeclick
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# JPA Properties
spring.jpa.database=posnubeclick

# Hibernate Configuration Properties
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.current_session_context_class=thread
spring.jpa.properties.hibernate.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

# Naming strategy
spring.jpa.hibernate.naming-strategy =org.hibernate.cfg.ImprovedNamingStrategy

#Turn Statistics on
spring.jpa.properties.hibernate.generate_statistics=true

# logging
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
logging.level.org.hibernate.stat=debug
logging.level.org.hibernate.type=trace
logging.level.org.hibernate.SQL=debug
#logging.level.org.hibernate.type.descriptor.sql=trace
logging.level.=error

这是我的主要课程

@SpringBootApplication(scanBasePackages = { "com.nubeclick.pos" })
public class MainApp extends Application {

private static final Logger log = LoggerFactory.getLogger(MainApp.class);

public static void main(String[] args) throws Exception {

    SpringApplication.run(MainApp.class, args);
    // launch(args);
}

@Override
public void start(Stage stage) throws Exception {
    try {
        log.info("Starting Hello JavaFX and Maven demonstration application");

        String fxmlFile = "/fxml/Main.fxml";
        log.debug("Loading FXML for main view from: {}", fxmlFile);
        FXMLLoader loader = new FXMLLoader();
    Parent rootNode = (Parent) loader.load(getClass().getResourceAsStream(fxmlFile));

        log.debug("Showing JFX scene");
        Scene scene = new Scene(rootNode);
        scene.getStylesheets().add("/styles/styles.css");
        stage.setTitle("NubeClick - Point of Sales");
        stage.setScene(scene);
        stage.show();
    } catch (Exception e) {
    // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

这是堆栈跟踪:

在上下文初始化期间遇到异常-取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为"org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration"的bean时出错:通过构造函数参数表示的不满意依赖关系0;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建在类路径资源[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration $ Hikari.class]中定义的名称为'dataSource'的bean时出错:通过工厂方法实例化Bean失败;嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[com.zaxxer.hikari.HikariDataSource]:工厂方法'dataSource'引发了异常;嵌套的异常是org.springframework.boot.autoconfigure.jdbc.DataSourceProperties $ DataSourceBeanCreationException:无法确定合适的驱动程序类

Spring消息是这样的:

说明:

无法自动配置数据源:未指定'spring.datasource.url',并且无法自动配置嵌入式数据源.

原因:无法确定合适的驱动程序类别

动作:

请考虑以下内容: 如果需要嵌入式数据库(H2,HSQL或Derby),请将其放在类路径中.

如果要从特定配置文件加载数据库设置,则可能需要激活它(当前没有配置文件处于活动状态).

我已参考此其他帖子和此其他帖子

我不知道该怎么办,也许从零开始重新开始,但我想使这些框架适应我的当前项目.

该怎么办?

编辑


疯狂的事情,我决定将项目上传到另一个存储库,对项目文件夹进行了彻底清理(删除了.project,.settings,.classpath,bin,target),然后将项目重新导入到eclipse中,进行了配置->添加maven本质,现在该错误消失了,至少这个错误又出现了一些其他错误,但是现在它从属性文件中加载了所有内容,所以,为什么会发生这种情况?

解决方案

我决定将项目上传到另一个存储库,对项目文件夹进行了彻底的清理(删除了.project,.settings,.classpath,bin,target)并将项目重新导入到eclipse中,是否进行了配置->添加了maven nature,现在该错误消失了,至少这个错误出现了一些其他错误,但是现在它从属性文件中加载了所有内容,所以,为什么会发生这种情况?

I'm really new into Spring, specifically Spring Boot. Please I beg you, be the most explicit possible, give me docs to read so I can understand very well what I have to do.

My project is using MySQL+JavaFX+Spring Boot+Spring Data JPA+Hibernate frameworks/technologies.

This is my POM file.

<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.nubeclick</groupId>
<artifactId>pos</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>POSNubeClick</name>
<description>Sistema de punto de venta (Point Of Sale).</description>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.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>
    <slf4j.version>1.7.12</slf4j.version>
    <log4j.version>1.2.17</log4j.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
    <dependency>
        <groupId>com.miglayout</groupId>
        <artifactId>miglayout-javafx</artifactId>
        <version>5.0</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>
    <!-- <dependency> -->
    <!-- <groupId>org.slf4j</groupId> -->
    <!-- <artifactId>slf4j-api</artifactId> -->
    <!-- <version>${slf4j.version}</version> -->
    <!-- </dependency> -->
    <!-- <dependency> -->
    <!-- <groupId>org.slf4j</groupId> -->
    <!-- <artifactId>jcl-over-slf4j</artifactId> -->
    <!-- <version>${slf4j.version}</version> -->
    <!-- </dependency> -->
    <!-- <dependency> -->
    <!-- <groupId>org.slf4j</groupId> -->
    <!-- <artifactId>slf4j-log4j12</artifactId> -->
    <!-- <version>${slf4j.version}</version> -->
    <!-- </dependency> -->
    <!-- <dependency> -->
    <!-- <groupId>log4j</groupId> -->
    <!-- <artifactId>log4j</artifactId> -->
    <!-- <version>${log4j.version}</version> -->
    <!-- </dependency> -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <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>
    <dependency>
        <groupId>com.jfoenix</groupId>
        <artifactId>jfoenix</artifactId>
        <version>1.10.0</version>
    </dependency>
    <dependency>
        <groupId>de.jensd</groupId>
        <artifactId>fontawesomefx</artifactId>
        <version>8.9</version>
    </dependency>
    <dependency>
        <groupId>org.controlsfx</groupId>
        <artifactId>controlsfx</artifactId>
        <version>8.40.13</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

This is my .properties configuration.

spring.main.banner-mode=off

# Datasource connection properties
spring.datasource.url=jdbc:mysql://localhost/posnubeclick
spring.datasource.username=nubeclick
spring.datasource.password=nubeclick
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# JPA Properties
spring.jpa.database=posnubeclick

# Hibernate Configuration Properties
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.current_session_context_class=thread
spring.jpa.properties.hibernate.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

# Naming strategy
spring.jpa.hibernate.naming-strategy =org.hibernate.cfg.ImprovedNamingStrategy

#Turn Statistics on
spring.jpa.properties.hibernate.generate_statistics=true

# logging
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
logging.level.org.hibernate.stat=debug
logging.level.org.hibernate.type=trace
logging.level.org.hibernate.SQL=debug
#logging.level.org.hibernate.type.descriptor.sql=trace
logging.level.=error

This is my main class

@SpringBootApplication(scanBasePackages = { "com.nubeclick.pos" })
public class MainApp extends Application {

private static final Logger log = LoggerFactory.getLogger(MainApp.class);

public static void main(String[] args) throws Exception {

    SpringApplication.run(MainApp.class, args);
    // launch(args);
}

@Override
public void start(Stage stage) throws Exception {
    try {
        log.info("Starting Hello JavaFX and Maven demonstration application");

        String fxmlFile = "/fxml/Main.fxml";
        log.debug("Loading FXML for main view from: {}", fxmlFile);
        FXMLLoader loader = new FXMLLoader();
    Parent rootNode = (Parent) loader.load(getClass().getResourceAsStream(fxmlFile));

        log.debug("Showing JFX scene");
        Scene scene = new Scene(rootNode);
        scene.getStylesheets().add("/styles/styles.css");
        stage.setTitle("NubeClick - Point of Sales");
        stage.setScene(scene);
        stage.show();
    } catch (Exception e) {
    // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

This is the stacktrace:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': 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$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

The Spring message is this:

Description:

Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.

Reason: Failed to determine a suitable driver class

Action:

Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.

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

I have refered to this other post and this other one

I don't know what else to do, maybe start all over again from zero but I want to adapt those frameworks to my current project.

What can I do to solve this?

Edit


Crazy thing, I decided to upload the project to another repository, did a whole clean to the project folder (deleted .project, .settings, .classpath, bin, target) and reimported project into eclipse, did configure -> add maven nature, and now the error is gone, at least this error, got some other errors but now it loads everything from the properties file, so, Why did this happened?

解决方案

I decided to upload the project to another repository, did a whole clean to the project folder (deleted .project, .settings, .classpath, bin, target) and reimported project into eclipse, did configure -> add maven nature, and now the error is gone, at least this error, got some other errors but now it loads everything from the properties file, so, Why did this happened?

这篇关于无法自动配置数据源:"spring.datasource.url"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆