springMVC的多环境配置_基于springprofile

14 篇文章 0 订阅

记录一下springMVC项目的多环境的切换。基于springprofile。

一 简单实现

     1 首先将配置文件进行分离,分成development(本地环境)、test(测试环境)、production(正式环境) 

配置文件目录如下

common目录用来存放一些每个环境下都一样的配置文件

2 配置spring和springmvc配置文件最下面配置如下beans

 
<!-- 开发环境配置文件 -->
<beans profile="development">
    <context:property-placeholder
            location="classpath*:config_common/*.properties, classpath*:config_development/*.properties"/>
</beans>

<!-- 测试环境配置文件 -->
<beans profile="test">
    <context:property-placeholder
            location="classpath*:config_common/*.properties, classpath*:config_test/*.properties"/>
</beans>

<!-- 生产环境配置文件 -->
<beans profile="production">
    <context:property-placeholder
            location="classpath*:config_common/*.properties, classpath*:config_production/*.properties"/>
</beans>

3 配置web.xml

<!-- 多环境配置 在上下文context-param中设置profile.default的默认值 -->
<context-param>
    <param-name>spring.profiles.default</param-name>
    <param-value>production</param-value>
</context-param>

<!-- 多环境配置 在上下文context-param中设置profile.active的默认值 -->
<!-- 设置active后default失效,web启动时会加载对应的环境信息 -->
<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>test</param-value>
</context-param>

这样启动的时候就可以按照切换spring.profiles.active的属性值来进行切换了

也有更便捷的方法,通过jvm启动时设置属性spring.profiles.active或者依赖于maven 的profile来打包,可自行百度

二 扩展项目中出现加载配置文件情况

项目中可能会出现这样的情况,比如配置一些上传文件的路径 FTP的信息 以及一些回调接口;这类配置信息不是向数据库配置文件 redis那样在项目启动时加载,而是在类里通过ResourceBundle 或是ClassLoder加载 

1.写个listener实现 ServletContextListener接口 

package cn.jeeweb.core.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * @ClassName:myContextListener
 * @Description:获取webxml配置的环境用于PropertiesUtil工具类中动态导入.properties文件
 * @Created by wenbin.li on 2018/10/18
 */
public class ProfileContextListener implements ServletContextListener {

    public static String profile;

    @Override
    public void contextInitialized(ServletContextEvent sce) {

        profile = sce.getServletContext().getInitParameter("spring.profiles.active");
        if (profile == null || profile.equals("")) {
            profile = sce.getServletContext().getInitParameter("spring.profiles.default");
        }
        profile = "config_" + profile + "/";
        //动态改变log4j的配置文件
        //  String log4jConfigLocation = sce.getServletContext().getInitParameter("log4jConfigLocation");
        //  sce.getServletContext().setInitParameter("log4jConfigLocation", "classpath:"+profile + "log4j.properties");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}

2.修改web.xml 

<!--多环境配置 监听器-->
<listener>
    <listener-class>cn.jeeweb.core.listener.ProfileContextListener</listener-class>
</listener>

3.在加载配置文件时判断一下不是common下面的配置文件 就直接通过listener里的静态变量profile拼接路径获取就行

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值