SpringBoot读取自定义的Properties
程序员文章站
2024-03-14 15:13:04
...
该配置文件, 会在jar包部署目录所在路径
目前兼容两种环境下 1. jar包部署情况下 2. 开发环境下
SpringBoot本身的配置文件
确定是开发环境还是jar包环境, 读取不一样的绝对路径位置
# 公共配置文件路径 csmp
common-config:
active: dev # dev代表开发环境 prod代表jar部署环境
relativepath: csmp/initSystem.properties # 公共配置文件相对路径
Properties配置文件
# 外部管理网络名称
network.drainage.externalManagementNet=ProviderManagementNet
# 内部管理网络名称
network.drainage.innerManagementNet=ManagementNetwork
# 外部业务入口
network.drainage.externalBusinessIn=ProviderNetIn
# 外部业务出口
network.drainage.externalBusinessOut=ProviderNetOut
# 虚拟防火墙路由实例名称
network.drainage.fwRoute.name=CSSMP_fw_route
# 虚拟防火墙路由实例外部管理网
network.drainage.fwRoute.externalManagementNet=10.121.10.87
# 虚拟防火墙路由实例内部管理网
network.drainage.fwRoute.innerManagementNet=172.172.0.11
# 虚拟防火墙路由实例外部业务入口网
network.drainage.fwRoute.externalBusinessIn=20.1.1.10
# 虚拟防火墙路由实例外部业务出口网
network.drainage.fwRoute.externalBusinessOut=30.1.1.10
实体类(跟配置文件一一对应)
@Data
public class NetworkProperties {
// 外部管理网络名称
private String externalManagementNet;
// 内部管理网络名称
private String innerManagementNet;
// 外部业务入口
private String externalBusinessIn;
// 外部业务出口
private String externalBusinessOut;
// 虚拟防火墙路由实例名称
private String fwRouteName;
// 虚拟防火墙路由实例外部管理网
private String fwRouteExternalManagementNet;
// 虚拟防火墙路由实例内部管理网
private String fwRouteInnerManagementNet;
// 虚拟防火墙路由实例外部业务入口网
private String fwRouteExternalBusinessIn;
// 虚拟防火墙路由实例外部业务出口网
private String fwRouteExternalBusinessOut;
public static NetworkProperties getNetworkProperties(Properties properties) {
if (null == properties) return null;
NetworkProperties network = new NetworkProperties();
network.setExternalManagementNet(properties.getProperty("network.drainage.externalManagementNet"));
network.setInnerManagementNet(properties.getProperty("network.drainage.innerManagementNet"));
network.setExternalBusinessIn(properties.getProperty("network.drainage.externalBusinessIn"));
network.setExternalBusinessOut(properties.getProperty("network.drainage.externalBusinessOut"));
network.setFwRouteName(properties.getProperty("network.drainage.fwRoute.name"));
network.setFwRouteExternalManagementNet(properties.getProperty("network.drainage.fwRoute.externalManagementNet"));
network.setFwRouteInnerManagementNet(properties.getProperty("network.drainage.fwRoute.innerManagementNet"));
network.setFwRouteExternalBusinessIn(properties.getProperty("network.drainage.fwRoute.externalBusinessIn"));
network.setFwRouteExternalBusinessOut(properties.getProperty("network.drainage.fwRoute.externalBusinessOut"));
return network;
}
}
编写SpringBoot组件, 由该组件进行获取配置文件, 交给工厂管理
@Component
@Slf4j
public class NetworkPropertiesKit {
@Value("${common-config.active}")
private String active;
@Value("${common-config.relativepath}")
private String relativePath;
public NetworkProperties getNetworkProperties() {
String configPath = null;
if ("dev".equals(active)) {
configPath = Thread.currentThread().getContextClassLoader().getResource("").getPath() + relativePath;
log.info("编译器环境, 配置文件直接从resource中取");
log.info("路径: " + configPath);
}
if ("prod".equals(active)) {
configPath = System.getProperty("user.dir") + File.separator + relativePath;
log.info("jar包部署环境, 配置文件根据相对路径取");
log.info("路径: " + configPath);
}
if (null == configPath) {
log.error("路径获取失败");
}
return NetworkProperties.getNetworkProperties(getProperties(configPath));
}
private Properties getProperties(String filePath) {
try {
Properties properties = new Properties();
properties.load(new FileInputStream(filePath));
return properties;
} catch (IOException e) {
log.error(filePath + ": 文件读取失败");
e.printStackTrace();
}
return null;
}
}
读取
@Autowired
private NetworkPropertiesKit networkPropertiesKit;
NetworkProperties networkProperties = networkPropertiesKit.getNetworkProperties();
推荐阅读
-
SpringBoot读取自定义的Properties
-
SpringBoot自定义Properties
-
Springboot读取自定义properties文件和application.properties文件的值
-
SpringBoot 加载自定义 properties文件
-
SpringBoot自定义读取properties
-
JAR包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法(优先级配置)
-
Java读取properties配置文件时,出现中文乱码的解决方法
-
POI对Excel自定义日期格式的读取(实例代码)
-
application.properties文件没有叶子图标,springboot项目启动的时候也报错
-
Java读取properties配置文件时,出现中文乱码的解决方法