从配置文件到配置类的一种实现方式

为什么想要实现

  • 在写配置文件的时候, key并不能进行很好的提示
  • 感觉配置文件过于关注底层, 例如:
    • 在代码中可以使用String.class.getName()来获取全类名, 但是在配置文件中必须写成java.lang.String
    • 在代码中可以使用16*1024*1024来表示16MB, 但是在配置文件中则必须自己将具体的数值计算出来

综上原因, 提供一种从配置文件到配置类的实现方式

实现逻辑

  1. 设置接口XConfig
  2. 配置类实现接口, 并重载初始化方法init(), 在其中进行属性的配置
  3. 通过工具类将各个配置类中的属性进行整合

通用接口

public interface XConfig {
    Properties properties = new Properties();

    default Properties getProperties() {
        return properties;
    }

    /**
     * 用来接收各种输入类型, 不需要在使用的时候进行考虑类型
     *
     * @param key
     * @param value
     * @param <V>
     */
    static <V> void setProperty(String key, V value) {
        if (value instanceof String) {
            // value为字符串类型
            properties.setProperty(key, (String) value);
        } else if (value instanceof Class) {
            // 如果是一个类的类型
            properties.setProperty(key, ((Class<?>) value).getName());
        } else {
            // value为数值等其他基本类型
            properties.setProperty(key, String.valueOf(value));
        }

    }

    void init();
}

自定义配置类

public class XBaseConfig implements XConfig {

    // 配置需要设置的各项配置属性
    @Override
    public void init() {

        XConfig.setProperty(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, "hadoop001:9092,hadoop002:9092,hadoop003:9092");

        XConfig.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        XConfig.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);

        XConfig.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        XConfig.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    }
}

工具类

public class XUtils {
	
    //整合所有的配置类
    public static Properties getSystemResource(List<Class<? extends XConfig>> configClasses) {
        Properties properties = new Properties();
        try {
            for (Class<? extends XConfig> configClass : configClasses) {
                XConfig xConfig = configClass.newInstance();
                xConfig.init();
                Properties propertiesX = xConfig.getProperties();
                propertiesX.forEach((key, value) -> properties.setProperty((String) key, (String) value));
            }
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }

        return properties;
    }
}

使用示例

    @Test
    public void getConfigClassTest() {
        // 这里可以添加多个配置类
        List<Class<? extends XConfig>> classes = new ArrayList<>();
        classes.add(XBaseConfig.class);

        Properties properties = getSystemResource(classes);
        properties.forEach((key, value) -> System.out.println(key + "=" + value));
    }

测试结果:
image-20221104214232986

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值