zoukankan      html  css  js  c++  java
  • 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  这个是自己定义的监听类

     
  • 相关阅读:
    GridView多行多列合并单元格(指定列合并)
    项目管理知识体系指南PMBOK指南
    作业调度框架 Quartz.NET 2.0 beta 发布
    外语学习有助于大脑发育
    抢书之JS版
    mac 安装python和Django开发环境
    戏谈一道面试题
    —安装时填写注册表
    深入浅出SQL Server Replication第一篇:走近Replication(上)
    IIS寄宿方式的Web地址、BaseAddress和EndPoint Address的关系
  • 原文地址:https://www.cnblogs.com/dkws/p/12228105.html
Copyright © 2011-2022 走看看