标签:
Profile为开发环境和生产环境下使用不同的配置(如:数据库的配置)提供了支持。
如DemoBean,和普通Bean一样(可根据构造函数的传参不同,提供不同的数据库配置);
package com.wisely.highlight_spring4.ch2.profile;
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;
}
}
@Configuration
public class ProfileConfig{
@Bean
@Profile("dev")//开发环境
public DemoBean devDemoBean(){
return new DemoBean("from development profile");
}
@Bean
@Profile("prod")//生产环境
public DemoBean prodDemoBean(){
return new DemoBean("from production profile");
}
}
public class Main{
public static void main(String[] args){
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("prod");//将活动的Profile设置为生产环境
context.register(ProfileConfig.class);//后置注册Bean配置类,不然会报Bean未定义的错误
context.refresh();//刷新容器
DemoBean demoBean=context.getBean(DemoBean.class);
System.out.println(demoBean.getContent());
context.close();
}
}
标签:
原文地址:http://www.cnblogs.com/quickcodes/p/Spring-Boot-jiao-cheng7Profile.html