首页 > 其他分享 >SpringBoot环境扩展机制

SpringBoot环境扩展机制

时间:2023-06-12 20:12:12浏览次数:35  
标签:SpringBoot 扩展 springframework env org import 机制 public EnvironmentPostProcessor

前言

Spring Boot在启动时,会先创建Environment实例,然后再创建ApplicationContext上下文。在创建Environment时,提供了扩展机制给用户对Environment实例进行修改,如Spring Boot默认使用的application.yml属性配置文件。

如何使用该机制

  • 编写类实现EnvironmentPostProcessor接口。
  • 在类路径下的META-INF目录下建立spring.factories,然后将类写到该文件中。

例子代码如下所示

package com.wangtao.springboottest.env;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.util.List;

public class UserEnvPostProcessor implements EnvironmentPostProcessor, Ordered {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        // 加载类路径下的yml文件, 并将其添加到Spring环境中
        final String filename = "user.yml";
        YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
        try {
            List<PropertySource<?>> propertySources = loader.load(filename, new ClassPathResource(filename));
            propertySources.forEach(source -> environment.getPropertySources().addLast(source));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 最低优先级执行, 低于别的EnvironmentPostProcessor实例执行
     */
    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }
}

spring.factories

org.springframework.boot.env.EnvironmentPostProcessor=\
com.wangtao.springboottest.env.UserEnvPostProcessor

注: key固定为org.springframework.boot.env.EnvironmentPostProcessor,value为自定义类的全路径名,可以指定多个,逗号分割。

验证

package com.wangtao.springboottest.env;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Data
@ConfigurationProperties(prefix = "user")
public class UserProperties {

    private String nickname;

    private Integer age;
}

启动类

package com.wangtao.springboottest;

@EnableConfigurationProperties({UserProperties.class})
@SpringBootApplication
public class SpringBootTestApplication implements SchedulingConfigurer {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootTestApplication.class, args);
    }
}

user.yml

user:
  nickname: wangtao
  age: 25

总结

使用这种方式加载属性配置文件有一个好处,会在ApplicationContext上下文创建之前就加载好。而@PropertySource这种方式是在上下文创建的过程中随着bean的注册而加载的,并且默认情况下该注解不支持yml格式。

标签:SpringBoot,扩展,springframework,env,org,import,机制,public,EnvironmentPostProcessor
From: https://www.cnblogs.com/wt20/p/17476002.html

相关文章

  • Springboot读取不到yml文件的问题(转载)
    Springboot工程分为两个大文件夹,/src/main和/src/test,main和test下面分别由java和resource,如图所示: 规则:main的java里面的代码,只能读取main的resource的配置文件;test的java里面的代码,既可以读取main的resource的配置文件,也可以读取test的resource的配置文件。test......
  • 随笔(十八)『SpringBoot 整合 Swagger2』
    1、添加Swagger2依赖<!--swagger2--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version>......
  • gradle 构建springboot 项目两种方式
    第一种直接用插件spring-boot-gradle-plugin无需写入版本buildscript{repositories{maven{url'https://maven.aliyun.com/repository/public'}}dependencies{classpath'org.springframework.boot:spring-boot-gradle-plugin......
  • springboot日期格式化,时差问题
    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录前言一、mysql中日期字段的正确设置二、日期格式化,时差1.日期字段返回格式不正确--方案一2.日期字段返回格式不正确--方案二二、日期无法自动填充1.mybatis-plus2.mybatis只能靠自己了总结前言随着mysql8......
  • 自定义SpringBoot的starter
    1.自定义starter名为my-starter-spring-boot-starter1.1idea中创建一个maven模块groupId为com.exampleartifactId为my-starter-spring-boot-starter起名规范:1.官方starter是spring-boot-starter-xxxx2.自定义starter是xxx-spring-boot-starter依赖如下<?xmlversion="1.0......
  • SpringBoot多模块项目搭建以及搭建基础模板
    多模块项目搭建目录多模块项目搭建1.父项目pom文件编辑2.创建子模块1.父项目pom文件编辑<!--1.父工程添加pom格式--><packaging>pom</packaging><!--定义子模块--><modules><module>walker-service</module><module>walker-utils&......
  • springboot使用swagger2以及遇到的一些问题
    1.导入依赖<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><dependency><groupId>io.springfox</groupId>......
  • springboot kettle gralde dockerfile 多阶段构建
    dockerfileFROMopenjdk:8-jdk-alpineASTEMP_BUILD_IMAGEENVENVREFRESH_DATE2023-06-1215:00RUNset-eux&&sed-i's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g'/etc/apk/repositoriesRUNapkupdate&&apkadd--no-cacheb......
  • Kafka副本机制
    1副本机制的定义:所谓副本机制(Replication),也可以称之为备份机制,通常是指分布式在多台网络互连的机器上保存有相同的数据拷贝。2副本机制的好处:提供数据冗余。即使系统部分组件失效,系统依然能够继续运转,因而增加了整体可用性以及数据持久性。提供高伸缩性。支持横向扩展,能够通......
  • Redis两种持久化机制RDB和AOF详解
    redis是一个内存数据库,数据保存在内存中,但是我们都知道内存的数据变化是很快的,也容易发生丢失。幸好Redis还为我们提供了持久化的机制,分别是RDB(RedisDataBase)和AOF(AppendOnlyFile)。在这里假设你已经了解了redis的基础语法,某字母网站都有很好的教程,可以去看。基本使用的文章......