List of usage examples for org.springframework.core.env ConfigurableEnvironment getPropertySources
MutablePropertySources getPropertySources();
From source file:org.springframework.boot.SpringApplication.java
/** * Add, remove or re-order any {@link PropertySource}s in this application's * environment./*from w w w .j a v a 2 s . c om*/ * @param environment this application's environment * @param args arguments passed to the {@code run} method * @see #configureEnvironment(ConfigurableEnvironment, String[]) */ protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) { MutablePropertySources sources = environment.getPropertySources(); if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) { sources.addLast(new MapPropertySource("defaultProperties", this.defaultProperties)); } if (this.addCommandLineProperties && args.length > 0) { String name = CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME; if (sources.contains(name)) { PropertySource<?> source = sources.get(name); CompositePropertySource composite = new CompositePropertySource(name); composite .addPropertySource(new SimpleCommandLinePropertySource(name + "-" + args.hashCode(), args)); composite.addPropertySource(source); sources.replace(name, composite); } else { sources.addFirst(new SimpleCommandLinePropertySource(args)); } } }
From source file:org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer.java
@Override public void initialize(ConfigurableApplicationContext applicationContext) { ConfigurableEnvironment environment = applicationContext.getEnvironment(); MapPropertySource decrypted = new MapPropertySource(DECRYPTED_PROPERTY_SOURCE_NAME, decrypt(environment.getPropertySources())); if (!decrypted.getSource().isEmpty()) { // We have some decrypted properties insert(environment.getPropertySources(), decrypted); ApplicationContext parent = applicationContext.getParent(); if (parent != null && (parent.getEnvironment() instanceof ConfigurableEnvironment)) { ConfigurableEnvironment mutable = (ConfigurableEnvironment) parent.getEnvironment(); // The parent is actually the bootstrap context, and it is fully // initialized, so we can fire an EnvironmentChangeEvent there to rebind // @ConfigurationProperties, in case they were encrypted. insert(mutable.getPropertySources(), decrypted); parent.publishEvent(new EnvironmentChangeEvent(decrypted.getSource().keySet())); }//ww w .j a va 2 s. c om } }
From source file:org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationListener.java
@Override public void initialize(ConfigurableApplicationContext applicationContext) { ConfigurableEnvironment environment = applicationContext.getEnvironment(); Map<String, Object> overrides = new LinkedHashMap<String, Object>(); for (PropertySource<?> source : environment.getPropertySources()) { decrypt(source, overrides);/*from w w w . ja v a 2s. co m*/ } if (!overrides.isEmpty()) { environment.getPropertySources().addFirst(new MapPropertySource("decrypted", overrides)); } }
From source file:org.springframework.cloud.config.server.environment.NativeEnvironmentRepository.java
@Override public Environment findOne(String config, String profile, String label) { SpringApplicationBuilder builder = new SpringApplicationBuilder(PropertyPlaceholderAutoConfiguration.class); ConfigurableEnvironment environment = getEnvironment(profile); builder.environment(environment);//from www . j a v a 2 s .c o m builder.web(false).bannerMode(Mode.OFF); if (!logger.isDebugEnabled()) { // Make the mini-application startup less verbose builder.logStartupInfo(false); } String[] args = getArgs(config, profile, label); // Explicitly set the listeners (to exclude logging listener which would change // log levels in the caller) builder.application().setListeners(Arrays.asList(new ConfigFileApplicationListener())); ConfigurableApplicationContext context = builder.run(args); environment.getPropertySources().remove("profiles"); try { return clean(new PassthruEnvironmentRepository(environment).findOne(config, profile, label)); } finally { context.close(); } }
From source file:org.springframework.cloud.config.server.environment.NativeEnvironmentRepository.java
private ConfigurableEnvironment getEnvironment(String profile) { ConfigurableEnvironment environment = new StandardEnvironment(); environment.getPropertySources().addFirst(new MapPropertySource("profiles", Collections.<String, Object>singletonMap("spring.profiles.active", profile))); return environment; }
From source file:org.springframework.cloud.config.server.NativeEnvironmentRepository.java
@Override public Environment findOne(String config, String profile, String label) { SpringApplicationBuilder builder = new SpringApplicationBuilder(PropertyPlaceholderAutoConfiguration.class); ConfigurableEnvironment environment = getEnvironment(profile); builder.environment(environment);//from www . j av a 2 s .c om builder.web(false).showBanner(false); String[] args = getArgs(config, label); // Explicitly set the listeners (to exclude logging listener which would change // log levels in the caller) builder.application().setListeners(Collections.singletonList(new ConfigFileApplicationListener())); ConfigurableApplicationContext context = builder.run(args); environment.getPropertySources().remove("profiles"); try { return clean(new PassthruEnvironmentRepository(environment).findOne(config, profile, label)); } finally { context.close(); } }
From source file:org.springframework.cloud.dataflow.server.cloudfoundry.config.CloudFoundryEnvironmentPostProcessor.java
@Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { if (Boolean.valueOf(environment.getProperty(FEATURES_PREFIX + FeaturesProperties.TASKS_ENABLED))) { logger.warn(FEATURES_PREFIX + FeaturesProperties.TASKS_ENABLED + " has been set to true directly. " + "Be advised this is an EXPERIMENTAL feature, normally enabled via " + FEATURES_PREFIX + "experimental.tasksEnabled"); }/*from ww w .j a v a 2 s .c o m*/ Map<String, Object> propertiesToOverride = new HashMap<>(); boolean isTasksEnabled = Boolean .valueOf(environment.getProperty(FEATURES_PREFIX + "experimental.tasksEnabled")); if (isTasksEnabled) { propertiesToOverride.put(FEATURES_PREFIX + FeaturesProperties.TASKS_ENABLED, true); environment.getPropertySources() .addFirst(new MapPropertySource("CFDataflowServerProperties", propertiesToOverride)); } }
From source file:org.springframework.cloud.dataflow.server.config.DefaultEnvironmentPostProcessor.java
@Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { Map<String, Object> internalDefaults = new HashMap<>(); Map<String, Object> defaults = new HashMap<>(); MutablePropertySources existingPropertySources = environment.getPropertySources(); contributeDefaults(internalDefaults, serverDefaultsResource); contributeDefaults(defaults, serverResource); String defaultPropertiesKey = "defaultProperties"; if (!existingPropertySources.contains(defaultPropertiesKey) || existingPropertySources.get(defaultPropertiesKey) == null) { existingPropertySources.addLast(new MapPropertySource(defaultPropertiesKey, internalDefaults)); existingPropertySources.addLast(new MapPropertySource(defaultPropertiesKey, defaults)); } else {/*from w w w . ja v a 2 s. c o m*/ PropertySource<?> propertySource = existingPropertySources.get(defaultPropertiesKey); @SuppressWarnings("unchecked") Map<String, Object> mapOfProperties = Map.class.cast(propertySource.getSource()); for (String k : internalDefaults.keySet()) { Set<String> setOfPropertyKeys = mapOfProperties.keySet(); if (!setOfPropertyKeys.contains(k)) { mapOfProperties.put(k, internalDefaults.get(k)); logger.debug(k + '=' + internalDefaults.get(k)); } } for (String k : defaults.keySet()) { Set<String> setOfPropertyKeys = mapOfProperties.keySet(); if (!setOfPropertyKeys.contains(k)) { mapOfProperties.put(k, defaults.get(k)); logger.debug(k + '=' + defaults.get(k)); } } } }
From source file:org.springframework.cloud.gcp.autoconfigure.core.cloudfoundry.GcpCloudFoundryEnvironmentPostProcessor.java
@Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { if (!StringUtils.isEmpty(environment.getProperty(VCAP_SERVICES_ENVVAR))) { Map<String, Object> vcapMap = this.parser.parseMap(environment.getProperty(VCAP_SERVICES_ENVVAR)); Properties gcpCfServiceProperties = new Properties(); Set<GcpCfService> servicesToMap = new HashSet<>(Arrays.asList(GcpCfService.values())); if (vcapMap.containsKey(GcpCfService.MYSQL.getCfServiceName()) && vcapMap.containsKey(GcpCfService.POSTGRES.getCfServiceName())) { LOGGER.warn("Both MySQL and PostgreSQL bound to the app. " + "Not configuring Cloud SQL."); servicesToMap.remove(GcpCfService.MYSQL); servicesToMap.remove(GcpCfService.POSTGRES); }// w ww. ja v a 2s .c om servicesToMap.forEach((service) -> gcpCfServiceProperties.putAll(retrieveCfProperties(vcapMap, service.getGcpServiceName(), service.getCfServiceName(), service.getCfPropNameToGcp()))); // For Cloud SQL, there are some exceptions to the rule. // The instance connection name must be built from three fields. if (gcpCfServiceProperties.containsKey("spring.cloud.gcp.sql.instance-name")) { String instanceConnectionName = gcpCfServiceProperties .getProperty("spring.cloud.gcp.sql.project-id") + ":" + gcpCfServiceProperties.getProperty("spring.cloud.gcp.sql.region") + ":" + gcpCfServiceProperties.getProperty("spring.cloud.gcp.sql.instance-name"); gcpCfServiceProperties.put("spring.cloud.gcp.sql.instance-connection-name", instanceConnectionName); } // The username and password should be in the generic DataSourceProperties. if (gcpCfServiceProperties.containsKey("spring.cloud.gcp.sql.username")) { gcpCfServiceProperties.put("spring.datasource.username", gcpCfServiceProperties.getProperty("spring.cloud.gcp.sql.username")); } if (gcpCfServiceProperties.containsKey("spring.cloud.gcp.sql.password")) { gcpCfServiceProperties.put("spring.datasource.password", gcpCfServiceProperties.getProperty("spring.cloud.gcp.sql.password")); } environment.getPropertySources() .addFirst(new PropertiesPropertySource("gcpCf", gcpCfServiceProperties)); } }
From source file:org.springframework.core.env.AbstractEnvironment.java
@Override public void merge(ConfigurableEnvironment parent) { for (PropertySource<?> ps : parent.getPropertySources()) { if (!this.propertySources.contains(ps.getName())) { this.propertySources.addLast(ps); }/*from ww w.j a va2s . c o m*/ } String[] parentActiveProfiles = parent.getActiveProfiles(); if (!ObjectUtils.isEmpty(parentActiveProfiles)) { synchronized (this.activeProfiles) { for (String profile : parentActiveProfiles) { this.activeProfiles.add(profile); } } } String[] parentDefaultProfiles = parent.getDefaultProfiles(); if (!ObjectUtils.isEmpty(parentDefaultProfiles)) { synchronized (this.defaultProfiles) { this.defaultProfiles.remove(RESERVED_DEFAULT_PROFILE_NAME); for (String profile : parentDefaultProfiles) { this.defaultProfiles.add(profile); } } } }