private void copyPropertySources(ConfigurableEnvironment source, StandardEnvironment target) { removePropertySources(target.getPropertySources(), isServletEnvironment(target.getClass(), this.classLoader)); for (PropertySource<?> propertySource : source.getPropertySources()) { if (!SERVLET_ENVIRONMENT_SOURCE_NAMES.contains(propertySource.getName())) { target.getPropertySources().addLast(propertySource); } } }
public static StandardEnvironment prepareEnvironment(Environment environment) { StandardEnvironment standardEnvironment = new StandardEnvironment(); standardEnvironment.getPropertySources() .remove(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME); standardEnvironment.getPropertySources() .remove(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME); standardEnvironment.getPropertySources() .addFirst(new EnvironmentPropertySource(environment)); return standardEnvironment; }
/** * Use Spring 3.1. environment support to set properties for the * customer-specific application context. * * @param ctx * @param customer */ private void setEnvironmentForCustomer(ConfigurableApplicationContext ctx, String customer) { StandardEnvironment env = new StandardEnvironment(); Properties props = new Properties(); // populate properties for customer props.setProperty("host", "host.for." + customer); props.setProperty("user", "user"); props.setProperty("password", "password"); props.setProperty("remote.directory", "/tmp"); PropertiesPropertySource pps = new PropertiesPropertySource("ftpprops", props); env.getPropertySources().addLast(pps); ctx.setEnvironment(env); } }
@Test public void covertNonOptionArgsToStringArrayAndList() { CommandLinePropertySource<?> ps = new SimpleCommandLinePropertySource("--o1=v1", "noa1", "--o2", "noa2"); StandardEnvironment env = new StandardEnvironment(); env.getPropertySources().addFirst(ps); String nonOptionArgs = env.getProperty("nonOptionArgs"); assertThat(nonOptionArgs, equalTo("noa1,noa2")); String[] nonOptionArgsArray = env.getProperty("nonOptionArgs", String[].class); assertThat(nonOptionArgsArray[0], equalTo("noa1")); assertThat(nonOptionArgsArray[1], equalTo("noa2")); @SuppressWarnings("unchecked") List<String> nonOptionArgsList = env.getProperty("nonOptionArgs", List.class); assertThat(nonOptionArgsList.get(0), equalTo("noa1")); assertThat(nonOptionArgsList.get(1), equalTo("noa2")); } }
environment.getPropertySources().addLast(new NestedMapPropertySource("uaa.yml", properties));
@Test public void testNullProperties() throws Exception { EnvironmentPropertiesFactoryBean factory = new EnvironmentPropertiesFactoryBean(); StandardEnvironment environment = new StandardEnvironment(); environment.getPropertySources().addFirst(new MapPropertySource("foo", Collections.singletonMap("foo", null))); factory.setEnvironment(environment); Properties properties = factory.getObject(); assertEquals("", properties.get("foo")); }
@Test public void testPlaceholderProperties() throws Exception { EnvironmentMapFactoryBean factory = new EnvironmentMapFactoryBean(); StandardEnvironment environment = new StandardEnvironment(); environment.getPropertySources().addLast(new NestedMapPropertySource("override", getProperties("bar=${spam}"))); factory.setEnvironment(environment); factory.setDefaultProperties(getProperties("foo=baz")); Map<String, ?> properties = factory.getObject(); assertEquals("baz", properties.get("foo")); assertEquals("${spam}", properties.get("bar")); }
@Test public void testOverrideProperties() throws Exception { EnvironmentMapFactoryBean factory = new EnvironmentMapFactoryBean(); factory.setDefaultProperties(getProperties("foo=foo")); StandardEnvironment environment = new StandardEnvironment(); environment.getPropertySources().addLast(new NestedMapPropertySource("override", getProperties("foo=bar"))); factory.setEnvironment(environment); Map<String, ?> properties = factory.getObject(); assertEquals("bar", properties.get("foo")); }
private MutablePropertySources getPropertySources() { if (this.environment instanceof ConfigurableEnvironment) { return ((ConfigurableEnvironment) this.environment).getPropertySources(); } return new StandardEnvironment().getPropertySources(); }
private Environment environment(MutablePropertySources incoming) { StandardEnvironment environment = new StandardEnvironment(); for (PropertySource<?> source : environment.getPropertySources()) { environment.getPropertySources().remove(source.getName()); } for (PropertySource<?> source : incoming) { environment.getPropertySources().addLast(source); } return environment; }
public static StringValueResolver createStringValueResolver(Map<String, Object> map) { StandardEnvironment standardEnvironment = new StandardEnvironment(); standardEnvironment.getPropertySources().addFirst( new MapPropertySource(UUID.randomUUID().toString(), map)); return standardEnvironment::resolvePlaceholders; }
EnvCopy(final ConfigurableEnvironment environment) { copy = new StandardEnvironment(); environment.getPropertySources().forEach(ps -> { final PropertySource<?> original = ps instanceof EncryptablePropertySource ? ((EncryptablePropertySource) ps).getDelegate() : ps; copy.getPropertySources().addLast(original); }); }
private StandardEnvironment copyEnvironment(ConfigurableEnvironment input) { StandardEnvironment environment = new StandardEnvironment(); MutablePropertySources capturedPropertySources = environment.getPropertySources(); // Only copy the default property source(s) and the profiles over from the main // environment (everything else should be pristine, just like it was on startup). for (String name : DEFAULT_PROPERTY_SOURCES) { if (input.getPropertySources().contains(name)) { if (capturedPropertySources.contains(name)) { capturedPropertySources.replace(name, input.getPropertySources().get(name)); } else { capturedPropertySources.addLast(input.getPropertySources().get(name)); } } } environment.setActiveProfiles(input.getActiveProfiles()); environment.setDefaultProfiles(input.getDefaultProfiles()); Map<String, Object> map = new HashMap<String, Object>(); map.put("spring.jmx.enabled", false); map.put("spring.main.sources", ""); capturedPropertySources .addFirst(new MapPropertySource(REFRESH_ARGS_PROPERTY_SOURCE, map)); return environment; }
private ClassPathXmlApplicationContext createContext(UdpMulticastEndToEndTests launcher, String location) { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(); applicationContext.setConfigLocation(location); StandardEnvironment env = new StandardEnvironment(); Properties props = new Properties(); props.setProperty("port", Integer.toString(launcher.getReceiverPort())); PropertiesPropertySource pps = new PropertiesPropertySource("ftpprops", props); env.getPropertySources().addLast(pps); applicationContext.setEnvironment(env); applicationContext.refresh(); return applicationContext; }
private ClassPathXmlApplicationContext createContext(UdpUnicastEndToEndTests launcher, String location) { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(); applicationContext.setConfigLocation(location); StandardEnvironment env = new StandardEnvironment(); Properties props = new Properties(); props.setProperty("port", Integer.toString(launcher.getReceiverPort())); PropertiesPropertySource pps = new PropertiesPropertySource("ftpprops", props); env.getPropertySources().addLast(pps); applicationContext.setEnvironment(env); applicationContext.refresh(); return applicationContext; }
new ConfigFileApplicationListener())); capture = builder.run(); if (environment.getPropertySources().contains(REFRESH_ARGS_PROPERTY_SOURCE)) { environment.getPropertySources().remove(REFRESH_ARGS_PROPERTY_SOURCE); for (PropertySource<?> source : environment.getPropertySources()) { String name = source.getName(); if (target.contains(name)) {
StandardEnvironment bootstrapEnvironment = new StandardEnvironment(); MutablePropertySources bootstrapProperties = bootstrapEnvironment .getPropertySources(); for (PropertySource<?> source : bootstrapProperties) { bootstrapProperties.remove(source.getName());
private String getServerPort() { // Temporary code for REST Assure test if (!serverPort.equals("0")) { return serverPort; } return ((StandardEnvironment) this.env).getPropertySources().get("server.ports").getProperty("local.server.port").toString(); }
@Configuration @PropertySource("classpath:default.properties") public class AppConfig { @Autowired public void addOptionalProperties(StandardEnvironment environment) { try { String localPropertiesPath = environment.resolvePlaceholders("file:${java:comp/env/configLocation}/override.properties"); ResourcePropertySource localPropertySource = new ResourcePropertySource(localPropertiesPath); environment.getPropertySources().addLast(localPropertySource); } catch (IOException ignored) {} } }
public static StringValueResolver createStringValueResolver(Map<String, Object> map) { StandardEnvironment standardEnvironment = new StandardEnvironment(); standardEnvironment.getPropertySources().addFirst( new MapPropertySource(UUID.randomUUID().toString(), map)); return strVal -> { return standardEnvironment.resolvePlaceholders(strVal); }; }