简体   繁体   中英

Spring Default Profile Selection Based on Scope

My application has an embedded database as a "test" dependency (Maven test), but this causes an exception when you want to start the application without any -D switches because the jdbc driver is declared in the application.yml (see below).

Is it possible to set the datasource based on the scope (ie runtime or test)? Or better yet can you change the default profile without any -D switches (eg -Dspring.profiles.active=test)?

This might seem trivial, but it's confusing for our team that you can successfully run tests in your IDE, but then when you try to "run the application" it fails unless you set the -D switch.

datasource:
  url: jdbc:h2

spring:
  profiles: profile1

datasource:
  url: jdbc:postgresql

If you use the embedded database only for tests, then you can place an application.properties (or .yml) in the folder src/test/resources/ where you put all properties for test running in. At least that works for me. So you don't need to put your production properties into a profile and they are used when you run your application.

  1. Move application.yml in src/test/resources/

  2. You can set the default profile with


    datasource.url=jdbc:h2  =>

    datasource:
      url: jdbc:h2  
    ---

    spring:
      profiles: profile1

    datasource:
      url: jdbc:postgresql

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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