JadConfig classpathRepository 扩展

JadConfig 默认包含了基于内存,properties 文件,系统属性,以及环境变量的Repository,但是对于classpath 的文件处理不是很方便
我们可以自己在扩展

接口实现定义

 
public interface Repository {
 
    /**
     * Opens the configuration repository, e. g. create a database connection, open a file on disk for reading
     *
     * @throws RepositoryException If an error occurred while opening the data source
     */
    void open() throws RepositoryException;
 
    /**
     * Reads the configuration parameter {@literal name} from the backing data source. The {@literal name} is specific
     * to the underlying data source.
     *
     * @param name The parameter name
     * @return The value of the provided {@literal name} as {@link String}
     */
    String read(String name);
 
    /**
     * Closes the underlying data source when it isn't require any more.
     *
     * @throws RepositoryException If an error occurred while closing the underlying data source
     */
    void close() throws RepositoryException;
}

参考实现

public class ClassPathRepository implements Repository {
    private final Properties PROPERTIES = new Properties();
 
    private String fileName = null;
 
    public ClassPathRepository(String filename) {
        this.fileName = filename;
    }
 
    @Override
    public void open() throws RepositoryException {
        if (fileName == null) {
            throw new RepositoryException("Properties file must not be null!");
        }
        InputStream inputStream = null;
        try {
            inputStream = ClassPathRepository.class.getClassLoader().getResourceAsStream(fileName);
            PROPERTIES.load(inputStream);
        } catch (IOException ex) {
            throw new RepositoryException("Couldn't open properties file: " + fileName, ex);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // Intentionally empty
                }
            }
        }
 
    }
 
    @Override
    public String read(String name) {
        return PROPERTIES.getProperty(name);
    }
 
    @Override
    public void close() throws RepositoryException {
 
    }
}

使用

 JadConfig jadConfig = new JadConfig(new ClassPathRepository("app.properties"),bean);
 jadConfig.process();

说明

以上是一个简单的classpathRepository实现,实际上我们也可以基于其他文件扩展,比如yaml ,nacos 等,可以方便的实现配置管理,JadConfig 对于配置的管理方式还是很支持的借鉴学习的,比如dremio 对于配置也实现了类似的能力,包含check,validator。。。。

参考资料

https://github.com/Graylog2/JadConfig

posted on   荣锋亮  阅读(26)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2021-09-26 cube.js websocket 实时数据更新说明
2020-09-26 gopacket 流量抓包golang 包
2018-09-26 stenciljs 学习六 组件开发样式指南
2018-09-26 stenciljs 学习五 事件
2018-09-26 stenciljs 学习四 组件装饰器
2018-09-26 stenciljs 学习三 组件生命周期
2018-09-26 stenciljs 学习二 pwa 简单应用开发

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示