简体   繁体   English

带有 Hibernate 应用程序的 Spring Boot 启动失败

[英]Spring Boot with Hibernate APPLICATION FAILED TO START

When I start my Spring Boot application, I got that:当我启动 Spring Boot 应用程序时,我得到了:

2020-11-11 03:02:24.333 WARN 20648 --- [ main] ConfigServletWebServerApplicationContext : 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; 2020-11-11 03:02:24.333 WARN 20648 --- [main] ConfigServletWebServerApplicationContext:上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“org.springframework”的 bean 时出错。 boot.autoconfigure.orm.jpa.HibernateJpaConfiguration':通过构造函数参数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;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class] 中定义名称为“dataSource”的 bean 创建错误:通过工厂方法的 Bean 实例化失败; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [com.zaxxer.hikari.HikariDataSource]:工厂方法“dataSource”抛出异常; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class 2020-11-11 03:02:24.335 INFO 20648 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2020-11-11 03:02:24.486 INFO 20648 --- [ main] ConditionEvaluationReportLoggingListener :嵌套异常是 org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException:无法确定合适的驱动程序类 2020-11-11 03:02:24.335 INFO 20648 --- [main] o.apache.catalina.core。 StandardService : 停止服务 [Tomcat] 2020-11-11 03:02:24.486 INFO 20648 --- [main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext.启动 ApplicationContext 时出错。 To display the conditions report re-run your application with 'debug' enabled.要显示条件报告,请在启用“调试”的情况下重新运行您的应用程序。 2020-11-11 03:02:24.489 ERROR 20648 --- [ main] osbdLoggingFailureAnalysisReporter : 2020-11-11 03:02:24.489 错误 20648 --- [主要] osbdLoggingFailureAnalysisReporter:


APPLICATION FAILED TO START应用程序无法启动


Description:描述:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.无法配置数据源:未指定“url”属性,无法配置嵌入的数据源。

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.请考虑以下事项: 如果您想要一个嵌入式数据库(H2、HSQL 或 Derby),请将其放在类路径中。 If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).如果您有要从特定配置文件加载的数据库设置,您可能需要激活它(当前没有配置文件处于活动状态)。

My pom.xml:我的 pom.xml:

   <?xml version="1.0" encoding="UTF-8"?>
<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>groupId</groupId>
    <artifactId>Netcracker-teamproject-2020</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.3.5.RELEASE</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.3.5.RELEASE</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.3.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.5.RELEASE</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>9.1.0.jre11-preview</version>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jdbc</artifactId>
            <version>9.0.10</version>
        </dependency>

        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.23.1-GA</version>
        </dependency>

    </dependencies>

</project>

Could you tell me please what should I do to fix that?你能告诉我我该怎么做才能解决这个问题吗?

Just use application.properties file to set up datasource url只需使用 application.properties 文件设置数据源 url

spring:
    datasource:
        url: 'jdbc:mssql://localhost:5432/dbname'
        username: username
        password: password 

I finally solved that, just by adding我终于解决了这个问题,只需添加

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

to the Spring Boot application.properties到 Spring Boot application.properties

My config file:我的配置文件:

package com.projectparty.config;

import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@EnableTransactionManagement
public class HibernateConfig {

    @Bean
    public DataSource dataSource() {
        return DataSourceBuilder.create()
                .driverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
                .url("jdbc:sqlserver://127.0.0.1:51264")
                .username("admin")
                .password("admin")
                .build();
    }


    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setHibernateProperties(hibernateProperties());

        return sessionFactory;
    }

    @Bean
    public PlatformTransactionManager hibernateTransactionManager() {
        HibernateTransactionManager transactionManager
                = new HibernateTransactionManager();
        transactionManager.setSessionFactory(sessionFactory().getObject());

        return transactionManager;
    }

    private Properties hibernateProperties() {
        Properties hibernateProperties = new Properties();
        hibernateProperties.setProperty(
                "hibernate.hbm2ddl.auto", "update");
        hibernateProperties.setProperty(
                "hibernate.dialect", "org.hibernate.SQLServerDialect");

        return hibernateProperties;
    }
}

This can be caused for a variety of reasons.这可能是由多种原因造成的。 In your case, I see that you chose JDBC connection " <artifactId>mssql-jdbc</artifactId> ", instead of Hikara connection pool.在您的情况下,我看到您选择了 JDBC 连接“ <artifactId>mssql-jdbc</artifactId> ”,而不是 Hikara 连接池。

Spring boot 2+ uses HikaraCP by default. Spring boot 2+ 默认使用 HikaraCP。 So, the dependency for HikariCp is on your class path "invoked by spring auto configurtion".因此,HikariCp 的依赖项位于“由 spring 自动配置调用”的类路径上。 To fix this, you may try excluding Hikari datasource, by adding the below to your pom.xml.要解决此问题,您可以尝试通过将以下内容添加到 pom.xml 来排除 Hikari 数据源。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    <exclusions>
        <exclusion>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Solution 2 is to not use JDBC "datasource type", use Hikara.解决方案2是不使用JDBC“数据源类型”,使用Hikara。 Sample Hikari config props.示例 Hikari 配置道具。

###
# HikariCP Properties
###

spring.datasource.hikari.connection-timeout=60000
spring.datasource.hikari.maximum-pool-size=200
spring.datasource.hikari.minimum-idle=30
spring.datasource.hikari.idle-timeout=3000
spring.datasource.hikari.poolName=HikariCP
spring.datasource.type=com.zaxxer.hikari.HikariDataSource



spring.datasource.hikari.jdbcUrl=**data source url**
spring.datasource.hikari.username=**username**
spring.datasource.hikari.password=**password**
spring.datasource.hikari.driverClassName=com.mssqlOrSomethingElse.jdbc.Driver

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

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