Spring Boot 属性注入的两种方式

版权声明: https://blog.csdn.net/u011263794/article/details/84944070

属性注入的两种方式@Value & @ConfigurationProperties

application.yml
spring:
  profiles:
    active: dev
application-dev.yml
book:
  name: test
  price: 15.00
  writer: gary
public class Book
{
    @Value("${book.name}")
    private String name;
    @Value("${book.price}")
    private double price;
    @Value("${book.writer}")
    private String writer;
}
@ConfigurationProperties(prefix = "book")
public class Book
{
    private String name;
    private double price;
    private String writer;
    
    public String getName()
    {
        return name;
    }

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

    public double getPrice()
    {
        return price;
    }

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

    public String getWriter()
    {
        return writer;
    }

    public void setWriter(String writer)
    {
        this.writer = writer;
    }
}

 

展开阅读全文

没有更多推荐了,返回首页