首页 > 解决方案 > 将SB项目作为Jar文件运行时无法启动ServletWebServerApplicationContext,但在IDEA中执行时很好

问题描述

我的 SpringBoot web-app 构建良好,但是当我尝试执行 jar 文件时,它给出了无法启动的错误。当我在 IDEA 中运行 Gradle bootRun 时,网络应用程序可以正确启动并且功能齐全。

Caused by: org.springframework.context.ApplicationContextException: Unable to start `ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.`

17:33:15.971 [main] WARN org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

SpringBoot 应用类:

@SpringBootApplication
@EnableEncryptableProperties
public class ProjectApplication extends SpringBootServletInitializer {

public static void main(String[] args) {
    SpringApplication.run(ProjectApplication.class, args);
}

我也使用 Jasypt 来加密应用程序属性。

应用程序属性:

spring.jpa.hibernate.naming.physical-

strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
server.port=8080
app.key = ENC(u+S5jaQft+NC5tHhtnZyNOl1bRskpmRufwK3aPuQYb0=)

# MariaDB / MySQL connection detail
jasypt.encryptor.password=Leaf23

spring.profiles.active=prod
server.error.path=/error
jasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator
jasypt.encryptor.algorithm=PBEWithMD5AndDES

应用程序-prod.properties:

# MariaDB / MySQL connection details
spring.datasource.url=jdbc:mariadb://localhost:3306/leafProject
spring.datasource.username=webappuser
spring.datasource.password=ENC(VJPGTDVqPqsBnnzKXOtj10sZ3/ui+Y38

# SSL information for TSL certification
server.ssl.key-store-password=ENC(c00xghLq85mdkdKKzrPUMy9h6fc02kfq)
server.ssl.key-store=src/main/resources/leafyproject.p12
server.ssl.key-store-type=PKCS12
server.error.path=/error

//build.gradle:

    plugins {
    id 'org.springframework.boot' version '2.4.0'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

group = 'tree.leaf'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'com.github.ulisesbocchio:jasypt-spring-boot-starter:3.0.3'
    compile('org.springframework.boot:spring-boot-starter-mail')
    compile group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity5'
    compile('org.springframework.boot:spring-boot-starter-mail')
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'

    runtime 'com.h2database:h2'
    runtime 'org.mariadb.jdbc:mariadb-java-client:2.7.0'

}

test {
    useJUnitPlatform()
}


tasks.bootRun.configure {
    systemProperty("spring.profiles.active", "dev")
}

tasks.register("bootRunProd") {
    group = "application"
    description = "Runs the Spring Boot application with the prod profile"
    doFirst {
        tasks.bootRun.configure {
            systemProperty("spring.profiles.active", "prod")
        }
    }
    finalizedBy("bootRun")
}

我不知道该怎么做,因为我需要能够执行 .jar 文件。谢谢你的帮助!

标签: javaspringspring-bootgradlejar

解决方案


推荐阅读