You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot 2.7项目部署本地Tomcat报驱动类识别失败求助

SpringBoot 2.7 WAR部署Tomcat时出现数据源驱动类无法识别问题

我有一个基于SpringBoot 2.7的Kotlin应用,在IntelliJ中使用注释内的普通启动类可以正常运行,但改成继承SpringBootServletInitializer打包成WAR部署到本地Tomcat时,catalina.log报错:

org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class


启动类代码

@SpringBootApplication
public class DemoApplication : SpringBootServletInitializer() {
    override fun configure(application: SpringApplicationBuilder): SpringApplicationBuilder? {
        return application.sources(DemoApplication::class.java)
    }

    @Throws(Exception::class)
    fun main(args: Array<String>) {
        SpringApplication.run(DemoApplication::class.java, *args)
    }
}

/*@SpringBootApplication
class DemoApplication

fun main(args: Array<String>) {
    runApplication<DemoApplication>(*args)
}*/

application.properties配置

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.url = jdbc:postgresql://localhost:5432/planesserver
spring.datasource.username = username 
spring.datasource.password = password
spring.datasource.driver-class-name=org.postgresql.Driver

Gradle配置

plugins {
    id("org.springframework.boot") version "2.7.0"
    id("io.spring.dependency-management") version "1.0.9.RELEASE"
    kotlin("jvm") version "1.7.0"
    kotlin("plugin.spring") version "1.7.0"
    kotlin("plugin.jpa") version "1.6.21"
    id("org.flywaydb.flyway") version "6.4.4"
}

group = "com.planes"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
    mavenCentral()
}

dependencies {
     implementation("org.apache.tomcat.embed:tomcat-embed-jasper")
     implementation("org.springframework.boot:spring-boot-starter-data-jpa")
     implementation("org.springframework.boot:spring-boot-starter-validation")
     implementation("org.springframework.boot:spring-boot-starter-security")
     implementation("org.springframework.boot:spring-boot-starter-web")
     implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
     implementation("org.jetbrains.kotlin:kotlin-reflect")
     implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
     implementation("io.jsonwebtoken:jjwt:0.9.0")
     implementation("org.postgresql:postgresql")
     implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core")
     testImplementation("org.springframework.boot:spring-boot-starter-test") {
         exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
    }
    testImplementation("org.springframework.security:spring-security-test")
}

tasks.withType<Test> {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "1.8"
    }
}

springBoot {
    mainClass.set( "com.planes.planesserver.DemoApplication")
}

解决思路

  1. 修正依赖配置(关键)
    WAR部署时必须排除内嵌Tomcat依赖,避免和外部Tomcat冲突,在Gradle的dependencies块中添加:

    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
    

    同时确认PostgreSQL驱动依赖为implementation,可显式指定兼容版本避免冲突:

    implementation("org.postgresql:postgresql:42.3.3")
    
  2. 修复配置文件格式错误
    配置中username和password后存在多余空格,会导致配置读取失败,修正为:

    spring.datasource.username=username
    spring.datasource.password=password
    

    可尝试移除spring.datasource.driver-class-name,SpringBoot会根据url自动识别驱动类,避免类加载问题。

  3. 验证WAR包内容
    解压生成的WAR包,检查WEB-INF/lib下是否存在postgresql-*.jar,WEB-INF/classes下是否有application.properties,确保配置和依赖都被正确打包。

  4. 排查Tomcat类加载问题
    不要将PostgreSQL驱动放在Tomcat全局lib目录下,避免类加载优先级冲突,确保驱动由WAR包自身提供。

内容的提问来源于stack exchange,提问作者Cristi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 17:02:47