首页 > 其他分享 >JadConfig classpathRepository 扩展

JadConfig classpathRepository 扩展

时间:2022-09-26 23:57:13浏览次数:80  
标签:String 扩展 throws classpathRepository RepositoryException JadConfig public name

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

标签:String,扩展,throws,classpathRepository,RepositoryException,JadConfig,public,name
From: https://www.cnblogs.com/rongfengliang/p/16733016.html

相关文章

  • JadConfig 注解驱动的java 配置管理包
    JadConfig是graylog开源的一个基于注解驱动的java配置管理包,graylogserver对于配置的管理就是使用了此包JadConfig使用比较简单,但是功能还是很强大的,配置包含了校......
  • JavaScript 之 扩展知识
    扩展知识: 1.JavaScript的应用     2.浏览器内核[WebKit]由 WebCore和  JavaScriptCore组成WebCore:负责HTML解析,布局,渲染......
  • 522 剩余系 欧拉定理 扩展欧拉定理
    视频链接:LuoguP5091【模板】扩展欧拉定理#include<iostream>usingnamespacestd;typedeflonglongLL;inta,b,m,phi,flag;chars[20000005];intget_phi(i......
  • lua的c扩展
    ubuntu%cathello.c#include<stdio.h>#include<lua.h>#include<lualib.h>#include<lauxlib.h>staticinthello(lua_State*L){printf("hello\n");re......
  • 扩展欧拉定理笔记
    扩展欧拉定理笔记前置知识欧拉定理\[\forall(a,m)=0,s.t.\,a^{\varphi(m)}\equiv1\;(mod\;m)\]简证:考虑\(m\)的简化剩余系\(S\),它关于模乘法封闭,\(a\)是其中元......
  • docker安装pdo扩展 Fatal error: Uncaught PDOException: could not find driver
    进入php容器$dockerexec-itphp/bin/bash执行安装$docker-php-ext-installpdopdo_mysql如果安装失败出现下面情况/usr/local/bin/docker-php-ext-enable:cannotcr......
  • 数据类型扩展
     packagemyaction;publicclassDemo1{publicstaticvoidmain(String[]args){//整数拓展     二进制0b十进制八进制0......
  • Unity Editor 扩展入门1
    教程来源:https://www.youtube.com/watch?v=491TSNwXTIg&t=204s一个点击物体修改材质颜色的简单editor扩展工具  usingUnityEngine;usingUnityEditor;public......
  • ENVI扩展工具:ENVITask调用代码生成器
    1功能介绍ENVITask开发技术已经非常成熟,可以很方便的调用自带和自定义的ENVITask。唯一不足就是调用代码编写时不太方便,尤其是参数设置,大部分参数名都较长,拼写繁琐,比如下......
  • 扩展加属性 swift
    在Swift中,class依然可以使用关联对象默认情况,extension不可以增加存储属性借助关联对象,可以实现类似extension为class增加存储属性的效果classPerson{}extensionPers......