背景

读取配置是基础能力,研发这个模式不错,可以从不同配置中读取数据,如下图:



可以根据不同分类的文件来管理配置,然后统一在conf中配置哪些文件

package com.jwen.platform.config;

import com.jwen.platform.exception.AppConfigurationException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern; public enum Configurations {
INSTANCE; private final static String KEY_VALUE_SEPARATOR = "=";
private final static String CONFIGURATION_FILE = "application.conf";
private final static String OTHER_CONFIGURATION_FILE_KEY = "app_include_file"; private List<String> otherConfigurationFiles = new ArrayList<>();
private Map<String, String> configurations = new HashMap<>(); Configurations() {
try {
init();
} catch (IOException | URISyntaxException e) {
throw new AppConfigurationException("has error in your application.conf", e);
}
} private void init() throws IOException, URISyntaxException { InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(CONFIGURATION_FILE);
BufferedReader br = new BufferedReader(new InputStreamReader(resourceAsStream)); String s = "";
List<String> lines = new ArrayList<String>(); while ((s = br.readLine()) != null) {
lines.add(s);
} // 关闭流
resourceAsStream.close();
br.close(); initConfigurations(lines);
initOtherConfigurations(); } private void initOtherConfigurations() throws URISyntaxException, IOException {
for (String fileName : otherConfigurationFiles) {
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(resourceAsStream)); String s = "";
List<String> lines = new ArrayList<String>(); while ((s = br.readLine()) != null) {
lines.add(s);
} // 关闭流
resourceAsStream.close();
br.close(); initConfigurations(lines);
} otherConfigurationFiles = null;
} private boolean isOtherConfigurationKey(String key) {
return OTHER_CONFIGURATION_FILE_KEY.equalsIgnoreCase(key);
} private void initConfigurations(List<String> lines) throws URISyntaxException, IOException {
lines.stream().forEach(line -> {
int keyValueSeparatorIndex = line.indexOf(KEY_VALUE_SEPARATOR);
if (isLegalConfigurationLine(line)) {
String key = line.substring(0, keyValueSeparatorIndex).trim();
String value = line.substring(keyValueSeparatorIndex + 1, line.length()).trim();
if (isOtherConfigurationKey(key)) {
otherConfigurationFiles.add(value);
} else {
configurations.put(key, value);
}
}
});
} private boolean isLegalConfigurationLine(String line) {
Pattern pattern = Pattern.compile("^[a-zA-Z](.*?)=(.*?)");
return pattern.matcher(line).matches();
} public String get(String key) {
return configurations.get(key);
} public static void main(String[] args) { System.out.println(Configurations.INSTANCE.get("test.on"));
}
}

spring项目读取配置的demo的更多相关文章

  1. spring项目读取配置文件

    Spring项目在运用中读取配置文件有两种方式: 通过项目的配置文件读取 在spring-context.xml里面加入以下代码 在运用到的类里面加入 @Value("#{configPro ...

  2. Python项目读取配置的几种方式

    1. 将配置写在Python文件中 配置文件(config.py 或 settings.py) 通常放置在程序源代码的目录,方便引用 配置文件 # settings.py class Config(o ...

  3. Spring项目读取resource下的文件

    目录 一.前提条件 二.使用ClassPathResource类读取 2.1.Controller.service中使用ClassPathResource 2.2.单元测试使用ClassPathRes ...

  4. Spring Boot读取配置的几种方式

    读取application文件 在application.yml或者properties文件中添加: info.address=USAinfo.company=Springinfo.degree=hi ...

  5. Spring Boot读取配置的 5 种方式

    读取application文件 在application.yml或者properties文件中添加: info.address=USA info.company=Spring info.degree= ...

  6. web项目中配置多个数据源

    web项目中配置多个数据源 spring + mybatis 多数据源配置有两种解决方案 1.配置多个不同的数据源,使用一个sessionFactory,在业务逻辑使用的时候自动切换到不同的数据源,  ...

  7. 非web环境的注解配置的spring项目应用(non-web, Spring-data-jpa, JavaConfig, Java Application, Maven, AnnotationConfigApplicationContext)

    非web环境的spring应用 springframework提供的spring容器,非常适合应用于javaweb环境中. 同时,spring组件的低耦合性为普通java应用也提供了足够的支持. 以下 ...

  8. Spring Boot项目属性配置

    接着上面的入门教程,我们来学习下Spring Boot的项目属性配置. 1.配置项目内置属性 属性配置主要是在application.properties文件里配置的(编写时有自动提示)这里我们将se ...

  9. 使用java配置来构建spring项目

    java配置是Spring4.x推荐的配置方式,可以完全代替xml配置,java配置是通过@Configuration和@Bean来实现的.@Configuration声明当前类是一个配置类,相当于S ...

随机推荐

  1. 查看PHP代码执行的时间

    $t1 = microtime(true); //...要执行的代码$t2 = microtime(true); echo '耗时'.round($t2-$t1,3).'秒';

  2. 提升HTML5的性能体验系列之四 使用原生UI

    原生UI的设计目的 HTML和css有一个优势就是灵活的样式设计.在大多数情况下,我们都应该使用HTML+css来负责UI.但是有些情况下,我们发现HTML+css的UI不满足需求.1. 绝对置顶HT ...

  3. 初识jvm堆,栈参数

    堆的分配参数: -Xmx //设立最大堆 -Xms //最小堆,初始化堆大小 -Xmn  //设置新生代(eden+2*surviivor+old)大小   官方推荐:3/8Xmx------> ...

  4. js网页上画图

    保存 1.d3.js  (http://www.d3.org/)使用svg技术,展示大数据量,动态效果很好,但是API暴露的不好,得靠自己摸索. 2.http://raphaeljs.com/refe ...

  5. NOIP2017普及组T1题解

    神奇的链接 上面时题目. 其实不得不说,这一题很水,比2015年的第一题水多了. 直接按题目套公式就行了,当然你也可以像我一样化简一下. 直接看代码: #include<cstdio> # ...

  6. POJ-3252 Avenger

    题意:在区间中,他们化成2进制的数的0的个数大于等于1的数有多少个. 思路:我们需要记录上一次0和1的个数,此外我们还要特别注意一下前导0. 如果前面全是0的时候我们就要注意下一位是不是还是0,如果一 ...

  7. chattr改变文件属性

    Linux chattr命令用于改变文件属性. 这项指令可改变存放在ext2文件系统上的文件或目录属性,这些属性共有以下8种模式: a:让文件或目录仅供附加用途. b:不更新文件或目录的最后存取时间. ...

  8. mysql之索引查询1

    一 备份数据 备份库: mysqldump:拷贝数据 --database:数据库 基本语法是:mysqldump -h服务器名 -u用户名 -p密码 --database 库名 > 备份路径. ...

  9. java运行报错:nested exception is java.lang.NoSuchFieldError: INSTANCE,但使用@Test测试是好的

    解决方法: 原因是,在tomcat里,同名不同版本的jar包,默认加载版本低的.我项目里有两个httpclient jar包.一个4.2.5  另一个是4.5.所以加载了4.2.5的,而我要用的是4. ...

  10. HMM(隐马尔可夫模型)不断学习中

    HMM(隐马尔可夫模型)是用来描述隐含未知参数的统计模型,举一个经典的例子:一个东京的朋友每天根据天气{下雨,天晴}决定当天的活动{公园散步,购物,清理房间}中的一种,我每天只能在twitter上看到 ...