Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 316
Old Engineer Avatar asked Dec 07 '25 02:12

Old Engineer


1 Answers

You have the following code

@SpringBootApplication
public class Demo1Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo1Application.class, args);
        Products products = new Products();  <---------------------------------ERROR!!!
        List<String> productsFromApplicationYml = products.getProducts();

        System.out.println(productsFromApplicationYml.size()); // I would like to see 2
        products.getProducts().forEach(System.out::println); // I would like to see "first" and "second"
    }



@Component
@ConfigurationProperties(prefix="products")
public class Products {
    private final List<String> products = new ArrayList<>();

    public List<String> getProducts() {
        return products;
    }
}

The error is in the line Products products = new Products(); of your main method. You don't retrieve the bean from Spring context but you create it by yourself in the JVM. Therefore it is as you created it empty.

Read more to understand how Spring uses proxies for your spring beans and not the actual classes that you write.

What you need is the following

public static void main(String[] args) {
  ApplicationContext app = SpringApplication.run(Demo1Application.class, args)

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

  List<String> productsFromApplicationYml = products.getProducts();
  System.out.println(productsFromApplicationYml.size())

Edit:

You have also false configured your application.yml file.

products:
  - first
  - second

The symbol - is used for array of complex objects which spring will try to serialize from application.yml. Check what I mean in this SO thread

Considering that you don't have a list of custom objects but a primitive List<String> your application.yml should be in the following form

products: first,second
like image 149
Panagiotis Bougioukos Avatar answered Dec 08 '25 16:12

Panagiotis Bougioukos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!