springboot加载外部的配置文件

package com.liuchao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;

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

public class MySpringApplicationRunListener implements SpringApplicationRunListener, Ordered {
    private SpringApplication application;
    private String[] args;

    @Override
    public void starting() {
        System.out.println(">>>>starting<<<<");
    }

    public MySpringApplicationRunListener(SpringApplication application, String[] args) {
        this.application = application;
        this.args = args;
    }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        // 配置文件读取到程序中 思路需要自己将本地文件读取到程序中,让后在放入到SpringBoot容器
        Properties properties = new Properties();
        try {
            // 1. 读取我们的my.properties文件
            properties.load(this.getClass().getClassLoader().
                    getResourceAsStream("my.properties"));
            // 2. 读取名称名称为my
            PropertySource propertySource = new
                    PropertiesPropertySource("my", properties);
            //3. 将资源添加到SprigBoot项目中
            MutablePropertySources propertySources = environment.getPropertySources();
            // 4.通过该api接口可以将配置文件读取 到SpringBoot项目中
            propertySources.addLast(propertySource);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {

    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {

    }

    @Override
    public void started(ConfigurableApplicationContext context) {

    }

    @Override
    public void running(ConfigurableApplicationContext context) {

    }

    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) {

    }

    @Override
    public int getOrder() {
        return -1;
    }
}在resource 目录下面建 META-INF/spring.factories  文件内容为

#org.springframework.boot.SpringApplicationRunListener=\ 这个值是定死的值
#com.liuchao.MySpringApplicationRunListener  这个是自己定义的监听类

 

原文地址:https://www.cnblogs.com/dkws/p/12228105.html

时间: 2024-08-28 00:47:30

springboot加载外部的配置文件的相关文章

springboot加载外部依赖并在构建包时将其打入相应的目录下

当我们在maven仓库中无法找到需要的依赖时,需要将相应的依赖jar包下载下来放到项目的某个目录下,然后通过配置文件配置将其引入项目中使用.如下引入sigar依赖:具体下载地址 1.依赖具体目录如下: 2.pom.xml配置: <!-- 此处使用外部引用 --> <dependency> <groupId>org.hyperic</groupId> <artifactId>sigar</artifactId> <scope>

19、属性赋值[email&#160;protected]加载外部配置文件

19.属性赋值[email protected]加载外部配置文件 加载外部配置文件的注解 19.1 [xml] 在原先的xml 中需要 导入context:property-placeholder 声明,然后使用${nickName}取值 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"

属性赋值[email&#160;protected]加载外部配置文件

配置类上添加注解@PropertySource加载外部配置文件 @Configuration @PropertySource("classpath:/person.properties") @ComponentScan(value = "com.yyc", includeFilters ={@ComponentScan.Filter(type= FilterType.CUSTOM, classes={MyTypeFilter.class})}, useDefault

转载:Flash AS3.0 加载外部资源(图片,MP3,SWF)的两种方式

Flash AS3.0 加载外部资源(图片,MP3,SWF)的两种方式 出自:http://www.cnblogs.com/top5/archive/2012/08/04/2623464.html 关于AS3加载图片等外部资源,我常用的有两种方式:Loader和Embed,各自的特点,总结如下: Embed:在Flash编译阶段就将资源嵌入,也就是说图片资源是会被编译进swf文件里面去的:这样子的好处是代码比较简洁,使用方便,也不存在加载顺序的问题,缺点是会增大swf文件的体积 Loader:在

Windows Phone 8加载外部动态链接库DLL(非安装包内的)

Windows Phone 8加载外部动态链接库DLL(非安装包内的) 在<动态加载与插件化>中大概介绍了下,wp8加载非安装包的下动态链接库,这次详细梳理下. 加载外部DLL主要的原理: 通过NtCurrentTeb获得线程环境块 从线程环境块中获得进程环境块 在进程环境块中加载过得DLL链表 从链表中找到kernelbase.dll的模块句柄 从kernelbase.dll中获得LoadLibraryEx函数地址 加载指定地址下的DLL 相关的结构体: typedef struct _CL

Hadoop中WordCount代码-直接加载hadoop的配置文件

Hadoop中WordCount代码-直接加载hadoop的配置文件 在Myeclipse中,直接编写WordCount代码,代码中直接调用core-site.xml,hdfs-site.xml,mapred-site.xml配置文件 package com.apache.hadoop.function; import java.io.IOException; import java.util.Iterator; import java.util.StringTokenizer; import 

PhoneGap 白名单安全机制 navigator.app 加载外部页面返回以及退出介绍

一. Phonegap 白名单安全机制 Phonegap应用的页面大多存在于本地,但有时需要加载外部的Web页面到应用内置的浏览器 视图中已完成特定的应用功能,出于安全性考虑,PhoneGap 设立了白名单安全机制,通过它来 控制能够加载到内置浏览器视图的内容来源. Android 不通过白名单安全机制的话调用外部浏览器 IOS 不通过白名单安全的话 会直接拒绝 二.Android 配置白名单 在res/xml/config.xml这个文件里进行配置 三.IOS 配置 以及 Wp7/wp8配置白

【Win 10 应用开发】加载外部的 srt 字幕

据说系统内置的多媒体功能支持 srt. ass 等字幕,老周测试过几种格式的字幕均能加载. SRT 字幕是最简单的字幕结构,甚至你用记事本都能做出来,就是分为几行来写. 第一行是字幕的编号,应该是从1开始的,反正工具生成的SRT都是从1开始编号: 第二行是字幕的开始时间和结束时间,中间用一个 “-->”来分隔: 第三行是字幕内容. 比如这样: 18 00:03:37,933 --> 00:03:45,366 時而憑風遨遊,時而髙談怪論 19 00:03:45,366 --> 00:03:

TP3.2加载外部PHPexcel类,实现导入和导出

TP3.2加载外部PHPexcel类,实现导入和导出 导入: 1.将下载好的PHPexcel文件放到libray/Org/Uti/文件夹下,将PHPEXCEL.PHP改为PHPEXCEL.class.php 2.导入类文件 代码: import("Org.Util.PHPExcel");import("Org.Util.PHPExcel.Reader.Excel5"); 3.配置 代码: $objReader = \PHPExcel_IOFactory::creat