Spring Profile与PropertyPlaceholderConfigurer实现项目多环境配置切换

这篇具有很好参考价值的文章主要介绍了Spring Profile与PropertyPlaceholderConfigurer实现项目多环境配置切换。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

最近考虑项目在不同环境下配置的切换,使用profile注解搭配PropertyPlaceholderConfigurer实现对配置文件的切换,简单写了个demo记录下实现。

基本知识介绍

@Profile

@Profile通过对bean进行修饰,来限定spring在bean管理时的初始化情况,只有环境中激活的profile状态和修饰的value值对应时,该bean才会被顺利加载并管理。

PropertyPlaceholderConfigurer

PropertyPlaceholderConfigurer是PlaceholderConfigurerSupport的实现类,是spring提供的一个解析yml或properties配置文件并将对应的值进行映射,通过${}形式进行调用的配置读取类。举例来说,配置文件中akb.num=48,那么在bean或配置文件中使用${akb.num}即可获取配置的值48。

简单实现

profile修饰的bean实例在不同环境下的切换

首先定义bean接口与default、dev、prob三种情况下的bean。
接口 DeafultProfileBean

@Component
public interface DefaultProfileBean {
    String getInfo();
}

default bean ProfileBeanDefault

@Profile("default")
@Component
public class ProfileBeanDefault implements DefaultProfileBean{
    @Override
    public String getInfo() {
        return "这是default状态下的bean";
    }
}

dev bean ProfileBeanDev

@Profile("dev")
@Component
public class ProfileBeanDev implements DefaultProfileBean{
    @Override
    public String getInfo(){
        return "这是dev环境使用的bean";
}
}

dev bean ProfileBeanProd

@Profile("prod")
@Component
public class ProfileBeanProd implements DefaultProfileBean{
    @Override
    public String getInfo() {
        return "这是prod环境使用的bean";
}
}

加载上下文并输出加载的bean结果

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        // 设置profile环境
        context.getEnvironment().setActiveProfiles("dev");
        context.scan("com.huiluczP");
        context.refresh();
        System.out.println("loading success");

        DefaultProfileBean bean = context.getBean(DefaultProfileBean.class);
        System.out.println(bean.getInfo());

切换profile环境后的不同输出结果:
Spring Profile与PropertyPlaceholderConfigurer实现项目多环境配置切换,java,spring,spring,java,后端
Spring Profile与PropertyPlaceholderConfigurer实现项目多环境配置切换,java,spring,spring,java,后端
Spring Profile与PropertyPlaceholderConfigurer实现项目多环境配置切换,java,spring,spring,java,后端
可以看出@Profile注解生效了。

配置类和配置文件

SpringConfigure配置类

@Configuration
public class SpringConfigure {
    @Bean
    @Profile("default")
    // 默认状态配置加载类
    public PropertyPlaceholderConfigurer defaultConfig(){
        PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
        Resource resource = new ClassPathResource("config/default.properties");
        ppc.setLocation(resource);
        return ppc;
    }

    @Bean
    @Profile("dev")
    // dev状态配置加载类
    public PropertyPlaceholderConfigurer devConfig(){
        PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
        Resource resource = new ClassPathResource("config/dev.properties");
        ppc.setLocation(resource);
        return ppc;
    }

    @Bean
    @Profile("prod")
    // prod状态配置加载类
    public PropertyPlaceholderConfigurer prodConfig(){
        PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
        Resource resource = new ClassPathResource("config/prod.properties");
        ppc.setLocation(resource);
        return ppc;
    }

}

管理了三个PropertyPlaceholderConfigurer类型的配置读取类,分别对应不同的profile状态。通过ClassPathResource读取对应的配置文件,如果用xml配置文件进行PropertyPlaceholderConfigurer bean的管理,直接增加property location,将value设置为对应的配置文件地址即可。

三个不同的配置文件内容:

default.properties

config.info=default information

dev.properties

config.info=dev information

prod.properties

config.info=prod information

配置获取测试接口和bean类

public interface DefaultConfigBean {
    String getConfigInfo();
}
@Component
public class DifferentConfigBean implements DefaultConfigBean{

    @Value("${config.info}")
    private String config;

    @Override
    public String getConfigInfo() {
        return "当前环境下的config信息为:" + config;
    }
}

通过${config.info}实现对配置文件的获取。

加载上下文进行处理

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        // 设置profile环境
        context.getEnvironment().setActiveProfiles("prod");
        context.scan("com.huiluczP");
        context.refresh();
        DefaultConfigBean configBean = context.getBean(DefaultConfigBean.class);
        System.out.println(configBean.getConfigInfo());

切换环境输出结果:

Spring Profile与PropertyPlaceholderConfigurer实现项目多环境配置切换,java,spring,spring,java,后端
Spring Profile与PropertyPlaceholderConfigurer实现项目多环境配置切换,java,spring,spring,java,后端
Spring Profile与PropertyPlaceholderConfigurer实现项目多环境配置切换,java,spring,spring,java,后端

profile激活

特别的,说明下对项目profile环境怎么进行设置以对profile进行激活。没有特别指定时,默认调用default修饰的bean。

  1. 直接上下文设定,也就是上文中使用的,对enviroment中的activeProfile进行设置即可。
  2. web项目中可以在web.xml中设置全局的变量:
<context-param>
        <param-name>spring.profiles.alive</param-name>
        <param-value>dev</param-value>
</context-param>
  1. 如果是springMVC管理,可以在DispatcherServlet的配置中增加init-param:
<init-param>
    <param-name>spring.profiles.alive</param-name>
    <param-value>dev</param-value>
</init-param>
  1. 可以在jvm的运行属性中设置,tomcat等服务器的启动option也可设置jvm属性。
-Dspring.profiles.active=dev
  1. 对测试类使用注解@ActiveProfiles进行修饰,value设置为对应的环境。

总结

简单记录了一下spring profile和PropertyPlaceholderConfigurers类实现不同环境下的不同配置文件加载的方法,在分支中进行快速切换还是挺方便的,而且PropertyPlaceholderConfigurer映射的配置在spring读取其他的配置文件时也可以通过${}进行读取,这样不同的环境配置文件只需要一份,并将其中需要变动的部分用PropertyPlaceholderConfigurer进行管理即可。文章来源地址https://www.toymoban.com/news/detail-651175.html

到了这里,关于Spring Profile与PropertyPlaceholderConfigurer实现项目多环境配置切换的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • linux profile文件环境变量配置

    profile 文件位于/etc/目录下 /etc/profile , 当登入系统时候获得一个 shell 进程时,其读取环境profile 文件时候会读取,/etc/bash.bashrc,/etc/profile.d 文件下配置的sh文件,所以我们也可以在profile.d 和bash.bashrc 目录下创建sh文件,配置环境变量 profile,路径:/etc/profile,用于设置系统级

    2024年02月05日
    浏览(39)
  • SpringBoot 通过@Profile注解配置多环境

    参考资料 Springboot中的@Profile注解 在Spring中,可以使用 配置文件 的方式来指定不同环境下所需要的配置信息 ⏹application.yml ⏹application-dev.yml ⏹application-product.yml 但有时候,我们不通过配置文件,而是通过配置类的方式来指定不同环境下的配置信息, 此时就需要用到 @Profile注

    2024年02月09日
    浏览(28)
  • SpringBoot多环境配置Maven Profile组

    Maven profile组 注意切换配置时   mvn clean下  或者 clean 加install 或者compile 编译 clean之后  install下 或者compile 编译 nohup java -Xms256m -Xmx512m -Dfile.encoding=UTF-8 -jar demo.jar --spring.profiles.active=prod system.log 21

    2024年02月02日
    浏览(32)
  • mac 配置环境变量 .bash_profile文件

    打开.bash_profile文件 open -e ~/.bash_profile 编辑:增减自己的各软件配置 export M2_HOME=/Users/lizhen/apache-maven-3.5.4 export PATH=$PATH:$M2_HOME/bin PATH=$PATH:/usr/local/mysql/bin # JAVA JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-18.0.2.jdk/Contents/Home # Android SDK export ANDROID_HOME=/Users/lizhen/Library/Android/sdk export PATH

    2024年02月06日
    浏览(32)
  • 解析Spring Boot中的Profile:配置文件与代码的双重掌控

    基于 Spring Boot 创建 Web 应用程序的方法有很多,我们选择在idea中直接进行创建,服务器URL选择Spring Initializer 网站,类型选择Maven项目,java版本根据jdk版本进行选择。 然后添加相应依赖以及选择spring boot版本 接下来我们写一个Controller 现在我们需要对这个应用程序进行打包,使

    2024年02月09日
    浏览(35)
  • MacOS环境配置 .zshrc .bashrc .bash_profile

    每当学习一门新技术的时候,其中一个最大的拦路虎就是环境安装配置,比如java,安卓,比如php都需要安装和配置一大堆工具,安装不顺利的时候无疑会在我们的热情上浇一头冷水。这不,最近打算学习Flutter开发,光是安装配置就捣鼓了好几天,现在记录下来Mac环境变量的

    2023年04月24日
    浏览(37)
  • Mac环境配置(Java)----使用bash_profile进行配置

    目录  一、下载Java 1、官方地址: 2、网盘下载地址(Java8): 二、Java环境配置(以Java8配置为例,所有版本配置方法一样) 1、打开软件--终端 2、首先查看本机Java的安装地址(系统默认的) 3、查看到Java8安装的路径如下:  4、如果是第一次配置环境变量,使用命令:【t

    2024年02月16日
    浏览(32)
  • SpringBoot - 配置文件application.yml使用详解(附:Profile多环境配置)

    SpringBoot - 配置文件application.yml使用详解(附:Profile多环境配置) 1,基本介绍 (1)YAML 是 JSON 的超集,简洁而强大,是一种专门用来书写配置文件的语言,可以替代 application.properties。 (2)在创建一个 SpringBoot 项目时,引入的 spring-boot-starter-web 依赖间接地引入了 s

    2024年02月08日
    浏览(38)
  • Spring Cloud配置application.yml与bootstrap.yml区别及多profile配置 | Spring Cloud 6

    Spring Cloud 构建于 Spring Boot 之上,在 Spring Boot 中有两种上下文,一种是 bootstrap ,另外一种是 application 。 bootstrap.yml/bootstrap.properties 和 application.yml/application.yml 都可以用来配置参数。 bootstrap.yml/bootstrap.properties :用来程序引导时执行,应用于更加早期配置信息读取。可以理解

    2024年02月09日
    浏览(42)
  • VSCode 配置 Spring Boot 项目开发环境

    神器IDEA在升级到2023之后越发卡顿, EDU邮箱也不能用了, 照现在这个JDK版本的升级速度, 神器不升级也不行, 需要开始物色替代品. 其它IDE我用得少, VSCode还是比较熟悉的, 可以作为备选项. 两三年前曾经试过配置Java环境, 存在不少问题作罢. 最近搜了下相关的文章, 感觉VSCode对Ja

    2024年03月10日
    浏览(51)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包