private void reorderSources(ConfigurableEnvironment environment) { ConfigurationPropertySources.finishAndRelocate(environment.getPropertySources()); PropertySource<?> defaultProperties = environment.getPropertySources().remove(DEFAULT_PROPERTIES); if (defaultProperties != null) { environment.getPropertySources().addLast(defaultProperties); } }
/** * Add config file property sources to the specified environment. * * @param environment the environment to add source to * @see #addPostProcessors(ConfigurableApplicationContext) */ protected void addPropertySources( ConfigurableEnvironment environment, ResourceLoader resourceLoader) { RandomValuePropertySource.addToEnvironment(environment); try { PropertySource<?> defaultProperties = environment.getPropertySources().remove(DEFAULT_PROPERTIES); new Loader(environment, resourceLoader).load(); if (defaultProperties != null) { environment.getPropertySources().addLast(defaultProperties); } } catch (IOException ex) { throw new IllegalStateException("Unable to load configuration files", ex); } }
protected static void configureInfoProperties(ConfigurableEnvironment env) { SystemInformation systemInformation = new SystemInformation(); Map<String, Object> infoMap = new HashMap<>(); infoMap.put("info.fileTimestamp", systemInformation.getDeploymentDateAsString()); infoMap.put("info.hostname", systemInformation.getHostname()); env.getPropertySources().addFirst(new MapPropertySource("INFO_MAP", infoMap)); }
private void addJsonPropertySource( ConfigurableEnvironment environment, PropertySource<?> source) { MutablePropertySources sources = environment.getPropertySources(); String name = findPropertySource(sources); if (sources.contains(name)) { sources.addBefore(name, source); } else { sources.addFirst(source); } }
/** @param containerAttributes */ private void setConfiguredContainerAttributes(ContainerAttributes containerAttributes) { Map<String, String> attributes = new HashMap<String, String>(); for (PropertySource<?> propertySource : environment.getPropertySources()) { if (propertySource instanceof EnumerablePropertySource) { EnumerablePropertySource<?> ps = (EnumerablePropertySource<?>) propertySource; for (String key : ps.getPropertyNames()) { if (key.startsWith(CONTAINER_ATTRIBUTES_PREFIX)) { String attributeKey = key.replaceAll(CONTAINER_ATTRIBUTES_PREFIX, ""); attributes.put(attributeKey, environment.getProperty(key)); } } } } containerAttributes.putAll(attributes); }
protected static void mess() { // System.setProperty("spring.profiles.active","foo"); // ConfigurableApplicationContext ctx = // new // ClassPathXmlApplicationContext("classpath:/com/rooney/spring/spel/Spel-context.xml"); // can we add appcontext.xml to a genericAppCtx and then add property sources, when call // refresh? ConfigurableApplicationContext ctx = new GenericApplicationContext(); ConfigurableEnvironment environment = ctx.getEnvironment(); MutablePropertySources sources = environment.getPropertySources(); sources.addFirst(new MyPropertySource()); System.out.println(ctx.getBean("stringBean")); }
@Override public void postProcessEnvironment( ConfigurableEnvironment environment, SpringApplication application) { if (CloudPlatform.CLOUD_FOUNDRY.isActive(environment)) { Properties properties = new Properties(); addWithPrefix(properties, getPropertiesFromApplication(environment), "vcap.application."); addWithPrefix(properties, getPropertiesFromServices(environment), "vcap.services."); MutablePropertySources propertySources = environment.getPropertySources(); if (propertySources.contains(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME)) { propertySources.addAfter( CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME, new PropertiesPropertySource("vcap", properties)); } else { propertySources.addFirst(new PropertiesPropertySource("vcap", properties)); } } }
/** * Bind the environment to the {@link SpringApplication}. * * @param environment the environment to bind * @param application the application to bind to */ protected void bindToSpringApplication( ConfigurableEnvironment environment, SpringApplication application) { RelaxedDataBinder binder = new RelaxedDataBinder(application, "spring.main"); binder.setConversionService(this.conversionService); binder.bind(new PropertySourcesPropertyValues(environment.getPropertySources())); }
@Override public void postProcessEnvironment( ConfigurableEnvironment environment, SpringApplication application) { assertThat(environment.getPropertySources().size(), is(equalTo(4))); }
@Test public void noBootstrapProperties() { assertFalse(environment.getPropertySources().contains("bootstrap")); }