Springboot can't read values from properties file

I have the following problem: I've wanted to read some values (the price of products) from .properties file.

However I'm trying, It's always getting 0.00 as a value.

This is my service bean, which in I want to create a product list:

@Service
@ConfigurationProperties(prefix = "product-service")
public class ProductService {

    private double first;
    private double second;
    private double third;

    public double getFirst() {
        return first;
    }

    public void setFirst(double first) {
        this.first = first;
    }

    public double getSecond() {
        return second;
    }

    public void setSecond(double second) {
        this.second = second;
    }

    public double getThird() {
        return third;
    }

    public void setThird(double third) {
        this.third = third;
    }

    private List<ProductDTO> productList = Arrays.asList(
            new ProductDTO(1, "Tomb Raider", first),
            new ProductDTO(2, "10000 rp to lol", second),
            new ProductDTO(3, "2k valorant points", third)
    );

    public List<ProductDTO> findAllProducts() {
        return productList;
    }
}

And this is the property file:

product-service.first = 225.00
product-service.second = 320.50
product-service.third = 150.99

Considering that you use the prefix = "product-service"

you should declare your class fields as following.

    private double first;
    private double second;
    private double third;

You should also update getters and setters.

You also have another error in your code as well

private List<ProductDTO> productList = Arrays.asList(
            new ProductDTO(1,"Tomb Raider",first),
            new ProductDTO(2,"10000 rp to lol",second),
            new ProductDTO(3,"2k valorant points",third)
    );

This field is initialized when your class is initialized. But Spring initializes your bean with a proxy. So when the class is initialized your productList will have 0 values for first, second,third.

If you wish this to work you should replace

public List<ProductDTO> findAllProducts() {
        return productList;
    }

with

public List<ProductDTO> findAllProducts() {
        return Arrays.asList(
                new ProductDTO(1,"Tomb Raider",first),
                new ProductDTO(2,"10000 rp to lol",second),
                new ProductDTO(3,"2k valorant points",third)
        );
    }

Don't mix your configuration properties with the service layer.

You have a few ways:

  • create a separate configuration class:
@ConfigurationProperties("product-service")
public class ProductProperties {
    private Double first;
    private Double second;
    private Double third;
    // getters & setters
}

And use it directly at your service class:

class ProductService {
    private ProductProperties properties;

    // use at code: properties.getFirst()
}

depending on your Spring Boot version you may need @EnableConfigurationProperties({ProductProperties.class}) under any class marked with @Configuration

  • use @Value:
class ProductService {
    @Value("${product-service.first}")
    private double first;
    //...
}

You should set to your main class:

Useful links:

  • Guide to @ConfigurationProperties in Spring Boot
  • Quick Guide to Spring @Value