第2章:spring中的Bean/2.6 环境参数和占位符/2.6.1 环境参数/2.6.1.1 概念
第2章:spring中的Bean/2.6 环境参数和占位符/2.6.1 环境参数/2.6.1.2 参数设置
- JAVA设置环境变量,比如:System.setProperty("参数名","参数值");比如:
- 从JVM参数中解析,比如:-DParamName = value值
- 注解方式 import org.springframework.context.annotation.Profile;
第2章:spring中的Bean/2.6 环境参数和占位符/2.6.1 环境参数/2.6.1.3 参数激活
-
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(Ch2Configuration.class);
ConfigurableEnvironment environment = applicationContext.getEnvironment();
第2章:spring中的Bean/2.6 环境参数和占位符/2.6.2 占位符2.6.2.1 概念以及格式
第2章:spring中的Bean/2.6 环境参数和占位符/2.6.2 占位符2.6.2.2 占位符赋值
- 声明一个Bean启动占位符解析
public static PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() {
- 为占位符赋值
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(Ch2Configuration.class);
ConfigurableEnvironment environment = applicationContext.getEnvironment();
environment.setActiveProfiles("dev");
MutablePropertySources propertySources = environment.getPropertySources();