属性注入的两种方式@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;
}
}