Java Code Examples for org.springframework.util.PropertyPlaceholderHelper

Following code examples demonstrate how to use org.springframework.util.PropertyPlaceholderHelperfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.util.PropertyPlaceholderHelperand various code implementation of this class.

	private String resolveStringValue(String value, final Properties properties) {
		try {
			PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(DEFAULT_PLACEHOLDER_PREFIX, DEFAULT_PLACEHOLDER_SUFFIX, DEFAULT_VALUE_SEPARATOR, false);
			return helper.replacePlaceholders(value, new PlaceholderResolver() {
				@Override
				public String resolvePlaceholder(String placeholderName) {
					return properties.getProperty(placeholderName.toLowerCase());
				}
			});
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "";
	} 


	public static void main(String[] args) {
		String tmpl = "this is ${name}";
		
		final Map<String, String> values = new HashMap<>();
		values.put("name", "wangbo");
		
		PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}");
		String content = helper.replacePlaceholders(tmpl, new PlaceholderResolver() {

			@Override
			public String resolvePlaceholder(String placeholderName) {
				return values.get(placeholderName);
			}
		});
		
		System.out.println(content);
	} 

    public static String replacePlaceholders(final Map<String, String> variables, final String value) throws PlaceholderResolutionException {
        PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${","}");
        return helper.replacePlaceholders(value, new PlaceholderResolver() {
            public String resolvePlaceholder(String key) {
                if (key.isEmpty()) {
                    throw new PlaceholderResolutionException("Placeholder in '" + value + "' has to have a length of at least 1");
                }
                
                String result = variables.get(key);
                if (result == null) {
                    throw new PlaceholderResolutionException("Missing value for placeholder: '" + key + "' in '" + value + "'");
                }
                
                return result;
            }
        });
    } 

    public static void main(String[] args) throws IOException {

        PropertyPlaceholderHelper propertyPlaceholderHelper
                = new PropertyPlaceholderHelper("${", "}");

        Properties properties = new Properties();
        properties.load(PlaceHolderHelperTest.class.getClassLoader().getResourceAsStream("Placeholder.properties"));

        
        String placeholders = propertyPlaceholderHelper.replacePlaceholders("${name}${age}${sex}", properties);
        System.out.println(placeholders);
    } 

Advertisement
Javadoc
Creates a new PropertyPlaceholderHelper that uses the supplied prefix and suffix. Unresolvable place

holders are ignored. @param placeholderPrefix the prefix that denotes the start of a placeholder @param placeholderSuffix the suffix that denotes the end of a placeholder

Read More
Advertisement