• 使用 Spring Profiles 的正确姿势


    Spring Profiles

    今天学习下,Spring的核心功能之一 profiles,该特性允许开发者将beans映射到不同的环境中,如dev、test、prod。开发者启动服务时,可以根据自身需要在不同的环境中激活不同的配置。

    bean使用profile注解

    先来学习一个最简单profle的使用方式,学习如何让bean属于特定的环境。假设一个场景:一个普通的bean,只在开发期间有效,其他环境无效。

    @Component
    @Profile("dev")
    public class DevDatasourceConfig{
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    如上述代码,只需要在声明bean时,配合@Profile注解,并指定特定的环境即可。根据上面的场景,反过来看:假设一个bean除了在开发期间无效,在其他环境(如test、prod)有效。@Profile支持NOT操作,只需要在前面加上 ! 符号。例如 !dev, 就可以将dev环境排除。

    @Component
    @Profile("!dev")
    // @Profile(value={"dev & local"})
    public class DevDatasourceConfig{
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    XML声明profile

    在XML配置文件中也可以配置profiles属性, 标签中定义了一个 profile 属性,多个属性值可以使用逗号分隔

    <beans profile="dev">
        <bean id="devDatasourceConfig" 
          class="org.baeldung.profiles.DevDatasourceConfig" />
    beans>
    
    • 1
    • 2
    • 3
    • 4

    设置profile

    可以通过多种方式设置profile向容器中注册bean。

    WebApplicationInitializer 接口

    web环境中,可以通过实现WebApplicationInitializer接口配置ServletContext上下文。

    @Configuration
    public class MyWebApplicationInitializer 
      implements WebApplicationInitializer {
    
        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
     
            servletContext.setInitParameter(
              "spring.profiles.active", "dev");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    ConfigurableEnvironment 接口

    通过ConfigurableEnvironment接口直接设置profile

    @Autowired
    private ConfigurableEnvironment env;
    ...
    env.setActiveProfiles("someProfile");
    
    • 1
    • 2
    • 3
    • 4

    Web.xml

    web开发者可以在web.xml中使用context param激活profile属性

    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>/WEB-INF/app-config.xmlparam-value>
    context-param>
    <context-param>
        <param-name>spring.profiles.activeparam-name>
        <param-value>devparam-value>
    context-param>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    JVM 设置

    profiles属性也可以通过JVM系统参数设置,并在应用启动时激活相关属性

    -Dspring.profiles.active=dev
    
    • 1

    系统环境变量

    Unix系统中,profiles可以通过声明系统变量来激活

    export spring_profiles_active=dev
    
    • 1

    Maven 设置

    Spring profiles属性通过maven配置文件声明激活。

    <profiles>
        <profile>
            <id>devid>
            <activation>
                <activeByDefault>trueactiveByDefault>
            activation>
            <properties>
                <spring.profiles.active>devspring.profiles.active>
            properties>
        profile>
        <profile>
            <id>prodid>
            <properties>
                <spring.profiles.active>prodspring.profiles.active>
            properties>
        profile>
    profiles>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在编译打包时,通过以下动态参数传递,直接指定profile属性,开发不需要改动任何代码。这种方式在实际开发中经常使用,编译打包完成后,直接交付给运维团队

    mvn clean package -Pprod
    
    • 1

    Profiles In Test

    开发测试时,使用@ActiveProfile注解指定需要激活的profile。

    @ActiveProfiles("dev")
    
    • 1

    目前为止,已知多种方式激活profile属性,它们的优先级,从高到低分别为:

    • Context parameter in web.xml
    • WebApplicationInitializer
    • JVM System parameter
    • Environment variable
    • Maven profile

    默认Profile

    如果没有指定profile,Spring激活默认的profile - default, 可以在属性文件值修改 spring.profiles.default 的值,从而修改默认profile的名字

    spring.profiles.default=none
    
    • 1

    获取生效的Profiles

    Spring通过@Profile注解激活/禁止beans, 但是开发者希望获取生效的Profiles列表。有两种方式可以实现:

    • 使用 Environment 对象
    • 获取spring.profiles.active属性值

    使用 Environment

    通过注入Environment bean获取激活的profiles

    public class ProfileManager {
        @Autowired
        private Environment environment;
    
        public void getActiveProfiles() {
            for (String profileName : environment.getActiveProfiles()) {
                System.out.println("Currently active profile - " + profileName);
            }  
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    使用 spring.profiles.active

    此外,可以通过注入spring.profiles.active属性值来获取有效的profiles。

    public class ProfileManager {
        //如果配置多个属性,则覆盖get方法 迭代出每一个有效的profile
        @Value("${spring.profiles.active}")
        private String activeProfiles;
    
        public String getActiveProfiles() {
            for (String profileName : activeProfiles.split(",")) {
                System.out.println("Currently active profile - " + profileName);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    但是,如果应用中没有profile,上述代码,由于缺少配置将会导致应用启动失败,为了避免这种情况,可以定义个 kong的字符串作为默认值。

    //@Value("${spring.profiles.active}")
    @Value("${spring.profiles.active:}")
    private String activeProfile;
    
    • 1
    • 2
    • 3

    Spring Boot Profiles

    Spring Boot 支持以上所有的功能,并增加了一些额外的特性。

    设置Profiles

    在Spring Boot 默认的配置文件 - application.properties 激活

    spring.profiles.active=dev
    
    • 1

    通过启动类设置profile

    //setAdditionalProfiles 不是静态方法,在实际使用中需要注意
    SpringApplication.setAdditionalProfiles("dev");
    
    • 1
    • 2

    使用spring-boot-maven-plugin插件

    <plugins>
        <plugin>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-maven-pluginartifactId>
            <configuration>
                <profiles>
                    <profile>devprofile>
                profiles>
            configuration>
        plugin>
        ...
    plugins>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    执行maven命令

    mvn spring-boot:run
    
    • 1

    Profile-specific Properties Files

    Spring Boot 核心特性之一是定义了基于profile的配置文件解析规则。配置文件必须以-{profile}.properties格式命名。Spring Boot自动加载解析application.properties文件,并根据profile指定,加载特定的 -{profile}.properties 文件。

    例如,需要配置开发/生产两种数据源,名称分别为application-dev.properties、application-production.properties。

    application-production.properties使用MYSQL数据源

    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/db
    spring.datasource.username=root
    spring.datasource.password=root
    
    • 1
    • 2
    • 3
    • 4

    application-dev.properties 使用内存数据库

    spring.datasource.driver-class-name=org.h2.Driver
    spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
    spring.datasource.username=sa
    spring.datasource.password=sa
    
    • 1
    • 2
    • 3
    • 4

    单文件配置

    为了简化不同环境的配置,开发者可以在同一个文件中定义所有属性,并使用分隔符来指定配置文件。

    my.prop=used-always-in-all-profiles
    #---
    spring.config.activate.on-profile=dev
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/db
    spring.datasource.username=root
    spring.datasource.password=root
    #---
    spring.config.activate.on-profile=production
    spring.datasource.driver-class-name=org.h2.Driver
    spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
    spring.datasource.username=sa
    spring.datasource.password=sa
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    Profile Group

    Spring Boot 2.4 添加的另一个特性 - Profile Group, 顾明思义,它允许开发者将类似的配置文件分组放置在一起。

    假设一个场景:需要为生产环境提供多个配置概要文件,例如,生产环境中的数据库proddb、调度程序的prodquartz。

    spring.profiles.group.production=proddb,prodquartz
    
    • 1
  • 相关阅读:
    python 中各类型介绍及相互转换 - list, array, tensor, dict, tuple, DataFrame
    只有程序员才能看懂的16张高端漫画
    flume框架原理
    更安全高效的文件传输工具,Ftrans国产FTP替代方案可以了解
    产品补丁包测试的基本流程
    【微服务】分布式基础概念
    一段JS去除畅言免费版广告
    【Spring面试】二、BeanFactory与IoC容器的加载
    【JAVA】java常见面试题——持续更新
    centos 安装 docker
  • 原文地址:https://blog.csdn.net/u013433591/article/details/127973203