org.springframework.beans.factory.config.PlaceholderConfigurerSupport Java Examples

The following examples show how to use org.springframework.beans.factory.config.PlaceholderConfigurerSupport. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PropertyUtil.java    From dk-foundation with GNU Lesser General Public License v2.1 6 votes vote down vote up
@SuppressWarnings("unchecked")
@SneakyThrows
private static Object v1(final Environment environment, final String prefix) {
    Class<?> resolverClass = Class.forName("org.springframework.boot.bind.RelaxedPropertyResolver");
    Constructor<?> resolverConstructor = resolverClass.getDeclaredConstructor(PropertyResolver.class);
    Method getSubPropertiesMethod = resolverClass.getDeclaredMethod("getSubProperties", String.class);
    Object resolverObject = resolverConstructor.newInstance(environment);
    String prefixParam = prefix.endsWith(".") ? prefix : prefix + ".";
    Method getPropertyMethod = resolverClass.getDeclaredMethod("getProperty", String.class);
    Map<String, Object> dataSourceProps = (Map<String, Object>) getSubPropertiesMethod.invoke(resolverObject, prefixParam);
    Map<String, Object> propertiesWithPlaceholderResolved = new HashMap<>();
    for (Entry<String, Object> entry : dataSourceProps.entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();
        if (value instanceof String && ((String) value).contains(
                PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX)) {
            String resolvedValue = (String) getPropertyMethod.invoke(resolverObject, prefixParam + key);
            propertiesWithPlaceholderResolved.put(key, resolvedValue);
        } else {
            propertiesWithPlaceholderResolved.put(key, value);
        }
    }
    return Collections.unmodifiableMap(propertiesWithPlaceholderResolved);
}
 
Example #2
Source File: PropertyUtil.java    From shardingsphere with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@SneakyThrows(ReflectiveOperationException.class)
private static Object v1(final Environment environment, final String prefix, final boolean handlePlaceholder) {
    Class<?> resolverClass = Class.forName("org.springframework.boot.bind.RelaxedPropertyResolver");
    Constructor<?> resolverConstructor = resolverClass.getDeclaredConstructor(PropertyResolver.class);
    Method getSubPropertiesMethod = resolverClass.getDeclaredMethod("getSubProperties", String.class);
    Object resolverObject = resolverConstructor.newInstance(environment);
    String prefixParam = prefix.endsWith(".") ? prefix : prefix + ".";
    Method getPropertyMethod = resolverClass.getDeclaredMethod("getProperty", String.class);
    Map<String, Object> dataSourceProps = (Map<String, Object>) getSubPropertiesMethod.invoke(resolverObject, prefixParam);
    Map<String, Object> propertiesWithPlaceholderResolved = new HashMap<>();
    for (Entry<String, Object> entry : dataSourceProps.entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();
        if (handlePlaceholder && value instanceof String && ((String) value).contains(PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX)) {
            String resolvedValue = (String) getPropertyMethod.invoke(resolverObject, prefixParam + key);
            propertiesWithPlaceholderResolved.put(key, resolvedValue);
        } else {
            propertiesWithPlaceholderResolved.put(key, value);
        }
    }
    return propertiesWithPlaceholderResolved;
}
 
Example #3
Source File: ContextNamespaceHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void propertyPlaceholder() throws Exception {
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
			"contextNamespaceHandlerTests-replace.xml", getClass());
	Map<String, PlaceholderConfigurerSupport> beans = applicationContext
			.getBeansOfType(PlaceholderConfigurerSupport.class);
	assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
	assertEquals("bar", applicationContext.getBean("string"));
	assertEquals("null", applicationContext.getBean("nullString"));
}
 
Example #4
Source File: ContextNamespaceHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void propertyPlaceholderEnvironmentProperties() throws Exception {
	MockEnvironment env = new MockEnvironment().withProperty("foo", "spam");
	GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext();
	applicationContext.setEnvironment(env);
	applicationContext.load(new ClassPathResource("contextNamespaceHandlerTests-simple.xml", getClass()));
	applicationContext.refresh();
	Map<String, PlaceholderConfigurerSupport> beans = applicationContext
			.getBeansOfType(PlaceholderConfigurerSupport.class);
	assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
	assertEquals("spam", applicationContext.getBean("string"));
	assertEquals("none", applicationContext.getBean("fallback"));
}
 
Example #5
Source File: ContextNamespaceHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void propertyPlaceholderIgnored() throws Exception {
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
			"contextNamespaceHandlerTests-replace-ignore.xml", getClass());
	Map<String, PlaceholderConfigurerSupport> beans = applicationContext
			.getBeansOfType(PlaceholderConfigurerSupport.class);
	assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
	assertEquals("${bar}", applicationContext.getBean("string"));
	assertEquals("null", applicationContext.getBean("nullString"));
}
 
Example #6
Source File: ServletConfiguration.java    From java-platform with Apache License 2.0 4 votes vote down vote up
@Bean
public static PlaceholderConfigurerSupport placeholderConfigurer() {
	return new ConfigurationPropertySourcesPlaceholderConfigurer(StaticConfigurationSupplier.getConfiguration());
}
 
Example #7
Source File: ConfigConfiguration.java    From java-platform with Apache License 2.0 4 votes vote down vote up
@Bean
public static PlaceholderConfigurerSupport placeholderConfigurer() {
	return new ConfigurationPropertySourcesPlaceholderConfigurer(StaticConfigurationSupplier.getConfiguration());
}