首页 > 其他分享 >关于服务器上部署war后怎么配置读取外部的yml文件

关于服务器上部署war后怎么配置读取外部的yml文件

时间:2023-06-15 12:56:05浏览次数:46  
标签:core 读取 springframework war File org import new yml

第一步打成war包
1.pom.xml 修改为

<packaging>war</packaging>

2.然后就是依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-tomcat</artifactId>
   <scope>provided</scope>
</dependency>

3.继承启动类

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        //Application的类名
        return application.sources(DataCenterApplication.class);
    }

}

4.在项目的config下建类 MyEnvironmentPostProcessor类实现

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.File;
import java.io.IOException;
import java.util.Properties;
public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication application) {
        //tomcat路径
        String property = System.getProperty("catalina.home");
        System.out.println("catalinahome:"+property);

        String path =property+ File.separator+"conf"+File.separator+"application.properties";
        File file = new File(path);
        System.out.println("Loading local settings from : "+path);

        if (file.exists()) {
            MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
            Properties properties = loadProperties(file);
            System.out.println(properties.toString());
            propertySources.addFirst(new PropertiesPropertySource("Config", properties));
        }
    }

    private Properties loadProperties(File f) {
        FileSystemResource resource = new FileSystemResource(f);
        try {
            return PropertiesLoaderUtils.loadProperties(resource);
        } catch (IOException ex) {
            throw new IllegalStateException("Failed to load local settings from " + f.getAbsolutePath(), ex);
        }
    }
}

4.1 如果你的文件是yml

package com.fyking.etown.config;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication application) {
        //tomcat路径
        String property = System.getProperty("catalina.home");
//        property = "D:\\opt\\module\\tomcat8\\apache-tomcat-8.5.89";
        System.out.println("catalinahome:" + property);

        String path = property + File.separator + "conf" + File.separator + "application-druid.yml";
        File file = new File(path);
        System.out.println("Loading local settings from : " + path);

        if (file.exists()) {
//            MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
            YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
            yaml.setResources(new FileSystemResource(path));
//            Properties properties = loadProperties(file);
            System.out.println(yaml.toString());

            MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
            propertySources.addFirst(new PropertiesPropertySource("Config", yaml.getObject()));

        }
    }

    private Properties loadProperties(File f) {
        FileSystemResource resource = new FileSystemResource(f);
        try {
            return PropertiesLoaderUtils.loadProperties(resource);
        } catch (IOException ex) {
            throw new IllegalStateException("Failed to load local settings from " + f.getAbsolutePath(), ex);
        }
    }
}

5.在classpath定义一个META-INF文件夹然后在其下面先建spring.factories文件,在其中指定

org.springframework.boot.env.EnvironmentPostProcessor=com.xxx.config.MyEnvironmentPostProcessor

标签:core,读取,springframework,war,File,org,import,new,yml
From: https://www.cnblogs.com/fyking0318/p/17482569.html

相关文章

  • XSSFWorkbook读取合并单元格中的数据
    @ServicepublicclassAppInfoUploadServiceImplimplementsAppInfoUploadService{@AutowiredprivateAppInfoMapperappInfoMapper;@AutowiredprivateCommonMappercommonMapper;@OverridepublicvoiduploadAppInfo(MultipartFilefile)......
  • python读取文件——python读取和保存mat文件
      首先我们谈谈MarkDown编辑器,我感觉些倒是挺方便的,因为用惯了LaTeX,对于MarkDown还是比较容易上手的,但是我发现,MarkDown中有这样几个问题一直没能找到具体的解决方法:图片大小的问题。在LaTeX中我们可以调整图片的大小,以适应整个文本;字体,字号大小的设置。在MarkDown里面标题倒是......
  • GPU-aware MPI + Python GPU arrays
     condainstall-cconda-forgempi4pyopenmpi  ForLinux64,OpenMPIisbuiltwithCUDAawarenessbutthissupportisdisabledbydefault.Toenableit,pleasesettheenvironmentalvariableOMPI_MCA_opal_cuda_support=truebeforelaunchingyourMPIproc......
  • poi 读取 excel 总行数 ,总列数 注意事项 lastRowNum 、lastCellNum,起始 行号 lastRowN
    poi读取excel总行数,总列数注意事项lastRowNum、lastCellNumhttps://blog.csdn.net/HaHa_Sir/article/details/127235280        poi读取excel总行数,总列数注意事项lastRowNum、lastCellNum一、概述        1、如下图,有一个4行3列的excel表......
  • 3、git warning: TLS certificate verification has been disabled
    gitwarning:TLScertificateverificationhasbeendisabled!报错warning:-----------------SECURITYWARNING----------------warning:|TLScertificateverificationhasbeendisabled!|warning:---------------------------------------------------warning......
  • 为什么RLHF中,PPO需要Critic模型而不是直接使用RewardModel
    在强化学习中,PPO(ProximalPolicyOptimization)算法是一种基于策略梯度的方法,用于训练强化学习智能体。PPO算法中引入Critic模型的主要目的是为了提供一个价值估计器,用于评估状态或状态动作对的价值,从而辅助策略的更新和优化。虽然奖励模型(RewardModel)可以提供每个状态或状态动作......
  • NodeJS研究笔记:利用Buffer类的二进制数据读取接口解析ELF文件格式
    javascript作为前端开发语言,自古来对二进制数据的读取解析方面的支持都很薄弱,一般来说,解析二进制数据时,往往是将数据转换成字符串,然后运用各种字符串操作技巧来实现二进制数据的读取。由于NodeJS作为后台服务器开发平台,数理逻辑的设计需求超越javascript作为前端语言时界面UI的设......
  • java开发C语言解释器:数组元素的读取和赋值
    本节技术内容难度较大,请结合视频对代码的讲解和调试来理解本节内容:用java开发编译器一个成熟的编译器或解释器,要能够解析和执行目标语言开发的逻辑复杂的程序代码,我们用java开发的C语言解释器,能够执行用C语言开发的较为复杂的程序时,才称得上是合格的,从本节开始,我们致力于C语言解......
  • VMware虚拟机和主机传输文件
    原文链接虚拟机为Linux系统使用vm-tools即可。卸载旧工具:vmware-uninstall-tools.pl安装新工具:apt-getinstallopen-vm-tools-desktop重启系统:reboot此时可以使用Ctrl+C、Ctrl+V的方式在主机和Linux虚拟机之间传输文件。虚拟机为Windows系统首先在本机新建一个文件......
  • 文件读取工具类
    importjava.io.*;/***文件读取工具类*/publicclassFileUtil{/***读取文件内容,作为字符串返回*/publicstaticStringreadFileAsString(StringfilePath)throwsIOException{Filefile=newFile(filePath);if(!file.......