简体   繁体   中英

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM