Spring无法解决占位符问题 [英] Spring Could not Resolve placeholder
问题描述
java.lang.IllegalArgumentException:无法解析字符串值[$ {appclient}]
中的占位符'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})
公共类AppClientProperties {
private static final Logger log = LoggerFactory.getLogger(AppClientProperties.class);
{
//当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
存在于resources文件夹中,其中包含主机和端口的信息。我不确定$ {appclient}
在哪里定义,如果有的话。也许它甚至没有定义,这导致了问题。我是否需要将$ {appclient}
更改为{classpath:/appclient.properties}
或者我错过了别的什么?
您没有正确读取属性文件。 propertySource应该将参数传递为: file:appclient.properties
或 classpath:appclient.properties
。将注释更改为:
@PropertySource(value = {classpath:appclient.properties})
但是我不知道你的 PropertiesConfig
文件包含什么,就像你一样'那也是进口的。理想情况下,应该保留 @PropertySource
注释。
I'm fairly new to spring so excuse me if this is a dumb question. When I try to launch a program I get the following error: java.lang.IllegalArgumentException: Could not resolve placeholder 'appclient' in string value [${appclient}]
. The error is thrown when the following code is executed:
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;
}
}
A property file called appclient.properties
exists in the resources folder with the information for host and port. I'm not sure where the "${appclient}"
is defined, if it is at all. Maybe it is not even defined and that is causing the problem. Do I need to change the "${appclient}"
to something like "{classpath:/appclient.properties}"
or am I missing something else?
You are not reading the properties file correctly. The propertySource should pass the parameter as: file:appclient.properties
or classpath:appclient.properties
. Change the annotation to:
@PropertySource(value={"classpath:appclient.properties"})
However I don't know what your PropertiesConfig
file contains, as you're importing that also. Ideally the @PropertySource
annotation should have been kept there.
这篇关于Spring无法解决占位符问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!