一个application.properties文件配置如下:
app.x1.age = 3
app.x1.ht = 4
@Component
@ConfigurationProperties(prefix = "app.x1")
public class X1Properties
{
private int age;
private int ht;
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public int getHt()
{
return ht;
}
public void setHt(int ht)
{
this.ht = ht;
}
}它会将app.x1后缀的属性值附到对应的set方法上去。
并且它配置了一个X1Properties bean。
也可以在配置类中使用以下代码,同时去掉X1Properties上的@Component注解。
@Configuration
@EnableConfigurationProperties(value = X1Properties.class) // 自动配置bean
public class DbConfig
{
}这同样会创建X1Properties bean。