SpringBoot读取.yml/.properties配置文件
2021-05-06 19:28
标签:rop efi imp env system config core 文件内容 red .properties文件内容如下: .yml文件内容如下: 读取类代码如下: 属性名要和配置文件名一致,且要有Getter and Setter,读取类代码如下: 读取类代码如下: SpringBoot读取.yml/.properties配置文件 标签:rop efi imp env system config core 文件内容 red 原文地址:https://www.cnblogs.com/antguo/p/13187574.htmltest.name=test
test.msg=123456
spring:
test:
name: test
msg: 123456
一、@Configuration+@PropertySource+@Value读取.properties
@Configuration
@PropertySource("classpath:test.properties")
public class Test {
@Value("${test.name}")
private String name;
@Value("${test.msg}")
private String msg;
}
二、@Component+@ConfigurationProperties读取.yml
@Component
@ConfigurationProperties(prefix = "spring.test")
public class Test {
private String name;
private String msg;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
三、@Component+Environment读取.yml
import org.springframework.core.env.Environment;
@Component
public class Test {
@Autowired
private Environment environment;
@Bean
public JedisCluster getJedisCluster() {
System.out.println("name:" + environment.getProperty("spring.test.name"));
...
}
}
文章标题:SpringBoot读取.yml/.properties配置文件
文章链接:http://soscw.com/index.php/essay/83347.html