Spring——环境切换

前言

    日常的开发过程中,一个项目的上线通常会经过dev、sit、pre、prd四个阶段,不同阶段的服务器地址、数据库配置都可能不同,为了方便在不同环境下配置的轻松切换。Spring提供了Profile的解决方案。

 

实现

方式一:基于Java配置

/**
 * 功能描述:实体类
 *
 * @author menghao
 * @date 2017/9/5
 */
public class UserBean {

    private String name;

    public UserBean(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

}
/**
 * 功能描述:配置类
 *
 * @author menghao
 * @date 2017/9/5
 */
public class UserConfig {

    @Bean
    @Profile("dev")
    public UserBean devUserBean() {
        return new UserBean("测试环境");
    }

    @Bean
    @Profile("prd")
    public UserBean prdUserBean() {
        return new UserBean("生产环境");
    }
    
}
/**
 * 功能描述:测试类
 *
 * @author menghao
 * @date 2017/9/5
 */
public class EnvironmentTest {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        //context.getEnvironment().setActiveProfiles("dev");
        context.getEnvironment().setActiveProfiles("prd");
        context.register(UserConfig.class);
        context.refresh();
        UserBean userBean = context.getBean(UserBean.class);
        System.out.println(userBean.getName()); 
        context.close();
    }
}

    通过setActiveProfiles设置不同的环境,会获取对应环境的bean。

 

方式二:基于xml配置

//setting-dev.properties
env.name = dev

//setting-prd.properties
env.name = prd

 spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
   	http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
   	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.menghao.springboot.environment"/>

    <beans profile="dev">
        <context:property-placeholder location="classpath:environment/setting-dev.properties"/>
    </beans>
    <beans profile="prd">
        <context:property-placeholder location="classpath:environment/setting-prd.properties"/>
    </beans>

</beans>

 web.xml:

<!--没有激活其他环境的默认环境-->
<context-param>
    <param-name>spring.profiles.default</param-name>
    <param-value>dev</param-value>
</context-param>


<!--可以在DispatcherServlet中配置激活-->
<servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/spring-servlet.xml</param-value>
    </init-param>
    <init-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>prd</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

JVM参数:

//可以在服务器启动时配置激活
-Dspring.profiles.active="prd"
/**
 * 功能描述:测试Controller
 *
 * @author menghao
 * @date 2017/9/5
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Value("${env.name}")
    private String name;

    @GetMapping
    @ResponseBody
    public String user(){
        return name;
    }
}

    根据不同的配置,请求该接口,会返回不同配置文件所配置的值。

转载于:https://my.oschina.net/marvelcode/blog/1529445

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值