欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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(); 

相关标签: Spring SpringBoot