【发布时间】:2015-02-03 19:34:01
【问题描述】:
我对 spring 还很陌生,所以如果这是一个愚蠢的问题,请原谅。当我尝试启动程序时,出现以下错误:java.lang.IllegalArgumentException: Could not resolve placeholder 'appclient' in string value [${appclient}]。执行以下代码时会抛出错误:
package ca.virology.lib2.common.config.spring.properties;
import ca.virology.lib2.config.spring.PropertiesConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
@Configuration
@Import({PropertiesConfig.class})
@PropertySource("${appclient}")
public class AppClientProperties {
private static final Logger log = LoggerFactory.getLogger(AppClientProperties.class);
{
//this initializer block will execute when an instance of this class is created by Spring
log.info("Loading AppClientProperties");
}
@Value("${appclient.port:}")
private int appClientPort;
@Value("${appclient.host:}")
private String appClientHost;
public int getAppClientPort() {
return appClientPort;
}
public String getAppClientHost() {
return appClientHost;
}
}
资源文件夹中存在一个名为appclient.properties 的属性文件,其中包含主机和端口信息。我不确定"${appclient}" 是在哪里定义的,如果有的话。也许它甚至没有定义,这就是导致问题的原因。我是否需要将"${appclient}" 更改为"{classpath:/appclient.properties}" 之类的东西,还是我错过了其他东西?
【问题讨论】:
-
你可以使用类路径引用 PropertySource:docs.spring.io/spring/docs/current/javadoc-api/org/…
-
我实际上是在尝试运行这个程序,在另一个名为“A”的程序中将其命名为“B”。显然,当您单独执行程序 B 时,它会获取
${appclient}值作为程序参数;但是,当我尝试从 A 运行 B 并导致我遇到的错误时,这并没有发生。 -
如果您有多个属性文件并且所有属性文件都具有相同的键,则会出现此问题。如果是,则在启动应用程序时使用配置文件参数。