/** * Return a sequential {@link Stream} containing the property sources. * @since 5.1 */ default Stream<PropertySource<?>> stream() { return StreamSupport.stream(spliterator(), false); }
private static Stream<PropertySource<?>> streamPropertySources( PropertySources sources) { return sources.stream().flatMap(ConfigurationPropertySources::flatten) .filter(ConfigurationPropertySources::isIncluded); }
@Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { environment = applicationContext.getEnvironment(); PropertySourcesPlaceholderConfigurer cfg = applicationContext.getBean( "org.springframework.context.support.PropertySourcesPlaceholderConfigurer#0", PropertySourcesPlaceholderConfigurer.class); localPropertySource = cfg.getAppliedPropertySources() .get(PropertySourcesPlaceholderConfigurer.LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME); }
@Test public void explicitPropertySources() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addLast(new MockPropertySource().withProperty("my.name", "foo")); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setPropertySources(propertySources); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("foo")); assertEquals(ppc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next()); }
private void refreshCachedProperties() { PropertySources propertySources = environment.getPropertySources(); propertySources.forEach(this::refreshPropertySource); }
private boolean hasCloudPropertySource(PropertySources propertySources) { return propertySources.contains(CLOUD_PROPERTY_SOURCE); }
/** * Returns a property value from the application context. * * @param name Property name. * @return Property value, or null if not found. */ public static String getProperty(String name) { ApplicationContext appContext = getRootAppContext(); if (appContext == null) { return null; } String value = appContext.getEnvironment().getProperty(name); if (value == null) { PropertySourcesPlaceholderConfigurer cfg = appContext.getBean(PropertySourcesPlaceholderConfigurer.class); PropertySource<?> ps = cfg.getAppliedPropertySources().get( PropertySourcesPlaceholderConfigurer.LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME); value = ps == null ? null : (String) ps.getProperty(name); } return value; }
@Test public void explicitPropertySourcesExcludesEnvironment() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addLast(new MockPropertySource()); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setPropertySources(propertySources); ppc.setEnvironment(new MockEnvironment().withProperty("my.name", "env")); ppc.setIgnoreUnresolvablePlaceholders(true); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}")); assertEquals(ppc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next()); }
private void refreshCachedProperties() { PropertySources propertySources = environment.getPropertySources(); propertySources.forEach(this::refreshPropertySource); }
/** * Return a sequential {@link Stream} containing the property sources. * @since 5.1 */ default Stream<PropertySource<?>> stream() { return StreamSupport.stream(spliterator(), false); }
private static void startServer() { Object port = ctx.getBean(PropertySourcesPlaceholderConfigurer.class).getAppliedPropertySources().get("localProperties").getProperty("server.port"); ctx.getBean(NettyServer.class).start(Integer.parseInt(port.toString())); }
@Override public Iterator<PropertySource<?>> iterator() { return this.propertySourceList.iterator(); }
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); } }); }
/** * Return a sequential {@link Stream} containing the property sources. * @since 5.1 */ default Stream<PropertySource<?>> stream() { return StreamSupport.stream(spliterator(), false); }