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.wbnull.springbootdemo.config.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 'configbeanvalue': 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.wbnull.springbootdemo.config.configbeanvalue;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.core.env.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=" + environment.getproperty("demo.sex") +
" , address=" + environment.getproperty("demo.address");
}
}
运行,发现中文乱码:

这里,我们在application.properties做如下配置:
server.tomcat.uri-encoding=utf-8 spring.http.encoding.charset=utf-8 spring.http.encoding.enabled=true spring.http.encoding.force=true spring.messages.encoding=utf-8
然后修改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.springframework.boot.context.properties.configurationproperties;
import org.springframework.context.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,然后再进行取值,示例如下:
import cn.wbnull.springbootdemo.config.configbeanprop;
import cn.wbnull.springbootdemo.config.configbeanvalue;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.core.env.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;
@autowired
private configbeanprop configbeanprop;
@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=" + environment.getproperty("demo.sex") +
" , address=" + environment.getproperty("demo.address") +
"<p>get properties value by ''@configurationproperties'' :" +
//3、使用@configurationproperties注解读取
" phone=" + configbeanprop.getphone() +
" , wife=" + configbeanprop.getwife();
}
}
运行结果如下:

到此这篇关于springboot读取properties配置文件中的数据的三种方法的文章就介绍到这了,更多相关springboot properties读取配置文件内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论