【问题标题】:Spring Could not Resolve placeholderSpring 无法解析占位符
【发布时间】: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 并导致我遇到的错误时,这并没有发生。
  • 如果您有多个属性文件并且所有属性文件都具有相同的键,则会出现此问题。如果是,则在启动应用程序时使用配置文件参数。

标签: java spring


【解决方案1】:

您没有正确读取属性文件。 propertySource 应将参数传递为:file:appclient.properties 或 classpath:appclient.properties。将注释更改为:

@PropertySource(value={"classpath:appclient.properties"})

但是我不知道您的 PropertiesConfig 文件包含什么,因为您也在导入它。理想情况下,@PropertySource 注释应该保留在那里。

【讨论】:

    【解决方案2】:

    如果您使用的是 Spring 3.1 及更高版本,则可以使用类似...

    @Configuration
    @PropertySource("classpath:foo.properties")
    public class PropertiesWithJavaConfig {
    
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
      return new PropertySourcesPlaceholderConfigurer();
    }
    }
    

    你也可以通过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>
    

    在早期版本中。

    【讨论】:

      【解决方案3】:

      希望它仍然会有所帮助,application.properties(或 application.yml)文件必须在两个路径中:

      • src/main/resource/config
      • src/test/resource/config

      包含您所指的相同属性

      【讨论】:

        【解决方案4】:

        如果您的配置文件位于与类路径不同的路径中,您可以将配置文件路径添加为系统属性:

        java -Dapp.config.path=path_to_config_file -jar your.jar
        

        【讨论】:

        • 有效! java -Xdebug -Dct.ctconfig.consul.server=http://{你的 QA IP}:8355 -Dspring.profiles.active=qa -Xrunjdwp:server=y,transport=dt_socket,address=8200,suspend=n - jar build/libs/YourAppJar-0.0.1-SNAPSHOT.jar
        【解决方案5】:

        我知道这是一条旧消息,但我想添加我的案例。

        如果您使用多个配置文件(dev、test、prod...),请检查您的执行配置文件。

        【讨论】:

          【解决方案6】:

          对于需要在 WAR 之外管理的属性:

          <context:property-placeholder location="file:///C:/application.yml"/>
          

          例如,如果 application.yml 内部是 name 和 id

          然后你可以在xml spring里面的runtime中创建bean

          <bean id="id1" class="my.class.Item">
              <property name="name" value="${name}"/>
              <property name="id" value="${id}"/>
          </bean>
          

          【讨论】:

            【解决方案7】:

            在我的情况下,生成的 war 文件没有获取属性文件,因此必须在 IntelliJ 编辑器中再次进行全新安装。

            【讨论】:

              【解决方案8】:

              我的解决方案是在 $ 和 { 之间添加一个空格。

              例如:

              @Value("${appclient.port:}")
              

              变成

              @Value("$ {appclient.port:}")
              

              【讨论】:

              • 对于遇到此问题的任何人,这都行不通。添加空格仅意味着@Value 直接解析字符串(因此最终值变为$ {appclient.port:} 而不是12345)。
              猜你喜欢
              • 1970-01-01
              • 2016-07-24
              • 2018-02-20
              • 2017-05-01
              • 1970-01-01
              • 1970-01-01
              • 2019-01-12
              • 2020-10-05
              • 2017-08-17
              相关资源
              最近更新 更多