首页 > 其他分享 >SpringBoot使用spring.factories加载默认配置

SpringBoot使用spring.factories加载默认配置

时间:2024-06-23 15:21:40浏览次数:3  
标签:SpringBoot spring FrameContextInitializer File tempDir new checkFilePath factori

在日常开发过程中,发布一些产品或者框架时,会遇到某些功能需要一些配置才能正常运行,这时我们需要的提供默认配置项,同时用户也能覆盖进行个性化

创建Initializer

public class FrameContextInitializer implements ApplicationContextInitializer  {

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        System.out.println("FrameContextInitializer--Start");
        System.out.println("FrameContextInitializer--End");
    }
}

配置Initializer

resources/META-INF文件夹下创建spring.factories文件,指定实现类

org.springframework.context.ApplicationContextInitializer=com.haopan.frame.common.initializer.FrameContextInitializer

实现Initializer

读取默认yml文件

 String frameYAMLFilePath = ClassPathFileUtil.getFilePathActual("systemfile/config/frame.yml");
 
 public static String getFilePathActual(String classFilePath) {
        String filePath = "";
        try {
            String templateFilePath = "tempfiles/classpathfile/";
            File tempDir = new File(templateFilePath);
            if (!tempDir.exists()) {
                tempDir.mkdirs();
            }
            String[] filePathList = classFilePath.split("/");
            String checkFilePath = "tempfiles/classpathfile";
            for (String item : filePathList) {
                checkFilePath += "/" + item;
            }
            File tempFile = new File(checkFilePath);
            if (tempFile.exists()) {
               tempFile.delete();
            }
            //解析
            ClassPathResource classPathResource = new ClassPathResource(classFilePath);
            InputStream inputStream = classPathResource.getInputStream();
            checkFilePath = "tempfiles/classpathfile";
            for (int i = 0; i < filePathList.length; i++) {
                checkFilePath += "/" + filePathList[i];
                if (i == filePathList.length - 1) {
                    //文件
                    File file = new File(checkFilePath);
                    if(!file.exists()){
                        FileUtils.copyInputStreamToFile(inputStream, file);
                    }
                } else {
                    //目录
                    tempDir = new File(checkFilePath);
                    if (!tempDir.exists()) {
                        tempDir.mkdirs();
                    }
                }
            }
            inputStream.close();
            filePath = checkFilePath;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return filePath;
    }

将yml内容加到环境上下文

这边的addLast是执行顺序为最后读取,如果项目的yml文件没有读取到,则默认配置是一个保底

System.out.println("FrameContextInitializer--Start");    

PropertySource<?> propertySource = loader.load("frameConfiguration", new InputStreamResource(new FileInputStream(frameYAMLFilePath))).get(0);
applicationContext.getEnvironment().getPropertySources().addLast(propertySource);

System.out.println("FrameContextInitializer--End");

标签:SpringBoot,spring,FrameContextInitializer,File,tempDir,new,checkFilePath,factori
From: https://www.cnblogs.com/yanpeng19940119/p/18263482

相关文章

  • 任务调度SpringTask入门
    任务调度简介1.1什么是任务调度在企业级应用中,经常会制定一些“计划任务”,即在某个时间点做某件事情,核心是以时间为关注点,即在一个特定的时间点,系统执行指定的一个操作。常见的任务调度框架有Quartz和SpringTask等。SpringTask入门1.2SpringTask入门小Demo创建模块52xbc-......
  • SpringMVC类型转换Converter使用
    Converter接口//S:表示接受的类型,T:表示目标类型publicinterfaceConverter<S,T>{/***实现类型转换的方法*/@NullableTconvert(Ssource);}自定义类型转换器/***@authorsongzixian*@create2019-07-23下午3:22*@description自定义类型转换器*/......
  • 计算机毕业设计|基于微信小程序的宠物自动喂养系统 基于SpringBoot的宠物自动喂养系统
    ......
  • SpringBoot + 虚拟线程,鸟枪换大炮!
    “虚拟”线程,望文生义,它是“假”的,它不直接调度操作系统的线程,而是由JVM再提供一层线程的接口抽象,由普通线程调度,即一个普通的操作系统线程可以调度成千上万个虚拟线程。虚拟线程比普通线程的消耗要小得多得多,在内存足够的情况下,我们甚至可以创建上百万的虚拟线程,这在之前(Jav......
  • Spring Boot原理
    自动装配原理pom.xml父工程spring-boot-starter-parent<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.3.1</version><relativePath/>......
  • SpringBoot前后端传递数据时常用的JSON格式数据是什么?【讲解JSON概念、语法、以及Java
    SpringBoot前后端传递数据时常用的JSON格式数据是什么?JSON概念JSON语法JSON的两种结构:JSON字符串和Java对象互转:objectMapper.writeValueAsString(person);objectMapper.readValue(jsonStr,Person.class);在SpringMVC框架中,前后端交互会自动转JsonJSON概念JSON:Jav......
  • 学生读书笔记共享系统-毕业设计-Springboot+mysql+Vue
    介绍学生读书笔记共享系统是一款专为学生设计的平台,旨在通过信息化手段实现读书笔记的共享和交流。系统分为管理端和用户端两个角色,分别为管理员和学生用户提供不同的功能模块,满足各自的需求。该系统不仅促进了学生之间的学习交流,还提升了学习效率和笔记管理的便捷性。技术栈......
  • 基于springboot的技术交流和分享平台 毕业设计 springboot+VUE
    介绍在当今迅速发展的信息时代,技术交流和知识分享已成为推动创新和个人成长的重要途径。然而,许多现有平台在笔记管理和分类上存在不足,缺乏有效的知识组织和分享机制,导致信息获取效率低下,交流互动有限。为了解决这些问题,我开发了一款基于SpringBoot的技术交流和分享平台。该平......
  • Springboot计算机毕业设计自动答疑系统小程序【附源码】开题+论文+mysql+程序+部署
    本系统(程序+源码)带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容研究背景随着教育信息化的快速发展,学生对于知识获取和问题解决的需求日益增加。然而,传统的答疑方式,如面对面咨询或邮件回复,存在效率低下、资源分配不均等问题......
  • 基于springboot的信息技术知识竞赛系统源码数据库
    传统办法管理信息首先需要花费的时间比较多,其次数据出错率比较高,而且对错误的数据进行更改也比较困难,最后,检索数据费事费力。因此,在计算机上安装信息技术知识赛系统软件来发挥其高效地信息处理的作用,可以规范信息管理流程,让管理工作可以系统化和程序化,同时,信息技术知识赛系统的......