我们有一个 Java 程序和一个第三方 API,两者之间与 Spring 保持同步。 从 4.2.5.RELEASE 升级到 5.0.7.RELEASE 后 我在其中一个文件中看到以下错误:
The method parseStringValue(String, Properties, HashSet) is undefined for the type 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;
}
}
最佳答案
正如您在 javadoc of version 4.x 中看到的那样此方法已在 5.x 版本中弃用并删除。
parseStringValue(String strVal, Properties props, Set visitedPlaceholders)
Deprecated.
as of Spring 3.0, in favor of using resolvePlaceholder(java.lang.String, java.util.Properties, int) with PropertyPlaceholderHelper. Only retained for compatibility with Spring 2.5 extensions.
所以你必须重构你的代码并使用PropertyPlaceholderHelper
关于java - 升级到 spring 5.0.7.RELEASE 导致 parseStringValue 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55498863/