@Override public final void setEnvironment(Environment environment) { if (environment instanceof ConfigurableEnvironment) { this.propertySources = ((ConfigurableEnvironment) environment).getPropertySources(); } } }
@Override public final void setEnvironment(Environment environment) { if (environment instanceof ConfigurableEnvironment) { this.propertySources = ((ConfigurableEnvironment) environment).getPropertySources(); } } }
@Override protected void initPropertySources() { WebApplicationContextUtils.initServletPropertySources(getEnvironment().getPropertySources(), this.servletContext, this.servletConfig); }
@Override public void initialize(ConfigurableApplicationContext applicationContext) { ConfigurableEnvironment environment = applicationContext.getEnvironment(); environment.getPropertySources().addFirst(new PropertySource<Object>("testPropertySource") { @Override public Object getProperty(String key) { return "name".equals(key) ? "testName" : null; } }); } }
@Override protected ApplicationContext initApplicationContext() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(WebConfig.class); Properties props = new Properties(); props.setProperty("myOrigin", "http://site1.com"); context.getEnvironment().getPropertySources().addFirst(new PropertiesPropertySource("ps", props)); context.register(PropertySourcesPlaceholderConfigurer.class); context.refresh(); return context; }
@Test public void propertySourceTypes() { ConfigurableEnvironment env = new StandardEnvironment(); MutablePropertySources sources = env.getPropertySources(); assertThat(sources.get(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME), instanceOf(SystemEnvironmentPropertySource.class)); }
@Test public void withImplicitName() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ConfigWithImplicitName.class); ctx.refresh(); assertTrue("property source p1 was not added", ctx.getEnvironment().getPropertySources().contains("class path resource [org/springframework/context/annotation/p1.properties]")); assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean")); }
@Test public void addPropertiesFilesToEnvironmentWithSinglePropertyFromVirtualFile() { ConfigurableEnvironment environment = new MockEnvironment(); MutablePropertySources propertySources = environment.getPropertySources(); propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME); assertEquals(0, propertySources.size()); String pair = "key = value"; ByteArrayResource resource = new ByteArrayResource(pair.getBytes(), "from inlined property: " + pair); ResourceLoader resourceLoader = mock(ResourceLoader.class); when(resourceLoader.getResource(anyString())).thenReturn(resource); addPropertiesFilesToEnvironment(environment, resourceLoader, FOO_LOCATIONS); assertEquals(1, propertySources.size()); assertEquals("value", environment.getProperty("key")); }
@Override public void initialize(ConfigurableApplicationContext applicationContext) { applicationContext.getEnvironment().getPropertySources().addFirst( new MockPropertySource().withProperty("enigma", "foo")); } }
@Test @SuppressWarnings("rawtypes") public void propertyNameOrderingIsPreservedInEnvironment() { final String[] expectedPropertyNames = new String[] { "foo", "baz", "enigma", "x.y.z", "server.url", "key.value.1", "key.value.2", "key.value.3" }; EnumerablePropertySource eps = (EnumerablePropertySource) env.getPropertySources().get( INLINED_PROPERTIES_PROPERTY_SOURCE_NAME); assertArrayEquals(expectedPropertyNames, eps.getPropertyNames()); }
@Test public void getDefaultProfiles() { assertThat(environment.getDefaultProfiles(), equalTo(new String[] {RESERVED_DEFAULT_PROFILE_NAME})); environment.getPropertySources().addFirst(new MockPropertySource().withProperty(DEFAULT_PROFILES_PROPERTY_NAME, "pd1")); assertThat(environment.getDefaultProfiles().length, is(1)); assertThat(Arrays.asList(environment.getDefaultProfiles()), hasItem("pd1")); }
@Test public void orderingDoesntReplaceExisting() throws Exception { // SPR-12198: mySource should 'win' as it was registered manually AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext(); MapPropertySource mySource = new MapPropertySource("mine", Collections.singletonMap("testbean.name", "myTestBean")); ctxWithoutName.getEnvironment().getPropertySources().addLast(mySource); ctxWithoutName.register(ConfigWithFourResourceLocations.class); ctxWithoutName.refresh(); assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name"), equalTo("myTestBean")); }
@Test public void acceptsProfiles_activeProfileSetViaProperty() { assertThat(environment.acceptsProfiles("p1"), is(false)); environment.getPropertySources().addFirst(new MockPropertySource().withProperty(ACTIVE_PROFILES_PROPERTY_NAME, "p1")); assertThat(environment.acceptsProfiles("p1"), is(true)); }
@Test public void propertySourceOrder() { ConfigurableEnvironment env = new StandardEnvironment(); MutablePropertySources sources = env.getPropertySources(); assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(0)); assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(1)); assertThat(sources.size(), is(2)); }
@Test @SuppressWarnings("all") public void braceAccess() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "#{environment['my.name']}") .getBeanDefinition()); GenericApplicationContext ctx = new GenericApplicationContext(bf); ctx.getEnvironment().getPropertySources().addFirst(new MockPropertySource().withProperty("my.name", "myBean")); ctx.refresh(); assertThat(ctx.getBean(TestBean.class).getName(), equalTo("myBean")); }
@Test public void importWithPlaceholder() throws Exception { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); PropertySource<?> propertySource = new MapPropertySource("test", Collections.<String, Object> singletonMap("test", "springframework")); ctx.getEnvironment().getPropertySources().addFirst(propertySource); ctx.register(ImportXmlConfig.class); ctx.refresh(); assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean")); ctx.close(); }
@Test @SuppressWarnings("rawtypes") public void addInlinedPropertiesToEnvironmentWithEmptyProperty() { ConfigurableEnvironment environment = new MockEnvironment(); MutablePropertySources propertySources = environment.getPropertySources(); propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME); assertEquals(0, propertySources.size()); addInlinedPropertiesToEnvironment(environment, asArray(" ")); assertEquals(1, propertySources.size()); assertEquals(0, ((Map) propertySources.iterator().next().getSource()).size()); }
@Test public void environmentAccess() throws Exception { Map<String, Object> map = new HashMap<>(); map.put("key.foo", "value.foo"); GenericApplicationContext wac = (GenericApplicationContext) context.getRequest().getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE); wac.getEnvironment().getPropertySources().addFirst(new MapPropertySource("mapSource", map)); wac.getDefaultListableBeanFactory().registerSingleton("bean2", context.getRequest().getAttribute("bean")); tag.setExpression("@environment['key.foo']"); int action = tag.doStartTag(); assertEquals(Tag.EVAL_BODY_INCLUDE, action); action = tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, action); assertEquals("value.foo", ((MockHttpServletResponse) context.getResponse()).getContentAsString()); }
@Test public void addActiveProfile_whenActiveProfilesPropertyIsAlreadySet() { ConfigurableEnvironment env = new StandardEnvironment(); assertThat(env.getProperty(ACTIVE_PROFILES_PROPERTY_NAME), nullValue()); env.getPropertySources().addFirst(new MockPropertySource().withProperty(ACTIVE_PROFILES_PROPERTY_NAME, "p1")); assertThat(env.getProperty(ACTIVE_PROFILES_PROPERTY_NAME), equalTo("p1")); env.addActiveProfile("p2"); assertThat(env.getActiveProfiles(), arrayContaining("p1", "p2")); }
@Before @SuppressWarnings("resource") public void setup() { StaticWebApplicationContext wac = new StaticWebApplicationContext(); Properties props = new Properties(); props.setProperty("myOrigin", "http://example.com"); wac.getEnvironment().getPropertySources().addFirst(new PropertiesPropertySource("ps", props)); wac.registerSingleton("ppc", PropertySourcesPlaceholderConfigurer.class); wac.refresh(); this.handlerMapping.setRemoveSemicolonContent(false); wac.getAutowireCapableBeanFactory().initializeBean(this.handlerMapping, "hm"); this.request.setMethod("GET"); this.request.addHeader(HttpHeaders.ORIGIN, "http://domain.com/"); }