Amar Dev
Amar Dev

Reputation: 1479

spring boot specific profile yaml not loading

I have a yaml file as follows:-

spring:
  profiles: test
msg: test
---
spring:
  profiles : dev
msg: hello
---
spring:
  profiles : prod
msg: production

located in /src/main/java/resources as application.yaml

my code the read the values from this file is as follows

@Configuration
@EnableConfigurationProperties
public class TestConfig {

    private String msg;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

I create a bean of this class as following:-

@Bean
public TestConfig testConfig() {
    return new TestConfig();
}

But trying to see the profile specific information does not work with the following code:-

public class Main implements CommandLineRunner{

    @Autowired
    private TestConfig testConfig;

    public static void main(String[] args) {

        SpringApplication.run(Main.class, args);

    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("it works!!!");
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>   " + testConfig.getMsg());
    }
}

Any help here is much appreciated.

Thanks, Amar

Upvotes: 1

Views: 5374

Answers (2)

Amar Dev
Amar Dev

Reputation: 1479

I solved my issue, I had a custom bootrun in my build script, which was overriding my spring.profiles.active system property to some value. I made a few tweaks here and there and now its working fine. Thanks for everyone's time!!!!

Upvotes: 2

Thomas
Thomas

Reputation: 12009

Your yaml file should start with three dashes --- and be named ".yml". Make sure to position it in /src/main/resources/, your current location would place it under a resources directory in the output, which is not what you want.

Upvotes: 1

Related Questions