Search code examples
spring-bootconfigurationjava-17

Yaml properties not loaded in springboot


I would like to use application.yml instead of application.properties

I followed: https://docs.spring.io/spring-boot/docs/2.1.13.RELEASE/reference/html/boot-features-external-config.html

I am using:

  • SpringBoot 2.6.2
  • Java 17
  • Gradle 7.3.2

My MCVE: https://github.com/OldEngineer1911/demo1

The issue is: Properties are not loaded. Can someone please help?


Solution

  • The solution from "@Panagiotis Bougiokos" is partially correct:

    Products products = app.getBean(Products.class); <---Retrieve the Proxy instance from Spring Context
    

    Is necessary, but the rest is optional (it is possible to write List in yml both ways). The solution was to fix yml file with nested products:

    products:
      products:
        - 'first'
        - 'second'
    

    And also add setter for products:

    @Component
    @ConfigurationProperties(prefix="products")
    public class Products {
        private List<String> products;
    
        public List<String> getProducts() {
            return products;
        }
    
        public void setProducts(List<String> products) {
            this.products = products;
        }
    }
    

    This is a working solution (everyone can check Github mentioned in the question). Anyway, it still can be improved - don't know why do I need nested products.