升级到Spring 5.0.7.RELEASE导致parseStringValue问题

时间:2019-04-03 15:27:32

标签: java spring

我们有一个Java程序和一个第三方API,它们在两者之间与Spring保持同步。 从4.2.5.RELEASE升级到5.0.7.RELEASE之后  我在其中一个文件中看到以下错误:

  

方法parseStringValue(String,Properties,HashSet)是   未定义类型   NestedPropertyPlaceholderConfigurer NestedPropertyPlaceholderConfigurer.java

我尝试使用此导入而不是PropertyPlaceholderHelper,但是随后我似乎陷入了很多错误,我什至不知道那是正确的方法。

这是完整的代码页(减去一些编辑过的信息):

            package redacted.primavera.common.spring;

            import java.util.ArrayList;
            import java.util.HashSet;
            import java.util.List;
            import java.util.Properties;

            import org.springframework.beans.BeansException;
            import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
            import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
            import org.springframework.context.ApplicationContext;
            import org.springframework.context.ApplicationContextAware;
            import org.springframework.core.io.Resource;


            /**
             * Extends the PropertyPlaceholderConfigurer to implement nested resource
             * location placeholders.  Properties from the top-level resources may be
             * used to define nested resources.
             *   
             * @author redacted
             *
             */
            public class NestedPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer implements ApplicationContextAware {
                private ApplicationContext applicationContext;
                private Resource[] topLocations;
                private List<String> nestedLocations;


                /*
                 * Post-process the bean factory.  Add the nested resource locations to the
                 * top-level resource locations before processing.
                 *  
                 * (non-Javadoc)
                 * @see org.springframework.beans.factory.config.PropertyResourceConfigurer#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
                 */
                @Override
                public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
                    try {
                        Properties props = mergeProperties();

                        List<Resource> merged = new ArrayList<Resource>();
                        for (int i = 0; i < topLocations.length; i++) {
                            merged.add(topLocations[i]);
                        }

                        for (String nestedLocation : this.nestedLocations) {
                            String location = parseStringValue(nestedLocation.replaceAll("#\\{", "\\${"), props, new HashSet<String>());
                            if (location != null) {
                                Resource[] resources = this.applicationContext.getResources(location);
                                for (int i = 0; i < resources.length; i++) {
                                    Resource resource = resources[i];
                                    if (!merged.contains(resource)) {
                                        merged.add(resource);
                                    }
                                }
                            }
                        }

                        setLocations(merged.toArray(new Resource[merged.size()]));
                    }
                    catch (Exception e) {
                        throw new RuntimeException("", e);
                    }

                    super.postProcessBeanFactory(beanFactory);
                }

                /*
                 * (non-Javadoc)
                 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
                 */
                public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
                    this.applicationContext = applicationContext;
                }

                /*
                 * Set resource locations and save a local copy.
                 * 
                 * (non-Javadoc)
                 * @see org.springframework.core.io.support.PropertiesLoaderSupport#setLocations(org.springframework.core.io.Resource[])
                 */
                @Override
                public void setLocations(Resource[] locations) {
                    this.topLocations = new Resource[locations.length];
                    System.arraycopy(locations, 0, this.topLocations, 0, locations.length);

                    super.setLocations(locations);
                }

                /**
                 * Set the nested resource locations.
                 * @param nestedLocations
                 */
                public void setNestedLocations(List<String> nestedLocations) {
                    this.nestedLocations = nestedLocations;
                }

            }

2 个答案:

答案 0 :(得分:3)

如您在javadoc of version 4.x中所见,此方法已在5.x版中弃用并删除。

  

parseStringValue(String strVal,属性道具,设置VisitedPlaceholders)

     

已弃用。

     

从Spring 3.0开始,建议使用带有PropertyPlaceholderHelper的resolvePlaceholder(java.lang.String,java.util.Properties,int)。仅保留用于与Spring 2.5扩展兼容。

因此,您必须重构代码并使用PropertyPlaceholderHelper

答案 1 :(得分:1)

parseStringValue(String strVal, Properties props, Set visitedPlaceholders) 

已弃用。

正如Jens所建议的,这是我重构后的代码更改,以解决编译问题:

  1. 添加了导入:

    import org.springframework.util.PropertyPlaceholderHelper;

  2. 方法变量:

    PropertyPlaceholderHelper pph = new PropertyPlaceholderHelper(DEFAULT_PLACEHOLDER_PREFIX, DEFAULT_PLACEHOLDER_SUFFIX);


 3.替换为方法调用:

String location = pph.replacePlaceholders(nestedLocation.replaceAll("#\\{", "\\${"), props);