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

 更新时间:2023年09月11日 15:39:30   作者:回炉重造P  
这篇文章主要介绍了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环境后的不同输出结果:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

可以看出@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());

切换环境输出结果

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

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>

3.如果是springMVC管理,可以在DispatcherServlet的配置中增加init-param:

<init-param>
    <param-name>spring.profiles.alive</param-name>
    <param-value>dev</param-value>
</init-param>

4.可以在jvm的运行属性中设置,tomcat等服务器的启动option也可设置jvm属性。

-Dspring.profiles.active=dev

5.对测试类使用注解 @ActiveProfiles 进行修饰,value设置为对应的环境。 总结

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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • SpringBoot + Disruptor实现特快高并发处理及使用Disruptor高速实现队列的过程

    SpringBoot + Disruptor实现特快高并发处理及使用Disruptor高速实现队列的过程

    Disruptor是一个开源的Java框架,它被设计用于在生产者—消费者(producer-consumer problem,简称PCP)问题上获得尽量高的吞吐量(TPS)和尽量低的延迟,这篇文章主要介绍了SpringBoot + Disruptor 实现特快高并发处理,使用Disruptor高速实现队列,需要的朋友可以参考下
    2023-11-11
  • 使用Java将实体类转换为JSON并输出到控制台的完整过程

    使用Java将实体类转换为JSON并输出到控制台的完整过程

    在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用JSON格式,用Java将实体类转换为JSON格式并输出其实不难,只需掌握几个步骤就可以做到!接下来,我们来看看这一过程究竟是如何实现的,感兴趣的小伙伴跟着小编一起来看看吧
    2025-05-05
  • Spring中使用atomikos+druid实现经典分布式事务的方法

    Spring中使用atomikos+druid实现经典分布式事务的方法

    这篇文章主要介绍了Spring中使用atomikos+druid实现经典分布式事务的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-06-06
  • Java构建对象常用3种方法解析

    Java构建对象常用3种方法解析

    这篇文章主要介绍了Java构建对象常用3种方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • 将本地服务注册到nacos上的实现过程

    将本地服务注册到nacos上的实现过程

    文章介绍了如何在本地安装和启动Nacos服务,并配置本地应用程序使用Nacos进行服务注册,文章强调了启动日志窗口的重要性,并提供了一个简短的总结,希望对读者有所帮助
    2026-01-01
  • Java入门教程--带包的类如何编译与运行

    Java入门教程--带包的类如何编译与运行

    我们一般都是通过IDE(如Eclipse、Intellij Idea,STS等)来开发,调试java项目。在不借助IDE的情况下,如何编译、运行Java程序。打包编译时,会自动创建包目录,不需要自己新建包名文件夹。
    2022-12-12
  • Java基础知识总结之继承

    Java基础知识总结之继承

    这一篇我们来学习面向对象的第二个特征——继承,文中有非常详细的基础知识总结,对正在学习java的小伙伴们很有帮助,需要的朋友可以参考下
    2021-06-06
  • SocketIo+SpringMvc实现文件的上传下载功能

    SocketIo+SpringMvc实现文件的上传下载功能

    这篇文章主要介绍了SocketIo+SpringMvc实现文件的上传下载功能,socketIo不仅可以用来做聊天工具,也可以实现局域网。文中给出了实现代码,需要的朋友可以参考下
    2018-08-08
  • JAVA 对接腾讯云直播的实现

    JAVA 对接腾讯云直播的实现

    这篇文章主要介绍了JAVA 对接腾讯云直播的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • java 抽象类与接口的区别总结

    java 抽象类与接口的区别总结

    这篇文章主要介绍了java 抽象类与接口的区别总结的相关资料,需要的朋友可以参考下
    2017-02-02

最新评论