title: Spring Boot: error when activating profile in YAML file
body: <p>I can't activate a profile dynamically in a Spring Boot app. I defined profiles in <code>pom.xml</code> as follows:</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;profiles&gt;
          &lt;profile&gt;
              &lt;id&gt;dev&lt;/id&gt;
              &lt;properties&gt;
                  &lt;activatedProperties&gt;dev&lt;/activatedProperties&gt;
              &lt;/properties&gt;
              &lt;activation&gt;
                  &lt;activeByDefault&gt;true&lt;/activeByDefault&gt;
              &lt;/activation&gt;
          &lt;/profile&gt;
          &lt;profile&gt;
              &lt;id&gt;prod&lt;/id&gt;
              &lt;properties&gt;
                  &lt;activatedProperties&gt;prod&lt;/activatedProperties&gt;
              &lt;/properties&gt;
          &lt;/profile&gt;
      &lt;/profiles&gt;
  </code></pre>
  <p>Then I created 2 files in <code>src/main/resources</code>:</p>
  <pre><code># application.yml
  
  spring:
    application:
      name: myapp-service
    profiles:
      active: @activatedProperties@
  server:
    port: 8200
  </code></pre>
  <p>Here is <code>application-dev.yml</code>:</p>
  <pre><code>spring:
    kafka:
      producer:
        bootstrap-servers: localhost:9092
        key-serializer: org.apache.kafka.common.serialization.StringSerializer
        value-serializer: org.apache.kafka.common.serialization.StringSerializer
  redis:
    host: localhost
    port: 6379
  </code></pre>
  <p>When starting the app from the IDE, it fails to read <code>active: @activatedProperties@</code> values rating the error about <code>@</code> symbol:</p>
  <pre><code>java.lang.IllegalStateException: Failed to load property source from target/classes/application.yml'
  ...
  Caused by: org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token
  found character '@' that cannot start any token. (Do not use @ for indentation)
   in 'reader', line 5, column 13:
          active: @activatedProperties@
                  ^
  </code></pre>
  <p>I found some examples of the same use but in <code>application.properties</code> file.
  Isn'it possible to do the same in a <code>yml</code> file ?</p>
  <p>I tried to replace it with <code>$</code> sign as follows:</p>
  <pre><code>...
    profiles:
      active: ${activatedProperties}
  </code></pre>
  <p>This time it is Spring Boot that raised the error about failing to load <code>redis.host</code> value:</p>
  <pre><code>Factory method 'redisConnectionFactory' threw exception; nested exception is java.lang.IllegalArgumentException: Host name must not be null or empty!
  </code></pre>
  <p>as if the profile is not recognized.</p>
  <p>Here is the configuration file:</p>
  <pre class="lang-java prettyprint-override"><code>@Configuration
  @EnableConfigurationProperties
  @ConfigurationProperties
  public class AppConfiguration {
      private String redisHost;
      private int redisPort;
      private String redisPassword;
  ... //getters and getters come here
  </code></pre>
  <p>Here is the Spring Boot application class:</p>
  <pre class="lang-java prettyprint-override"><code>@SpringBootApplication
  public class LiveDataServiceApplication {
  
      @Autowired
      private AppConfiguration configuration;
  
      public static void main(String[] args) {
          SpringApplication.run(LiveDataServiceApplication.class, args);
      }
  
      @Bean
      public RedisConnectionFactory redisConnectionFactory() {
          RedisStandaloneConfiguration redisConfiguration = new RedisStandaloneConfiguration(configuration.getRedisHost(), configuration.getRedisPort());
          redisConfiguration.setPassword(configuration.getRedisPassword());
          return new LettuceConnectionFactory(redisConfiguration);
      }
  </code></pre>
  <p>What am I missing?</p>
  <p><strong>UPDATE</strong></p>
  <p>I annotated every attribute in <code>AppConfiguration</code> class with `@Value():</p>
  <pre class="lang-java prettyprint-override"><code>public class AppConfiguration {
  
      @Value(&quot;${redis.host}&quot;)
      private String redisHost;
  
      @Value(&quot;${redis.port}&quot;)
      private int redisPort;
  
      @Value(&quot;${redis.password:}&quot;)
      private String redisPassword;
  ...
  </code></pre>
  <p>and moved the values to <code>application.yml</code>and it worked!
  If I keep the vaues in <code>application-dev.yml</code> file, it fails to load them.
  Why so? It seems like the default profile <code>dev</code> is not set.</p>
  <p><strong>UPDATE-2</strong></p>
  <p>Finally, it worked if I use a single YAML file <code>application.yml</code> including available environments as follows:</p>
  <pre><code>spring:
    application:
      name: live-data-service
    profiles:
      active: ${activatedProperties}
    kafka:
      producer:
        bootstrap-servers: localhost:9092
        key-serializer: org.apache.kafka.common.serialization.StringSerializer
        value-serializer: org.apache.kafka.common.serialization.StringSerializer
  server:
    port: 8200
  redis:
    host: localhost
    port: 6379
  ---
  spring:
    profiles: prod
    kafka:
      producer:
        bootstrap-servers: prod-kafka.server:9092
        key-serializer: org.apache.kafka.common.serialization.StringSerializer
        value-serializer: org.apache.kafka.common.serialization.StringSerializer
  redis:
    host: prod.server.com
    port: 6379
    password: prod-server-pwd
  </code></pre>
  <p>at least locally? I have no idea why id does not when using separate YAML files (<code>application-dev.yml</code>, <code>application-prod.yml</code>, etc.
  Offcial <a href="https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-change-configuration-depending-on-the-environment" rel="nofollow noreferrer">Spring Boot docs</a> have no more details about that.</p>