方法一:@value
@RestController
@RequestMapping("/Hello")
public class HelloController {
@Value("${people.name}")
private String name;
@Value("${people.country}")
private String country;
@RequestMapping("/print")
public String printHello() {
return name + "是" + country + "人";
}
}
#application.properties
server.port=8081
server.context-path=/app
people.name=张飞
people.country=蜀国
中文乱码了,网上给出了各种办法,都不可行,我的解决方案是不要在主配置文件
application.properties
中配置中文的属性值,可以单独新建一个配置文件,然后使用@PropertySource(value = {}, encoding = "utf-8")
来引入新配置文件,这样就不会出现乱码了。
@RestController
@RequestMapping("/Hello")
@PropertySource(value = { "classpath:config.properties" }, encoding = "utf-8")
public class HelloController {
@Value("${people.name}")
private String name;
@Value("${people.country}")
private String country;
@RequestMapping("/print")
public String printHello() {
return name + "是" + country + "人";
}
}
#config.properties
people.name=张飞
people.country=蜀国
方法二:@ConfigurationProperties
使用@Value(${property})注释注入配置属性有时会很麻烦,特别是在处理多个属性或数据本质上是分层的情况下。Spring Boot提供了另一种处理属性的方法@ConfigurationProperties
,这种方法允许强类型bean控制和验证应用程序的配置,如下例所示:
@Component
@ConfigurationProperties(prefix = "people")
@PropertySource(value = { "classpath:config.properties" }, encoding = "utf-8")
public class PropertyConfig {
private String name;
private String countryName;
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(final String countryName) {
this.countryName = countryName;
}
}
#config.properties
people.name=张飞
people.countryName=蜀国
@Component
表明该配置类需要被spring容器加载。
@ConfigurationProperties(prefix = "people")
该注解表明这是一个属性类,prefix表示所绑定的属性的前缀。
@PropertySource(value = { "classpath:config.properties" }, encoding = "utf-8")
导入自定义得配置文件,用UTF-8编码。
注意,该属性类的每个属性必须有setter方法,不然属性会注入失败!!!!!
那么属性名name、country是否要和properties中的文件一致呢?
松配置
现在把config.properties
修改如下:
#config.properties
people.name=张飞
people.country_name=蜀国
重新启动服务,也成功了,说明用@ConfigurationProperties
标明的属性类中的属性名不必和properties文件中的属性名完全一样,这是因为@ConfigurationProperties
支持Relaxed Binding,下面配置都是可以的,具体可以看看springboot开发文档
Property | Note |
---|---|
acme.my-project.person.first-name | Kebab case, which is recommended for use in .properties and .yml files. |
acme.myProject.person.firstName | Standard camel case syntax. |
acme.my_project.person.first_name | Underscore notation, which is an alternative format for use in .properties and .yml files. |
ACME_MYPROJECT_PERSON_FIRSTNAME | Upper case format, which is recommended when using system environment variables. |
嵌套属性
@Component
@ConfigurationProperties(prefix = "people")
@PropertySource(value = { "classpath:config.properties" }, encoding = "utf-8")
public class PropertyConfig {
private String name;
private String countryName;
private Salary salary;
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(final String countryName) {
this.countryName = countryName;
}
public Salary getSalary() {
return salary;
}
public void setSalary(final Salary salary) {
this.salary = salary;
}
public static class Salary {
private int amount;
private String unit;
public int getAmount() {
return amount;
}
public void setAmount(final int amount) {
this.amount = amount;
}
public String getUnit() {
return unit;
}
public void setUnit(final String unit) {
this.unit = unit;
}
}
}
#config.properties
people.name=张飞
people.country_name=蜀国
people.salary.amount=2000
people.salary.unit=美元
同样,salary作为嵌套属性,也必须有setter方法。如果是下面情形,可以省略setter方法
@Component
@ConfigurationProperties(prefix = "people")
@PropertySource(value = { "classpath:config.properties" }, encoding = "utf-8")
public class PropertyConfig {
private String name;
private String countryName;
private Salary salary = new Salary();//这里直接初始化
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(final String countryName) {
this.countryName = countryName;
}
public Salary getSalary() {
return salary;
}
public static class Salary {
private int amount;
private String unit;
public int getAmount() {
return amount;
}
public void setAmount(final int amount) {
this.amount = amount;
}
public String getUnit() {
return unit;
}
public void setUnit(final String unit) {
this.unit = unit;
}
}
}