激活profile
你要是爬山,就爬到底,一跌倒就会跌到深渊里。
Spring确定哪个profile处于激活状态时,需要依赖两个独立的属性:spring.profiles.active和spring.profiles.default。如果设定
spring.profiles.active,那么它的值就会用来确定哪个profile是激活状态的,如果没有设定,那么Spring会查spring.profiles.default的值。
有多种方式设置这两个属性:
1) 作为DispatcherServlet的初始化参数
2) 作为Web应用的上下文参数
3) 作为JNDI的条目
4) 作为环境变量
5) 作为JVM的系统属性
6) 在集成测试类上,使用@ActiveProfiles注解设置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- Web应用的上下文设置默认的profile --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param> <context-param> <param-name>spring.profiles.default</param-name> <param-value>dev</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 作为DispatcherServlet的初始化参数 --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>spring.profiles.default</param-name> <param-value>dev</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>