SpringBoot读取配置文件几种方式
一、引入外部配置文件方式:
①@PropertySource(value={"classpath:application1.properties"})
② @ImportResource(locations={"classpath:spring.xml"}
③默认引入application.properties和application1.yaml
注意:
①默认读取配置文件为application.yaml和application.properties,没有这两个文件默认读取不到数据
②yaml文件和properties文件有相同属性存在时,properties文件中的属性优先生效
二、读取yaml文件
(1)使用@Value(“${属性名}”) 注解
①application.yaml配置文件↓
spring:
name: yaml.name1
value: yaml.value1
②YmalTest1实体类↓
@Component
public class YmalTest1 {
@Value("${spring.name}")
private String name;
@Value("${spring.value}")
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
③Controller处理↓
@RestController
@RequestMapping("/yaml")
public class TestYamlController1 {
@Autowired
private YmalTest1 ymalTest1;
@RequestMapping(path = "/test1", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public YmalTest1 hello() {
return ymalTest1;
}
}
(2)使用@ConfigurationProperties(prefix = "属性前缀")注解
@Component
@ConfigurationProperties(prefix = "spring")
public class YmalTest1 {
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
Controller处理和application.yaml配置文件同(1)
(3)使用@Configuration和@Bean方式(推荐)
①application.yaml配置文件
spring:
name: yaml.name1
value: yaml.value1
②实体类
public class YmalTest2 {
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
③@Configuration配置
@Configuration
public class YmalTest2Config {
@Bean
@ConfigurationProperties(prefix = "spring")
public YmalTest2 getYmalTest2() {
return new YmalTest2();
}
}
④Controller处理
@RestController
@RequestMapping("/yaml")
public class TestYamlController2 {
@Autowired
private YmalTest2 ymalTest2;
@RequestMapping(path = "/test2", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public YmalTest2 hello() {
return ymalTest2;
}
}
三、读取.properties文件
(1)使用@Value(“${属性名}”) 注解
①application.properties配置文件↓
properties.name=properties.testname1
properties.value=properties.testvalue1
②PropertiesTest1实体类↓
@Component
public class PropertiesTest1 {
@Value("${properties.name}")
private String name;
@Value("${properties.value}")
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
③Controller处理↓
@RestController
@RequestMapping("/properties")
public class TestPropertiesController1 {
@Autowired
private PropertiesTest1 propertiesTest1;
@RequestMapping(path = "/test1", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public PropertiesTest1 hello() {
return propertiesTest1;
}
}
(2)使用@ConfigurationProperties(prefix = "属性前缀")注解
@Component
@ConfigurationProperties(prefix = "properties")
public class PropertiesTest1 {
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
Controller处理和application.properties配置文件同①
(3)读取非默认properties配置文件
使用@PropertySource(value = {"classpath:application2.properties"})注解可以读取非默认文件(application.properties)中的数据,但是要注意:当application2.properties配置文件中的属性和默认文件冲突时,默认文件中的属性优先起作用,而且@PropertySource 注解默认是只支持 properties 格式配置文件的读取的。
①application2.properties配置文件↓
properties.name='properties.testname2'
properties.value='properties.testvalue2'
②PropertiesTest1实体类文件↓
@Component
@ConfigurationProperties(prefix = "properties")
@PropertySource(value = {"classpath:application2.properties"})
public class PropertiesTest1 {
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
③Controller处理↓
@RestController
@RequestMapping("/properties")
public class TestPropertiesController1 {
@Autowired
private PropertiesTest1 propertiesTest1;
@RequestMapping(path = "/test1", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public PropertiesTest1 hello() {
return propertiesTest1;
}
}
(4)使用@Configuration和@Bean方式(推荐)
①application.properties配置文件↓
properties.name=properties.testname1
properties.value=properties.testvalue1
②PropertiesTest2实体类文件↓
public class PropertiesTest2 {
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
③@Configuration配置
@Configuration
public class PropertiesTest2Config {
@Bean
@ConfigurationProperties(prefix="properties")
public PropertiesTest2 getPropertiesTest2() {
return new PropertiesTest2();
}
}
④Controller处理↓
@RestController
@RequestMapping("/properties")
public class TestPropertiesController2 {
@Autowired
private PropertiesTest2 propertiesTest2;
@RequestMapping(path = "/test2", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public PropertiesTest2 hello() {
return propertiesTest2;
}
}
四、读取其他配置文件方式
在类上面使用@ImportResource(locations={"classpath:spring-common.xml"})引入外部文件
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了