有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从cmd运行时java Spring引导负载测试数据库

我有一个spring引导项目,当从Eclipse启动时,它可以正确地用postgres加载,但是当通过windows命令窗口启动时,测试数据库被加载

当我用java -jar happy_list-0.1.0.jar从cmd启动应用程序时,我有一个错误(我删除了stacktrace的一部分):

Caused by: java.lang.IllegalStateException: Driver for test database type [HSQL] is not available in the classpath
Caused by: java.lang.ClassNotFoundException: org.hsqldb.jdbcDriver

我没有任何测试或测试配置。它一定是由Spring boot完成的一些自动配置,但我不明白为什么它在从eclipse或windows cmd运行时表现不同

PersistentContext:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = { "happy_listing" })
public class PersistentContext {

}

应用程序。爪哇:

@SpringBootApplication
@EnableAutoConfiguration
@Configuration
@ComponentScan(basePackages = { "happy_listing" })
@Import({ PersistentContext.class })
public class App {

    @Configuration
    @PropertySource("classpath:application.properties")
    static class ApplicationProperties {
    }

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

}

建造。格拉德尔:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.5.RELEASE")
        classpath('se.transmode.gradle:gradle-docker:1.2')
    }
}

group = 'marcandregirard'

apply plugin: 'java'
apply plugin: 'spring-boot'
apply plugin: 'docker'

jar {
    baseName = 'happy_list'
    version =  '0.1.0'
}

springBoot {
  mainClass = "happy_listing.App"
}
repositories {
    mavenCentral()
}

dependencies {
    compile 'org.slf4j:slf4j-api:1.7.13'
    compile 'javax.ws.rs:javax.ws.rs-api:2.0'
    compile 'org.springframework:spring-core'
    compile 'org.springframework:spring-context'
    compile 'org.springframework:spring-jdbc'
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile group: 'org.apache.commons', name: 'commons-dbcp2', version: '2.1.1'
    compile 'org.springframework.data:spring-data-jpa:1.9.2.RELEASE'
    compile 'org.hibernate:hibernate-entitymanager'
    compile 'org.postgresql:postgresql:9.4-1206-jdbc42'
    compile 'org.springframework:spring-web'
    testCompile 'junit:junit:4.12'
}

task buildDocker(type: Docker, dependsOn: build) {
  push = true
  applicationName = jar.baseName
  dockerfile = file('src/main/docker/Dockerfile')
  doFirst {
    copy {
      from jar
      into stageDir
    }
  }
}

我使用命令gradle build buildDocker创建了jar,该命令创建了要在Docker中运行的jar和映像

从Eclipse启动时,一切正常


共 (1) 个答案

  1. # 1 楼答案

    对于初学者,我会开始清理依赖项,而不是所有单独的jar文件使用适当的启动器

    dependencies {
        compile 'javax.ws.rs:javax.ws.rs-api:2.0'
        compile 'org.springframework.boot:spring-boot-starter-web'
        compile 'org.springframework.boot:spring-boot-starter-data-jpa'
        compile group: 'org.apache.commons', name: 'commons-dbcp2', version: '2.1.1'
        compile 'org.postgresql:postgresql:9.4-1206-jdbc42'
        testCompile 'org.springframework.boot:spring-boot-starter-test'
    }
    

    接下来,看起来您正在尝试进行大量手动配置@SpringBootApplication已经意味着其他3个,Spring Boot已经加载了application.properties。去掉那个

    @SpringBootApplication
    public class App {
    
        public static void main(String... args) {
            SpringApplication.run(App.class, args);
        }
    
    }
    

    删除PersistenceContext类,因为Spring Boot已经为您完成了这项工作

    清理完类和依赖项后,确保没有遗留旧类。为此,执行Gradleclean任务gradle clean应该可以

    因此,在删除类时,还要确保使用gradle clean build而不是普通的gradle build