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;
}
}
(2)Profile配置
package com.wisely.highlight_spring4.ch2.profile;
@Configuration
public class ProfileConfig {
@Bean
@Profile("dev") //1 先将活动的Profile设置为prod
public DemoBean devDemoBean() {
return new DemoBean("from development profile");
}
@Bean
@Profile("prod") //2 后置注册Bean配置类,不然会报Bean未定义的错误。
public DemoBean prodDemoBean() {
return new DemoBean("from production profile");
}
}
(3)运行
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("prod"); //1
context.register(ProfileConfig.class);//2
context.refresh(); //3 刷新容器
DemoBean demoBean = context.getBean(DemoBean.class);
System.out.println(demoBean.getContent());
context.close();
}
}