Search code examples
javaspringspring-bootspring-cloud-configspring-config

Spring Boot 2 use dev profile if prod profile is not exist


I have Spring Boot Application and i have 3 property files: applications.properties, applications-dev.properties, applicaton-prod.properties. In applications.properties i specify that spring.profiles.active=prod. But I want to allow startup of application without prod profile(applicaton-prod.properties). It means that spring must startup application in dev profile(applications-dev.properties) automatically. How can i implement this? May be some MissingOnProfile annotation exist?) My task is to create different application behaviour based on application.properties files. Also i use @Profile annotation in each bean that depends on particular profile. All task is to create WebInstaller, and in finish step i will create application-prod.properties and by using RestartEndpoint i will restart application context and required beans from application-prod.properties will injected. But i need to make startup withoud application-prod.properties, but if this file exist i will startup in prod profile.


Solution

  • You can do this:

    SpringApplication application = new SpringApplication(IdMatrixApplication.class);
    File file = new File("src/main/resources/dev/application-prod.properties");
    if (file.exists()) {
        application.setAdditionalProfiles("prod","dev");
    }
    application.run(args);