SpringBoot入门:读取properties配置文件中的数据
SpringBoot⼊门:读取properties配置⽂件中的数据
Spring Boot最常⽤的3种读取properties配置⽂件中数据的⽅法:
1、使⽤@Value注解读取
读取properties配置⽂件时,默认读取的是application.properties。
application.properties:
demo.name=Name
demo.age=18
Java代码:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GatewayController {
@Value("${demo.name}")
private String name;
@Value("${demo.age}")
季氏将伐颛臾private String age;
@RequestMapping(value = "/gateway")
public String gateway() {
return "get properties value by ''@Value'' :" +
//1、使⽤@Value注解读取
" name=" + name +欠条的有效期是多少年
卷发棒用法
" , age=" + age;
}
}
运⾏结果如下:
这⾥,如果要把
@Value("${demo.name}")
private String name;
@Value("${demo.age}")
private String age;
部分放到⼀个单独的类A中进⾏读取,然后在类B中调⽤,则要把类A增加@Component注解,并在类B中使⽤@Autowired⾃动装配类A,代码如下。
类A:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ConfigBeanValue {
@Value("${demo.name}")
public String name;
@Value("${demo.age}")
public String age;
}
类B:
import cn.fig.ConfigBeanValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GatewayController {
汽车贷款利率@Autowired
private ConfigBeanValue configBeanValue;
@RequestMapping(value = "/gateway")
public String gateway() {
return "get properties value by ''@Value'' :" +
//1、使⽤@Value注解读取
" name=" + configBeanValue.name +
" , age=" + configBeanValue.age;
}
}
运⾏结果如下:
注意:如果@Value${}所包含的键名在application.properties配置⽂件中不存在的话,会抛出异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configBe
anValue': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder
'demo.name' in value "${demo.name}"
2、使⽤Environment读取
application.properties:
demo.sex=男
demo.address=⼭东
Java代码:
import cn.fig.ConfigBeanValue;
import org.springframework.beans.factory.annotation.Autowired;
import nv.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GatewayController {
@Autowired
private ConfigBeanValue configBeanValue;
@Autowired
private Environment environment;毕淑敏的作品
@RequestMapping(value = "/gateway")
public String gateway() {
return "get properties value by ''@Value'' :" +
//1、使⽤@Value注解读取
" name=" + configBeanValue.name +
" , age=" + configBeanValue.age +
"<p>get properties value by ''Environment'' :" +
//2、使⽤Environment读取
" , sex=" + Property("demo.sex") +
" , address=" + Property("demo.address");
}
}
运⾏,发现中⽂乱码:
这⾥,我们在application.properties做如下配置:
ding.charset=UTF-8
abled=true
ding.force=true
然后修改IntelliJ IDEA,File --> Settings --> Editor --> File Encodings ,将最下⽅Default encoding for properties files设置为UTF-8,并勾选Transparent native-to-ascii conversion。
重新运⾏结果如下:
3、使⽤@ConfigurationProperties注解读取
在实际项⽬中,当项⽬需要注⼊的变量值很多时,上述所述的两种⽅法⼯作量会变得⽐较⼤,这时候我们通常使⽤基于类型安全的配置⽅式,将properties属性和⼀个Bean关联在⼀起,即使⽤注解@ConfigurationProperties读取配置⽂件数据。
在src\main\resources下新建config.properties配置⽂件:
demo.phone=10086
demo.wife=self
珍惜时间的格言警句创建ConfigBeanProp并注⼊config.properties中的值:
import org.t.properties.ConfigurationProperties;
import t.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "demo")
@PropertySource(value = "config.properties")
public class ConfigBeanProp {
private String phone;
private String wife;
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
}
@Component 表⽰将该类标识为Bean
@ConfigurationProperties(prefix = "demo")⽤于绑定属性,其中prefix表⽰所绑定的属性的前缀。@PropertySource(value = "config.properties")表⽰配置⽂件路径。
使⽤时,先使⽤@Autowired⾃动装载ConfigBeanProp,然后再进⾏取值,⽰例如下:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。