/** * Create and return a new {@link StandardServletEnvironment}. */ @Override protected ConfigurableEnvironment createEnvironment() { return new StandardServletEnvironment(); }
@Override public void initPropertySources(@Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) { WebApplicationContextUtils.initServletPropertySources(getPropertySources(), servletContext, servletConfig); }
StandardServletEnvironment environment = new StandardServletEnvironment(); environment.initPropertySources(servletContext, (ServletConfig) null); builder.environment(environment);
Mockito.when(context.getResource(ArgumentMatchers.eq("file:foo/uaa.yml"))).thenReturn( new ByteArrayResource(("logging:\n config: "+tomcatLogConfig).getBytes())); environment.getPropertySources().addFirst(new PropertySource<Object>(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME) { @Override public boolean containsProperty(String name) { assertEquals("-Djava.util.logging.config=/some/path/logging.properties", environment.getProperty("logging.config")); Mockito.verify(servletContext,atLeastOnce()).log(servletLogCaptor.capture()); boolean logEntryFound = false;
@Test public void testLoadServletConfiguredResource() throws Exception { Mockito.when(servletConfig.getInitParameter("environmentConfigLocations")).thenReturn("foo.yml"); Mockito.when(context.getResource(ArgumentMatchers.eq("foo.yml"))).thenReturn( new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes())); initializer.initialize(context); assertEquals("bar", environment.getProperty("foo")); assertEquals("baz", environment.getProperty("spam.foo")); }
@Override public void merge( ConfigurableEnvironment parent ) { removeIfParentContains( parent, JNDI_PROPERTY_SOURCE_NAME ); removeIfParentContains( parent, SERVLET_CONFIG_PROPERTY_SOURCE_NAME ); removeIfParentContains( parent, SERVLET_CONTEXT_PROPERTY_SOURCE_NAME ); super.merge( parent ); Properties properties = new Properties(); properties.put( "spring.http.encoding.force", true ); getPropertySources().addLast( new PropertiesPropertySource( "springHttpEncodingPropertySource", properties ) ); }
StandardServletEnvironment environment = new StandardServletEnvironment(); environment.initPropertySources(servletContext, null); builder.environment(environment);
@Test public void testLoggingConfigVariableWorks() throws Exception { System.setProperty("APPLICATION_CONFIG_FILE", "foo/uaa.yml"); Mockito.when(context.getResource(ArgumentMatchers.eq("file:foo/uaa.yml"))).thenReturn( new ByteArrayResource("logging:\n config: /some/path".getBytes())); initializer.initialize(context); assertEquals("/some/path", environment.getProperty("logging.config")); assertNull(environment.getProperty("smtp.host")); assertNull(environment.getProperty("smtp.port")); }
private void mergeProperties(PropertySourcesPlaceholderConfigurer configurer) { PropertySources sources = configurer.getAppliedPropertySources(); sources.forEach(source -> { Object env = source.getSource(); if (env instanceof ConfigurableEnvironment) { ConfigurableEnvironment ssenv = (ConfigurableEnvironment) env; ssenv.merge(localenv); localenv.merge(ssenv); } }); }
/** * Create and return a new {@link StandardServletEnvironment}. * <p>Subclasses may override this in order to configure the environment or * specialize the environment type returned. * @since 4.3.9 */ protected Environment createEnvironment() { return new StandardServletEnvironment(); }
@Override public void initPropertySources(@Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) { WebApplicationContextUtils.initServletPropertySources(getPropertySources(), servletContext, servletConfig); }
@Test public void testLoadServletConfiguredFilename() throws Exception { Mockito.when(servletConfig.getInitParameter("APPLICATION_CONFIG_FILE")).thenReturn("/config/path/foo.yml"); Mockito.when(context.getResource(ArgumentMatchers.eq("file:/config/path/foo.yml"))).thenReturn( new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes())); initializer.initialize(context); assertEquals("bar", environment.getProperty("foo")); assertEquals("baz", environment.getProperty("spam.foo")); }
/** * Create and return a new {@link StandardServletEnvironment}. */ @Override protected ConfigurableEnvironment createEnvironment() { return new StandardServletEnvironment(); }
private void setProperty(String name, String value) { StandardServletEnvironment env = (StandardServletEnvironment) webApplicationContext.getEnvironment(); MockPropertySource mockPropertySource = new MockPropertySource(); mockPropertySource.setProperty(name, value); env.getPropertySources().addLast(mockPropertySource); assertEquals(value, webApplicationContext.getEnvironment().getProperty(name)); }
@Test public void testLoadContextConfiguredResource() throws Exception { Mockito.when(servletContext.getInitParameter("environmentConfigLocations")).thenReturn("foo.yml"); Mockito.when(context.getResource(ArgumentMatchers.eq("foo.yml"))).thenReturn( new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes())); initializer.initialize(context); assertEquals("bar", environment.getProperty("foo")); assertEquals("baz", environment.getProperty("spam.foo")); }
/** * Create and return a new {@link StandardServletEnvironment}. * <p>Subclasses may override this in order to configure the environment or * specialize the environment type returned. */ protected ConfigurableEnvironment createEnvironment() { return new StandardServletEnvironment(); }
@Bean public PropertySystemInfo systemInfo() { PropertySystemInfo info = new PropertySystemInfo(Status.RUNNING, Type.CONFIG); info.setName(getName()); info.setVersion(getVersion()); info.setStartTime(DateUtil.now()); if (!(env instanceof StandardServletEnvironment)) { return info; } StandardServletEnvironment env = (StandardServletEnvironment) this.env; for (PropertySource<?> next : env.getPropertySources()) { if (next instanceof ResourcePropertySource) { Map<String, Object> source = ((ResourcePropertySource) next).getSource(); info.setInfo(source); } } return info; }
@Test public void testLoadReplacedResource() throws Exception { System.setProperty("APPLICATION_CONFIG_URL", "file:foo/uaa.yml"); Mockito.when(context.getResource(ArgumentMatchers.eq("file:foo/uaa.yml"))).thenReturn( new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes())); initializer.initialize(context); assertEquals("bar", environment.getProperty("foo")); assertEquals("baz", environment.getProperty("spam.foo")); }
/** * Create and return a new {@link StandardServletEnvironment}. Subclasses may override * in order to configure the environment or specialize the environment type returned. */ @Override protected ConfigurableEnvironment createEnvironment() { return new StandardServletEnvironment(); }
@Override public void initPropertySources(@Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) { WebApplicationContextUtils.initServletPropertySources(getPropertySources(), servletContext, servletConfig); }