springboot used @ConfigurationProperties values from the configuration file

We wrote a book of some properties in the application.properties

book.name="活着"
book.price=100
book.size="大"

 

Then, we create a new class, book

package com.lyb.demo.model;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Email;

@ConfigurationProperties(prefix = "book")
@Component
public class book {
    private String name;
    private int price;
    private String size;

    @Override
    public String toString() {
        return "book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", size='" + size + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }
}

 

You can see, we use the book to spring @Component management, use @ConfigurationProperties (prefix = "book") that maps the book value of the configuration file to the book category,

We write test classes

To see whether the value taken from the configuration file and successfully mapped to the class

 @Autowired
private book book1;
@Test
    public void testConfigrationProperties(){
        System.out.print(book1.toString());
    }

Run you can see the output value of the configuration file

Guess you like

Origin blog.csdn.net/abc_123456___/article/details/91042191