目录

Spring Boot 读取配置文件方法

1. 使用@Value方式读取,示例如下

application.properties 中加入如下内容

id:: 635f50fb-51f7-4760-af1a-4e93fa368d63

1
test=123

然后在类中可以使用 @Value 注解取值

1
2
@Value("${test}")
private String test;

2. 使用Environment方式取值

同样,以读取方式一中的配置文件内容为例

1
2
3
4
5
6
7
@Autowired
private Environment env;

@RequestMapping("/test")
public String test(){
    return env.getProperty("test");
}

3. 自定义配置文件映射

application.properties 文件加入内容如下。

1
2
test.name=zhangsan
test.age=1

新建实体 Test.java,内容如下。

1
2
3
4
5
6
7
8
9
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Data
@ConfigurationProperties(prefix = "test")
public class Test {
    private String name;
    private int age;
}

可以使用 @PropertySource("classpath:my2.properties") 指定读取的配置文件。