背景
你是否在项目中遇到过磁盘路径、服务器的ip地址、秘钥字符串等属性写在代码中。当项目部署在服务器上发现路径需要修改,ip需要修改,秘钥更改,这时我们只能先修改好代码,然后在重新打包后发布。那么,为什么不把他们这类常量写在配置文件中呢。这样部署在服务器上后,我们仅需要告知运维人员如何修改配置文件即可。免去发布版本的问题,也可以将开发环境、生产环境、测试环境中的相关配置有效分开。
什么是自定义属性
在spring boot 中,我们通过读取application.properties中的属性来初始化项目。例如项目名称,端口号,数据库的地址等。我们将自己需要使用的属性写在一个拓展的配置文件application-config.properties中,这样方便我们区分系统配置或是自定义配置。
本教程使用如下版本
spring boot 2.7.0
创建项目
新建一个spring boot 项目。可以通过我之前的教程创建一个空项目。
编辑pom文件
在idea中新建一个maven项目后,首先需要在pom文件中引入如下依赖。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-properties-demo</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
不要忘记重新加载一下依赖,防止找不到jar包!依次点击maven,项目名称=》右键=》Reimport
目录结构
建立好如下图的包及文件目录结构,具体如下:
编写自定义属性文件
在application-config.properties文件中编写属性,这里我们有几个测试属性。
#文件路径
file_path=C:\\9\\0
#文件最大大小
file_max=1024
#上传地址
upload_url=http://localhost:8085/
#是否开启测试
file_test=0
编写测试类
在 MainStarter 类中编写读取配置文件的相关测试代码:
package com.study;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@SpringBootApplication
@RestController
@Configuration
@PropertySource(value = {
"classpath:config/application-config.properties","file:config/application-config.properties"}, ignoreResourceNotFound = true)
public class MainStarter {
@Value("${file_path}")
private String filePath;
@Value("${file_max}")
private Integer fileMax;
@Value("${upload_url}")
private String uploadUrl;
@Value("${file_test}")
private Integer fileTest;
@RequestMapping("getConf")
public Map<String, Object> getConf(){
Map<String, Object> map = new HashMap<String, Object>();
map.put("filePath", filePath);
map.put("fileMax", fileMax);
map.put("uploadUrl", uploadUrl);
map.put("fileTest", fileTest);
return map;
}
public static void main(String[] args) {
SpringApplication.run(MainStarter.class, args);
}
}
注意到上述文件中的@Configuration @PropertySource @Value注解 。Configuration是告诉spring boot这是一个配置类。@PropertySource是注解类,这里会优先读取相对文件的conf目录下的该文件,如果没有则使用classpath的conf目录下的该文件。@Value 会将我们自定义的属性和变量进行绑定。通过该变量即可读取到配置属性的值。getConf是我们的测试接口,用于读取配置内容。
测试
右键运行MainStarter类。观察控制台输出如下,观察项目正常启动:
打开浏览器,输入如下地址进行接口测试,选择一个查询数据的接口:
http://localhost:18080/getConf
结果如下
正常情况下只需要将相关的配置写在需要使用的类中加入@Configuration @PropertySource @Value 这三个注解,并配置好相关的变量即可。
在项目打包后,优先读取 jar包同一级的config下的application-config.properties。如果需要修改,只需要修改改文件即可。
打包后文件结构application-config.properties放入config文件夹内即可
写在最后
开源是一种美德,尽早加入开源社区,共建美好生态!