Spring IOC官方文档学习笔记(十三)之环境概要,
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/>标签中profile属性的值,来进行配置,如下
<!-- 相当于在@Configuration类上添加了@Profile("prod")注解 -->
<beans profile="prod" ....>
<!-- bean的定义... -->
</beans>
<!-- 在xml的配置中,profile的属性值不能使用像注解那样的表达式,比如前面的表达式:production & (online | offline) 是不能用于xml中的,不过xml支持 ! 运算符 -->
<beans profile="!prod" ....>
<!-- bean的定义... -->
</beans>
<!-- 虽然xml中不支持表达式,但为了表达出 &(与) 的效果,我们可以这样声明 -->
<beans ...>
<!-- 等价于@Profile("prod & online") -->
<beans profile="prod">
<beans profile="online">
<!-- bean的定义... -->
</beans>
</beans>
</beans>
(5) 在前面的例子中,我们已经看到了,可通过Environment中的setActiveProfiles()方法来选择激活某一个或多个profile,除此之外,我们还可以通过配置spring.profiles.active的属性值来声明激活哪些profile,这个spring.profiles.active可在springboot项目中的yml配置文件中进行配置,或通过jvm系统参数来进行配置,如下
(6) profile的默认属性值为default,可通过Environment中的setDefaultProfiles()方法或指定spring.profiles.default的属性值来进行修改
//@Profile("default")代表启用默认配置,即如果当前Environment中没有任何profile被指定,那么这个bean就会被添加到容器中,反之,如果指定了任何profile,那么这个bean就会被排除在外
@Configuration
@Profile("default")
public class Config {
@Bean
public ExampleA exampleA() {
return new ExampleA();
}
}
2.PropertySource概要
(1) PropertySource是对配置参数的抽象,Spring会将我们的JVM系统变量和系统环境变量抽象成PropertySource实例,然后,当我们想要获得某个配置参数的值时,Spring便会在这些PropertySource实例上进行搜索,如下
首先通过IDEA配置一个JVM系统变量my-property,其值为boke
接着启动容器,如下,观察打印结果,会发现可以获取到我们所配置的JVM系统变量my-property的值
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
Environment env = ctx.getEnvironment();
System.out.println(env.getProperty("my-property"));
(2) 我们还可以获取到操作系统中的变量,如下,在用户变量中配置我们我们的属性值
接着,重启IDEA,再启动容器,观察打印结果,会发现已经获取到了我们的系统变量的值,当然,这个前提是(1)中我们所向configurations中Environment variables配置的变量已被删除,否则,存在两个相同的配置时,以Environment variables中的配置值优先(即JVM系统变量的优先级高于系统环境变量)
3.使用@PropertySource注解
(1) 在一般情况下,我们会将我们的配置项写进一个专门的配置文件中,而不会像上面中的例子那样去设置JVM系统变量或系统环境变量,当设置好了后,便可使用@PropertySource注解来读取我们所定义的配置文件了,如下所示
首先在resources目录下新建一个application.properties文件,其包含一个配置项如下
user=aaa
public class ExampleA {
public ExampleA(String s) {
System.out.println(s);
}
}
//在我们的Configuration类上使用@PropertySource注解,指定配置文件的路径(此处采用类路径),那么Spring便会加载解析这个配置文件,之后,从Environment中获取到我们所定义的配置值
@Configuration
@PropertySource("classpath:/application.properties")
public class Config {
@Autowired
private Environment environment;
@Bean
public ExampleA exampleA() {
return new ExampleA(environment.getProperty("user"));
}
}
//当然,更一般的,我们会采用@Value注解来读取配置文件中的配置项,它使用了${ }语法,其中大括号中的值为我们所需的键值
@Configuration
@PropertySource("classpath:/application.properties")
public class Config {
@Value("${user}")
private String username;
@Bean
public ExampleA exampleA() {
return new ExampleA(username);
}
}
(2) ${ }语法还可用于更一般的场景,如下所示
首先在resources目录下新建一个文件夹为boke,将我们的application.properties配置文件迁移到此文件夹下
---resources
|---boke
|---application.properties
接着再通过IDEA配置一个JVM系统变量mypath,其值为boke
然后我们就可以使用${ }来充当占位符,如下@PropertySource注解中的属性值存在一个占位符${mypath},在容器启动后,Spring会从环境变量中解析这个占位符,将其替换为真正的值,此处为boke
@Configuration
@PropertySource("classpath:/${mypath}/application.properties")
public class Config {
@Value("${user}")
private String username;
@Bean
public ExampleA exampleA() {
return new ExampleA(username);
}
}
用户点评