vendredi 6 mars 2015

Spring Boot overriding YML profile from Command Line

I wan't to override an existing YML file profile using command line, so I did this.



  1. Created a folder and added to the classpath

  2. Copied another application.yml in that new folder

  3. Ran this command mvn spring-boot:run -Dspring.profiles.active=unit-test


but it still picking up the "default" active profile from the source code application.yml. I also tried creating a application.properties instead of application.yml but it still didn't get picked up?



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class SuchApplication implements CommandLineRunner {

@Autowired
private DogeService dogeService;

@Override
public void run(String... args) {
System.out.println("AutoConfiguration should have wired up our stuff");
System.out.println("Let's see if we are doge-worthy...");
if (dogeService.requiresDogeness()) {
System.out.println(dogeService.leDoge());
} else {
System.out.println("No Doge for us :(");
}
}

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


I have the following YML file under my resources folder



spring:
profiles.active: default
---
spring:
profiles: default

doge:
wow: 10
such: so
very: true

---
spring:
profiles: unit-test
doge:
wow: 4
such: so
very: true

Aucun commentaire:

Enregistrer un commentaire