简体   繁体   English

Spring boot - 使用内存数据库启动应用程序

[英]Spring boot - Start application with inmemory database

I have a simple Spring boot application.我有一个简单的 Spring 启动应用程序。 I have it connected to a Postgres database.我将它连接到 Postgres 数据库。 The URL and login information is in the application.properties file. URL 和登录信息位于 application.properties 文件中。

This meens that when I am starting the application locally for testing, I need to also have the database up and running.这意味着当我在本地启动应用程序进行测试时,我还需要启动并运行数据库。 Or else it will fail on startup.否则它将在启动时失败。

Is there a way to either:有没有办法:

  1. Start the application from a test, and use application-test.properties to set the URL and login to a in-memoty database?从测试启动应用程序,并使用 application-test.properties 设置 URL 并登录到内存数据库?
  2. Or maybe when I start the application locally, that it will change the URL and login information to my in-memory database.或者当我在本地启动应用程序时,它会将 URL 和登录信息更改到我的内存数据库。 Skipping the need to start the application from a test.跳过从测试启动应用程序的需要。

I'd go with your option 1: create a test properties config test/resource/application-test.properties and annotate the test class with @ActiveProfiles("test")我会选择 go 与您的选项 1:创建测试属性配置test/resource/application-test.properties并使用@ActiveProfiles("test")注释测试 class

edit If you want to just run the app w/ a different datasource, Spring Profiles is the way to go.编辑如果您只想使用不同的数据源运行应用程序,Spring Profiles 是 go 的方式。

You declare your db config bean in a @Configuration class, with the @Profile annotation:您在@Configuration class 中声明您的数据库配置 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();
    }
}

Alternatively, you can just create a profiled properties file named application-mem.properties with your db configuration there.或者,您可以使用您的数据库配置创建一个名为application-mem.properties的配置文件。

Finally, you just run your app with a profile like so:最后,您只需使用如下配置文件运行您的应用程序:

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

The below dependency to be added to the pom.xml.要添加到 pom.xml 的以下依赖项。

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

Next, in the application.properties file set the test as active profile.接下来,在 application.properties 文件中将测试设置为活动配置文件。

#Profile configuration
spring.profiles.active=test

Next, write the application-test.properties as below:接下来,编写 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应用程序.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

Well, your use case is very common and generally using In-memory is preferred when running locally.好吧,您的用例很常见,通常在本地运行时首选使用内存。

Things required to activate it激活它所需的东西

1.Add below dependency in pom.xml 1.在pom.xml中添加以下依赖

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

you can get other H2 versions from here你可以从这里获得其他 H2 版本

2.Can have a local property file application-local.properties will following content 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.Activate local profile while running 3.运行时激活本地配置文件

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

OR或者

You can specify spring.profiles.active property in application.properties您可以在application.properties中指定spring.profiles.active属性

spring.profiles.active=local

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

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