类似于Maven Profile,通过设置参数区分不同环境加载不同的类或资源文件
@Configuration
public class App
{
public static void main( String[] args )
{
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().setActiveProfiles("dev");
ctx.scan("SpringTest");
ctx.refresh();
UserService service = ctx.getBean(UserService.class);
System.out.println(service.getName());
}
@Bean
@Profile("dev")
public UserService userService(){
UserService service = new UserService();
service.setName("dev bean");
return service;
}
@Bean
@Profile("prod")
public UserService userService2(){
UserService service = new UserService();
service.setName("prod bean");
return service;
}
}
网友评论