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。。。。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用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 简单应用开发