DEV/QA/PROD Envs Switch Without Modifying Code

本文介绍了一种不修改代码即可在DEV、QA、PROD环境中切换配置的方法。通过在application.properties文件中声明当前环境,并根据不同环境加载特定的属性文件,实现灵活的环境配置管理。此外,还展示了如何在开发环境使用模拟数据,而在生产环境中使用真实数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DEV/QA/PROD Envs Switch Without Modifying Code

application.properties
# announce your current env here in application.properties, and there must be different properties files in different env.(in the same relative path)
cur_env=DEV
LocalPropertyUtil.class
    public static String getProperty(String propertyName) {
        Properties p = new Properties();
        try {
            Properties props = System.getProperties();
            String homepath = props.getProperty("user.home");

            FileInputStream fileInputStream = null;
            if (props.getProperty("os.name").toLowerCase().contains("windows")) {
                fileInputStream = new FileInputStream(new File("..\\config\\application.properties"));
            } else if (props.getProperty("os.name").toLowerCase().contains("mac")) {
                // no
            } else if (homepath.toLowerCase().contains("jenkins_home")) {
                fileInputStream = new FileInputStream(new File("your ABS path of application.properties on jenkins "));
            } else {
                fileInputStream = new FileInputStream(new File("your ABS path of application.properties on other server"));
            }
            p.load(fileInputStream);
            fileInputStream.close();
        } catch (IOException e1) {
            logger.error("Exception", e1);
        }

        return p.getProperty(propertyName);
    }
Service.Class
# if you must use a mock data in your dev environment, and it will be real data in production environment, you can do it like this.

Model model = new Model();
if (LocalPropertyUtil.getProperty("cur_env").equalsIgnoreCase(ConstantDefine.ENV.PROD)) {
     model = getRealData();
}else {
     model.setId("762980");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值