替换字符串占位符

Posted dongma

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了替换字符串占位符相关的知识,希望对你有一定的参考价值。

可以用spring提供的一个PropertyPlaceholderHelper类

替换代码如下:

protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
        throws BeansException {

    StringValueResolver valueResolver = new PlaceholderResolvingStringValueResolver(props);
    BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver);

    String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
    for (String curName : beanNames) {
        // Check that we‘re not parsing our own bean definition,
        // to avoid failing on unresolvable placeholders in properties file locations.
        if (!(curName.equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) {
            BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(curName);
            try {
                visitor.visitBeanDefinition(bd);
            }
            catch (Exception ex) {
                throw new BeanDefinitionStoreException(bd.getResourceDescription(), curName, ex.getMessage());
            }
        }
    }

    // New in Spring 2.5: resolve placeholders in alias target names and aliases as well.
    beanFactoryToProcess.resolveAliases(valueResolver);

    // New in Spring 3.0: resolve placeholders in embedded values such as annotation attributes.
    beanFactoryToProcess.addEmbeddedValueResolver(valueResolver);
}

 

以上是关于替换字符串占位符的主要内容,如果未能解决你的问题,请参考以下文章

java中有啥方法可以读取占位符的字符串,并且把占位符替换成参数

java中有啥方法可以读取占位符的字符串,并且把占位符替换成参数

用占位符替换字符串并在函数之后将它们替换回来。

js使用占位符替换字符串

具有许多占位符的 ItemStack

Java 模板变量替换(字符串占位符替换)