Spring读取不同环境的配置文件

目录

  • 配置文件

    • 需求
    • Spring环境
      • 配置类
      • PropertySourceFactory
    • 非Spring环境

配置文件

需求

针对不同环境下,可以配置不同的配置文件,如application-dev.properties、application-test.properties。

Spring环境

采用spring.profiles.active属性来区分不同的应用环境。

配置类

@Data
@Component
@ConfigurationProperties(prefix = "log")
@PropertySource(name = "log", value = "classpath:log-${spring.profiles.active:}.properties", factory = SystemPropertySourceFactory.class)
public class LogConfig implements Serializable {

    private static final long serialVersionUID = 1L;
    private String            tableName        = "comm_log";
    //private String            logType          = "LX2";
    private String            host;
    private int               port;
    private int               maxThreads       = 20;
    private int               saveMode         = 0;
    private boolean           saveLog          = true;
    private String            topic            = "TOPIC_COMMON_LOG";
    private String            defaultErrCode;
    private boolean           openInTable      = false;
    private String            inTableName;
    private String            remoteUrl;
    private String            namesrvAddr;
    private String            producerGroupName;
    private String            instanceName;
    private int               sendTimeout;
}

若spring.profiles.active为空,则该值为空字符串。

PropertySourceFactory

/**
 * 针对自定义配置文件的一个多环境配置逻辑
 *
 */
public class SystemPropertySourceFactory implements PropertySourceFactory {

    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource)
        throws IOException {
        String fileSuffix = "";
        try {
            String filename = resource.getResource().getFilename();
            fileSuffix = filename.substring(filename.lastIndexOf("."));
            resource.getResource().getFile();
            return new ResourcePropertySource(name, resource);
        } catch (Exception e) {
            InputStream inputStream = SystemPropertySourceFactory.class.getClassLoader()
                .getResourceAsStream(name + fileSuffix);
            //转成resource
            InputStreamResource inResource = new InputStreamResource(inputStream);
            return new ResourcePropertySource(new EncodedResource(inResource));
        }
    }

}

若spring.profiles.active=dev有配置,但是没有这个配置文件(例:log-dev.properties),则默认读取log.properties。若spring.profiles.active无配置,则默认读取log.properties。

非Spring环境

思路:将spring.profiles.active 属性设置到System环境变量。则非Spring环境可从System环境变量中获取该值。

@Component
public class ServerInfo implements ApplicationListener<WebServerInitializedEvent> {
    private static final String SPRING_PROFILES_ACTIVE = "spring.profiles.active";

    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        //设置系统变量
        System.setProperty(SPRING_PROFILES_ACTIVE, this.getProfileActive());
    }

    /**
     * 获取spring.profiles.active属性值
     * @return
     */
    public String getProfileActive() {
        return env.getProperty(SPRING_PROFILES_ACTIVE, "");
    }

}

原文地址:https://www.cnblogs.com/chenzuyibao/p/11425046.html

时间: 2024-08-29 19:11:13

Spring读取不同环境的配置文件的相关文章

Spring读取外部的资源配置文件—@PropertySource和@Value实现资源文件配置

通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值: @PropertySource注解主要是让Spring的Environment接口读取属性配置文件用的,标识在@Configuration配置类上:@Value注解可以用在字段和方法上,通常用于从属性配置文件中读取属性值,也可以设置默认值. 具体用法: @PropertySource(value = { "classpath:config.properties" }, ignoreResourceN

spring读取properties和其他配置文件的几种方式

1.因为spring容器的一些机制,在读取配置文件进行数据库的配置等等是很有必要的,所以我们要考虑配置文件的的读取方式以及各个方式的实用性 2.配置文件的读取方式我这里介绍2种,目的是掌握这2种就可以很好的应用了 3.这里我的properies配置文件如下: driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/m_model?useUnicode=true&characterEncoding=utf8 username=roo

spring读取classpath目录下的配置文件通过表达式去注入属性值.txt

spring读取配置文件: 1. spring加载配置文件: <context:property-placeholder location="classpath:config/system.properties"/> 2. 配置文件内容: YTZ_SPECIAL_USER_PHONE=13912622596,18721293900,18656253360 YTZ_SPECIAL_USER_PHONE2=13912622596,18721293900,18656253360

java web路径和spring读取配置文件

此篇博客缘起:部署java web系统到阿里云服务器(ubuntu14.04)的时候,有以下两个问题 找不到自定义的property配置文件 上传图片的时候找不到路径 开发的时候是在windows上的,运行正常,部署的时候就出问题了,肯定是windows和linux路径区别导致的(一个小问题来回鼓捣了几个小时,因为有自己对windows下和linux下的区别还不是特别了解,还有就是每次在windows下修改完成以后都要重新上传到阿里云,项目较大来回也需要较多时间...),遂决定好好看看java

SpringData系列一Spring Data的环境搭建

本节作为主要讲解Spring Data的环境搭建 JPA Spring Data :致力于减少数据访问层(DAO)的开发量.开发者唯一要做的就是声音持久层的接口,其他都交给Spring Data JPA来帮你完成! 使用Spring Data JPA进行持久层开发需要的四个步骤: 配置Spring 整合 JPA 在Spring配置文件中配置Spring Data,让Spring 为声明的接口创建代理对象.配置了<jpa:repositories>后,Spring 初始化容器时将会扫描base-

Shell学习笔记 - 环境变量配置文件

一.source命令 功能:在当前bash环境下读取并执行配置文件中的命令 1. 命令格式 source 配置文件  或  . 配置文件 2. 命令示例 [[email protected] ~]# source /etc/profile #让/etc/profile配置文件生效 [[email protected] ~]# . /etc/profile #与上面的命令效果一样 二.全局环境变量配置文件 全局环境变量配置文件会对所有用户生效. 1. /etc/profile 作用: 1) 定义U

使用Spring读取xml文件中的配置信息

一般写程序时我们都会将一些配置信息写到配置文件中,以便在不修改源码的情况下对程序的某些点进行更改.这里介绍一种Spring读取xml配置文件的方式,其基本思路如下:定义一个java类,其中定义一些静态变量对应我们的配置信息,然后采用注入的方式将变量值初始化为配置值.示例代码如下: 新建一个java类: package java; public class Config { //要配置的值 public static int value = 0; //这里不能写成静态的 public void s

linux环境变量配置文件

环境变量配置文件中主要是定义对系统操作环境生效的系统默认环境变量,如PATH等.当你登陆Linux系统启动一个bash shell时,默认情况下bash会几个文件中查找命令,bash检查的启动文件取决于bash shell的方式.启动bash shell的方式有三种: (1).登陆时当作默认登陆的shell (2).作为非登陆的交互式shell (3).作为运行脚本的非交互shell 环境变量配置文件: /etc/profile /etc/profile.d/*.sh ~/.bash_profi

环境变量配置文件profile

环境变量配置文件 在Ubuntu中有如下几个文件可以设置环境变量1./etc/profile:在登录时,操作系统定制用户环境时使用的第一个文件,此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.2./etc/environment:在登录时操作系统使用的第二个文件,系统在读取你自己的profile前,设置环境文件的环境变量.3.~/.bash_profile:在登录时用到的第三个文件是.profile文件,每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时