一、点睛
Profile为在不同的环境下使用不同的配置提供了支持(开发环境下的配置和生产环境下的配置肯定是不同的,例如数据库的配置)。
1、通过设定Environment的ActiveProfile来设定当前Context需要使用配置环境。在开发中使用@Profile注解类或者方法,达到在不同的情况下选择实例化不同的Bean。
2、通过设定jvm的spring.profiles.active参数来设定配置环境;
Profile为在不同的环境下使用不同的配置提供了支持(开发环境下的配置和生产环境下的配置肯定是不同的,例如数据库的配置)。
1、通过设定Environment的ActiveProfile来设定当前Context需要使用配置环境。在开发中使用@Profile注解类或者方法,达到在不同的情况下选择实例化不同的Bean。
2、通过设定jvm的spring.profiles.active参数来设定配置环境;
3、web项目设置在Servlet的context parameter中
Servlet2.5及以下:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>spring.profiles</param-name>
<param-value>production</param-value>
</init-param>
</servlet>
Servlet3.0及以上
public class WebInit inplements WebApplicationInitializer{
@Override
public void onStarup(ServletContext container) throws ServletException{
container,serInitParameter("spring.profiles.default","dev")
}
}
二、示例
public class DemoBean{
private String content;
public DemoBean(String content){
super();
this.content=content
}
public String getContent(){
return content;
}
public void setContent(String content){
this.content=content;
}
}
Servlet2.5及以下:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>spring.profiles</param-name>
<param-value>production</param-value>
</init-param>
</servlet>
Servlet3.0及以上
public class WebInit inplements WebApplicationInitializer{
@Override
public void onStarup(ServletContext container) throws ServletException{
container,serInitParameter("spring.profiles.default","dev")
}
}
二、示例
public class DemoBean{
private String content;
public DemoBean(String content){
super();
this.content=content
}
public String getContent(){
return content;
}
public void setContent(String content){
this.content=content;
}
}
(2)、Profile配置
@Configuration
public class ProfileConfig{
@Bean
@Profile("dev")//Profile为dev时实例化devDemoBean
public DemoBean devDemoBean(){
return new DemoBean("from development profile")
}
@Bean
@Profile("prod")//Profile为prod时实例化prodDemoBean
public DemoBean prodDemoBean(){
return new DemoBean("from production")
}
}
@Configuration
public class ProfileConfig{
@Bean
@Profile("dev")//Profile为dev时实例化devDemoBean
public DemoBean devDemoBean(){
return new DemoBean("from development profile")
}
@Bean
@Profile("prod")//Profile为prod时实例化prodDemoBean
public DemoBean prodDemoBean(){
return new DemoBean("from production")
}
}
(3)运行
public class main{
public static void main( String[] args){
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("prod");
context.register(ProfileConfig.class);
context.refresh();
DemoBean demebean =context.getBean(DemoBean.class);
System.out.print(demoBean.getContext());
context.close();
}
}
代码解释:
1 、先将活动的Profile设置为prod
2、后置注册的Bean配置类,不然会报Bean未定义的错误。
3、刷新容器。
public class main{
public static void main( String[] args){
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("prod");
context.register(ProfileConfig.class);
context.refresh();
DemoBean demebean =context.getBean(DemoBean.class);
System.out.print(demoBean.getContext());
context.close();
}
}
代码解释:
1 、先将活动的Profile设置为prod
2、后置注册的Bean配置类,不然会报Bean未定义的错误。
3、刷新容器。