【问题标题】:Spring boot - Start application with inmemory databaseSpring boot - 使用内存数据库启动应用程序
【发布时间】:2020-11-30 02:20:40
【问题描述】:

我有一个简单的 Spring Boot 应用程序。 我将它连接到 Postgres 数据库。 URL 和登录信息在 application.properties 文件中。

这意味着当我在本地启动应用程序进行测试时,我还需要启动并运行数据库。否则它会在启动时失败。

有没有办法:

  1. 从测试启动应用程序,并使用 application-test.properties 设置 URL 并登录到内存数据库?
  2. 或者当我在本地启动应用程序时,它会将 URL 和登录信息更改为我的内存数据库。无需从测试中启动应用程序。

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    我会选择您的选项 1:创建一个测试属性配置 test/resource/application-test.properties 并使用 @ActiveProfiles("test") 注释测试类

    编辑 如果您只想使用不同的数据源运行应用程序,那么 Spring Profiles 是您的最佳选择。

    您在 @Configuration 类中声明您的数据库配置 bean,并带有 @Profile 注释:

    @Configuration
    @Profile("mem")
    public class DataSourceConfig {
        
        @Bean
        public DataSource getDataSource() {
            DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
            dataSourceBuilder.driverClassName("org.h2.Driver");
            dataSourceBuilder.url("jdbc:h2:mem:test");
            dataSourceBuilder.username("SA");
            dataSourceBuilder.password("");
            return dataSourceBuilder.build();
        }
    }
    

    或者,您可以使用您的数据库配置创建一个名为 application-mem.properties 的配置文件。

    最后,您只需使用如下配置文件运行您的应用:

    java -jar app.jar --spring.profiles.active=mem
    

    【讨论】:

    • 如何编写测试?调用main方法写累了,但它只会启动应用程序并立即退出
    • @Kims 这就是 JUnit 背后的理念——它执行任务然后退出。与所有事情一样 - 先想好你想做什么(在这种情况下:测试),然后再去做:)
    • 只想正常启动应用,但是内存数据库
    • 那么为什么不能在application.properties 中指定呢?你又问什么? :)
    • 让我试着用另一种方式来解释它。有一个很大的机会,我正在以错误的方式思考这个问题。我有我的主要方法。这将启动我的应用程序。它将转到我的 applications.properties 文件,并获取有关我的数据库的信息。但是当我只想在本地测试我的应用程序时,有时我不想启动我的数据库。有时我只是希望它快一点,并指向一个内存数据库。例如,也许我想通过 Postman 快速发送请求。我怎么能做到这一点,而不改变我的 applications.properties 我想我的问题是
    【解决方案2】:

    嗯,您的用例很常见,通常在本地运行时首选使用内存。

    激活它所需的东西

    1.在pom.xml中添加以下依赖

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.200</version>
        <scope>runtime</scope>
    </dependency>
    

    您可以从here获取其他H2版本

    2.可以有一个本地属性文件application-local.properties会跟随内容

    spring.datasource.url=jdbc:h2:mem:testdb
    spring.datasource.driverClassName=org.h2.Driver
    spring.datasource.username=sa
    spring.datasource.password=password
    spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
    

    3.运行时激活本地配置文件

    mvn spring-boot:run -Dspring.profiles.active=local
    

    您可以在application.properties 中指定spring.profiles.active 属性

    spring.profiles.active=local
    

    【讨论】:

      【解决方案3】:

      以下要添加到 pom.xml 的依赖项。

       <dependency>
               <groupId>org.hsqldb</groupId>
               <artifactId>hsqldb</artifactId>
               <version>2.4.0</version>
               <scope>runtime</scope>
           </dependency>
      

      接下来,在 application.properties 文件中将测试设置为活动配置文件。

      #Profile configuration
      spring.profiles.active=test
      

      接下来,编写 application-test.properties 如下:

      #Database configuration
      spring.datasource.url=jdbc:hsqldb:mem:scratchdb/table_name  
      spring.datasource.username=sa
      spring.datasource.password=
      spring.jpa.show-sql = true
      

      application.yml

      spring:
        profiles:
          active: test
      
      server:
        servlet.context-path: /
        port: 4343
      
      ---  
      spring:      
        profiles: dev
        datasource:    
          url: jdbc:mysql://localhost:3306/db_name
          datasource:
          username: root
          password: root 
        jpa:
          show-sql: true
         
      ---
      spring:      
        profiles: test
        datasource:    
          url: jdbc:hsqldb:mem:scratchdb/db_name
          username: sa
          password:      
        jpa:
          show-sql: true
      

      【讨论】:

      • 它仍在使用 application.properties 中的属性,而不是 application-test.properties 中的属性
      • 您只想使用单个文件中的属性吗?然后,您可以选择 yml 格式,您可以在单个文件“application.yml”中指定所有属性。
      猜你喜欢
      • 1970-01-01
      • 2019-02-22
      • 1970-01-01
      • 2020-12-07
      • 1970-01-01
      • 2017-03-31
      • 2015-02-21
      • 2017-09-01
      • 1970-01-01
      相关资源
      最近更新 更多