Java Spring无法解析占位符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28307237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Could not Resolve placeholder
提问by Brkk
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:
我对春天相当陌生,所以如果这是一个愚蠢的问题,请原谅我。当我尝试启动程序时,出现以下错误: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;
}
}
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?
appclient.properties
资源文件夹中存在一个名为的属性文件,其中包含主机和端口的信息。我不确定"${appclient}"
定义在哪里,如果有的话。也许它甚至没有定义,这导致了问题。我是否需要将其更改"${appclient}"
为类似的内容,"{classpath:/appclient.properties}"
或者我是否缺少其他内容?
采纳答案by Rohit Jain
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 应将参数传递为:file:appclient.properties
或classpath:appclient.properties
。将注释更改为:
@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.
但是我不知道你的PropertiesConfig
文件包含什么,因为你也在导入它。理想情况下,@PropertySource
注释应该保留在那里。
回答by Aninda Bhattacharyya
If you are using Spring 3.1 and above, you can use something like...
如果您使用的是 Spring 3.1 及更高版本,则可以使用类似...
@Configuration
@PropertySource("classpath:foo.properties")
public class PropertiesWithJavaConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
You can also go by the xml configuration like...
您还可以通过 xml 配置,如...
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:property-placeholder location="classpath:foo.properties" />
</beans>
In earlier versions.
在早期版本中。
回答by Piotr R
For properties that need to be managed outside of the WAR:
对于需要在 WAR 之外管理的属性:
<context:property-placeholder location="file:///C:/application.yml"/>
For example if inside application.yml are name
and id
例如,如果在 application.yml 中是name
和id
Then you can create bean in runtime inside xml spring
然后你可以在运行时在 xml spring 中创建 bean
<bean id="id1" class="my.class.Item">
<property name="name" value="${name}"/>
<property name="id" value="${id}"/>
</bean>
回答by Sahan Madusanka
If your config file is in a different path than classpath, you can add the configuration file path as a system property:
如果您的配置文件位于与类路径不同的路径中,您可以将配置文件路径添加为系统属性:
java -Dapp.config.path=path_to_config_file -jar your.jar
回答by Ben G
My solution was to add a space between the $ and the {.
我的解决方案是在 $ 和 { 之间添加一个空格。
For example:
例如:
@Value("${appclient.port:}")
becomes
变成
@Value("$ {appclient.port:}")
回答by Jonathan
Hopefully it will be still helpful, the application.properties (or application.yml) file must be in both the paths:
希望它仍然有用,application.properties(或application.yml)文件必须在两个路径中:
- src/main/resource/config
- src/test/resource/config
- 源代码/主/资源/配置
- 源代码/测试/资源/配置
containing the same property you are referring
包含您所指的相同属性