1.环境与Profile
在开发软件的时候,有时候需要从一个环境迁移到另一个环境。比如在开发阶段我们使用的是dev的环境,在测试阶段使用的是product环境,这时我们就需要不同的配置。Spring同样也提供了类似的解决方案(在Spring3.1中引入了bean profile功能)。如下所示,是一个使用@Profile注解来实现的实例。
首先需要准备两个配置类:
// ProdConfiguration.java
@Configuration
@Profile("prod")
public class ProdConfiguration {
@Bean
public ProdE getProdE(){
ProdE prodE = new ProdE();
prodE.setContent("产品");
return prodE;
}
}
// DevConfiguration.java
@Configuration
@Profile("dev")
public class DevConfiguration {
@Bean
public DevE getDevE() {
DevE devE = new DevE();
devE.setContent("开发");
return devE;
}
}
再需要在初始化Servlet环境配置类(用于替换web.xml)中设置profile值(下划线)
public class HelloWorldInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(HelloWorldConfiguration.class);
ctx.setServletContext(container);
ctx.getEnvironment().setActiveProfiles("dev");
ServletRegistration.Dynamic servlet = container.addServlet(
"dispatcher", new DispatcherServlet(ctx));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
}
}
最后用一个Controller来测试一下:
@Controller
@RequestMapping("/")
public class HelloWorldController {
@Autowired(required=false)
ProdE prodE;
@Autowired(required=false)
DevE devE;
@RequestMapping(method = RequestMethod.GET)
public String test(ModelMap model) {
if(null!=prodE){
System.out.println("******"+prodE.getContent()+"******");
}else{
System.out.println("******prodE is null******");
}
if(null!=devE){
System.out.println("******"+devE.getContent()+"******");
}else{
System.out.println("******devE is null******");
}
model.addAttribute("greeting", "Hello World from Spring 4 MVC");
return "welcome";
}
}
当然也可以使用XML配置,如下所示:
<beans profile="prod">
<!-- 在这里配置bean -->
</beans>
<beans profile="dev">
<!-- 在这里配置bean -->
</beans>
从Spring3.2开始已经支持在方法级别上使用@Profile注解,与@Bean注解一起使用,这样就可以将这两种声明放置在同一个配置类中。
2.激活使用profile
如果不激活使用profile,你配置的profile也是不会被使用的,因为Spring不能确定哪个profile是需要使用的。Spring提供了两个独立的属性来构成一个比较合理的设置:spring.profiles.active和spring.profiles.default。如果设置了spring.profiles.active属性,则Spring会设置本值,如果spring.profiles.active没有设置,则使用spring.profiles.default的默认值。spring.profiles.active对应于注解中的ConfigurableEnvironment.setActiveProfiles(String... profiles);spring.profiles.default对应于注解中的ConfigurableEnvironment.setDefaultProfiles(String... profiles);我们可以有多种方式设置这两种属性:
- 作为DispatcherServlet的初始化参数
- 作为Web应用上下文条目参数
- 作为JNDI条目
- 作为环境变量
- 作为JVM的系统变量
- 在集成测试类上,使用@ActiveProfiles注解设置
XML中的配置如下所示:
<?xml version="1.0" encoding="ISO-8859-1"?>
<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">
<display-name>test</display-name>
<!-- - Location of the XML file that defines the root application context.
- Applied by ContextLoaderListener. -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application-config.xml</param-value>
</context-param>
<!-- 为上下文设置默认的profile -->
<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>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.spr