美文网首页
Spring Profile Test

Spring Profile Test

作者: 风雨楼兰 | 来源:发表于2018-01-13 10:43 被阅读0次

    类似于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;
        }
    }
    

    相关文章

      网友评论

          本文标题:Spring Profile Test

          本文链接:https://www.haomeiwen.com/subject/okqcoxtx.html