前言
日常的开发过程中,一个项目的上线通常会经过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;
}
}
根据不同的配置,请求该接口,会返回不同配置文件所配置的值。