背景

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



可以根据不同分类的文件来管理配置,然后统一在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. jquery全选反选

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. 155. Min Stack - Unsolved

    https://leetcode.com/problems/min-stack/#/solutions Design a stack that supports push, pop, top, and ...

  3. python之常用模块篇5

    一.日志模块,logging模块 1)logging模块简单使用,屏幕输出.默认级别30 import logging logging.debug( logging.info( logging.war ...

  4. ubuntu新建、删除用户

    新建用户名为newuser的用户,并赋予sudo权限 adduser newuser --ingroup sudo 删除用户以及用户目录 deluser -r newuser

  5. MyBatis中实现多表查询

    如果查询的数据量大,推荐使用N+1次查询.数据量少使用联合查询... 一. 1.Mybatis是实现多表查询方式 1.1  业务装配:对两个表编写单表查询语句,在业务(Service)把查询的两表结果 ...

  6. ThinkPHP 二维码生成

    请求获取并展示二维码 <img src="<?php echo U('createCode?zsnumber='.$time.$kcname['id'].$stuInfo['id ...

  7. .NET性能优化(文摘)

    第1章 性能指标 1.1 性能目标 1.2 性能指标 第2章 性能度量 2.1 性能度量方式 白盒测试-小程序 黑盒测试-大型程序 2.2 Windows内置工具 2.2.1 性能计数器 2.2.2  ...

  8. PHP-CGI、FASTCGI和php-fpm的关系

    首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. web server(比如说nginx)只是内容的分发者.比如,如果请求/index.h ...

  9. BZOJ 3259 [Sdoi2014]数表 (莫比乌斯反演 + 树状数组)

    3529: [Sdoi2014]数表 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 2321  Solved: 1187[Submit][Status ...

  10. 链家web前端面试

    共有三轮面试,每个面试官的第一个问题都是:介绍一个你觉着比较出彩的项目 第一轮面试: 因为公司项目没什么亮点,很传统的pc端,美女面试官就说让讲一下我用react的私人项目; 问了很多都是关于reac ...