设为首页 加入收藏

TOP

Spring IOC官方文档学习笔记(十三)之环境概要(一)
2023-07-25 21:40:38 】 浏览:75
Tags:Spring IOC 方文档 习笔记 十三

1.profiles

(1) profiles提供了一种在不同环境(Environment)下注册不同的bean的机制,如下

//假定现在我们存在开发和生产两个环境,每种环境下ExampleA所需的url都是不同的,那么我们就可以使用@Profile注解,来声明在哪种环境下该注入哪种bean
@Configuration
public class Config {

    //development环境下注入该bean
    @Bean
    @Profile("development")
    public ExampleA exampleAForDevelopment() {
        return new ExampleA("http://127.0.0.1:8080");
    }

    //production环境下注入该bean
    @Bean
    @Profile("production")
    public ExampleA exampleAForProduction() {
        return new ExampleA("http://192.168.7.70:8080");
    }
}

@Profile属性值不仅可以为一个字符串值,亦可以为一个表达式,规则如下:

  • ! : 逻辑非

  • | : 逻辑或

  • & : 逻辑与

@Configuration
public class Config {
    //使用逻辑或,声明在development或test环境下注入这个bean ExampleA
    @Bean
    @Profile("development | test")
    public ExampleA exampleAForDevelopment() {
        return new ExampleA("http://127.0.0.1:8080");
    }
    
    //声明在production环境下且online或offline中至少某一种环境被激活下注入这个bean
    @Bean
    @Profile("production & (online | offline)")
    public ExampleA exampleAForProduction() {
        return new ExampleA("http://192.168.7.70:8080");
    }
}

//例一:只声明profile为production,观察打印结果,可见未有任何ExampleA被注入到容器中
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
//使用Environment中的setActiveProfiles方法来设置激活哪一个profile
ctx.getEnvironment().setActiveProfiles("production");
ctx.register(Config.class);
ctx.refresh();
Arrays.stream(ctx.getBeanDefinitionNames()).forEach(System.out::println);

//例二:指定多个配置信息,观察打印结果,可见两个ExampleA都被注入到容器中
//setActiveProfiles方法可同时激活多个profile
ctx.getEnvironment().setActiveProfiles("production", "online", "test");

(2) @Profile注解可用作元注解,用于创建自定义组合注解

//下面这个@Production注解,等价于@Profile("production")注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Profile("production")
public @interface Production {
}

(3) 对于重载的@Bean方法,@Profile会先校验其中的第一个方法,如果它不通过,则后面所有它的重载方法也全部不通过,否则,在上一个重载方法通过后,它才会继续接下来的剩余重载方法的校验,例子如下

//该Config类中有两个重载方法exampleA,它们的profile属性值不相同
@Configuration
public class Config {
    @Bean
    @Profile("dev")
    public ExampleA exampleA(@Value("aaa") String str) {
        return new ExampleA();
    }

    @Bean
    @Profile("prod")
    public ExampleA exampleA() {
        return new ExampleA();
    }
}

//先设置active-profile为prod,如下,启动容器,会发现容器抛出NoSuchBeanDefinitionException异常,这就是因为容器先校验了exampleA(String str)这个方法,发现它的profile为dev,不符,因此,后面的它的重载方法exampleA()也直接不通过,故而没有一个ExampleA实例被装入到容器中
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().setActiveProfiles("prod");
ctx.register(Config.class);
ctx.refresh();
System.out.println(ctx.getBean(ExampleA.class));

//对于上面的例子,我们只颠倒Config类中两个方法的位置,如下
@Configuration
public class Config {
    @Bean
    @Profile("prod")
    public ExampleA exampleA() {
        return new ExampleA();
    }

    @Bean
    @Profile("dev")
    public ExampleA exampleA(@Value("aaa") String str) {
        return new ExampleA();
    }
}

//然后,再次启动容器,会发现我们获得了一个ExampleA实例,这就是因为容器先校验了exampleA(),其profile值与active-profile值一致,通过,接下来校验exampleA(String str),结果不通过,因此会有一个ExampleA实例被注入到容器中

(4) 在基于xml的配置中,我们可以指定<beans/>

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇自定义异常 下一篇为什么95%的Java程序员人,都是用..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目