Java 无法解析字符串值中的占位符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20244696/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:32:28  来源:igfitidea点击:

Could not resolve placeholder in string value

javaspring

提问by Hwen

I am trying to use properties from a .propertiesfile, but it doesn't seem to work.

我正在尝试使用.properties文件中的属性,但它似乎不起作用。

Here is my code:

这是我的代码:

@Service("ServiceFTP")
@Transactional
public class ServiceFTPImpl implements ServiceFTP {

@Value("${project.ftp.adresse}")
private String adresse;

@Value("${project.ftp.login}")
private String compte;

@Value("${project.ftp.password}")
private String motDePasse;

@Value("${project.ftp.root}")
private String ROOT;

[...]

}

This class uses @Valueannotations to get the properties. It is also declared as a Spring Service and is linked to my infraContext.xmlfile :

这个类使用@Value注释来获取属性。它也被声明为 Spring 服务并链接到我的infraContext.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:context-core.properties"/>

[...]

</beans>

Using context:property-placeholder, I link this file to my context-core.propertiesfile :

使用context:property-placeholder,我将此文件链接到我的context-core.properties文件:

project.ftp.adresse = localhost
project.ftp.login = anonymous
project.ftp.password =
project.ftp.root = /anonymous/

This does make sense, right ?

这确实有道理,对吧?

But when I try to launch my project, Tomcat throw this exception :

但是当我尝试启动我的项目时,Tomcat 抛出此异常:

    ERROR [context.ContextLoader.initWebApplicationContext()] Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ServiceFTP': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String project.sins.service.impl.ServiceFTPImpl.adresse; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'project.ftp.adresse' in string value "${project.ftp.adresse}"
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:388)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:293)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4887)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:657)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1636)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String project.sins.service.impl.ServiceFTPImpl.adresse; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'project.ftp.adresse' in string value "${project.ftp.adresse}"
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:513)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
    ... 27 more
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'project.ftp.adresse' in string value "${project.ftp.adresse}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:173)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:125)
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:151)
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:142)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:169)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:748)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:740)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:485)
    ... 29 more

Or, in short : java.lang.IllegalArgumentException: Could not resolve placeholder 'project.ftp.adresse' in string value "${project.ftp.adresse}"

或者,简而言之: java.lang.IllegalArgumentException: Could not resolve placeholder 'project.ftp.adresse' in string value "${project.ftp.adresse}"

EDIT :

编辑 :

Here is my web.xml :

这是我的 web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="sins" version="2.5">

    <display-name>Project</display-name>

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <context-param>
        <param-name>log4jExposeWebAppRoot</param-name>
        <param-value>false</param-value>
    </context-param>

    <filter>
        <filter-name>ExpiresFilter</filter-name>
        <filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
        <init-param>
            <param-name>ExpiresByType text/html</param-name>
            <param-value>now plus 0 seconds</param-value>
        </init-param>
        <init-param>
            <param-name>ExpiresByType application/json</param-name>
            <param-value>now plus 0 seconds</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>ExpiresFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

    <filter>
        <filter-name>EncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
        <filter-class>
            org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
    </listener>

    <context-param>
        <param-name>
            org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG
        </param-name>
        <param-value>
            /WEB-INF/tiles/user.xml
        </param-value>
    </context-param>

    <resource-ref>
        <res-ref-name>jdbc/si_nsg</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>

</web-app>

My infraContext.xml is imported in another .xml file named applicationContext.xml :

我的 infraContext.xml 被导入到另一个名为 applicationContext.xml 的 .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:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">

    <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="environment">
            <bean class="org.springframework.web.context.support.StandardServletEnvironment"/>
        </property>
    </bean>

    <import resource="classpath:securityContext.xml"/>

    [...]
    <import resource="classpath:project/sins/persistenceContext.xml"/>

    <import resource="classpath:project/sins/infraContext.xml"/>

</beans>

I am obviously missing something, but I can't figure out what.

我显然错过了一些东西,但我无法弄清楚是什么。

Please let me know if you need more details, as it is my first question here, I'll try to answer as soon as I can :).

如果您需要更多详细信息,请告诉我,因为这是我在这里的第一个问题,我会尽快回答:)。

采纳答案by M. Deinum

In your configuration you have 2 PropertySourcesPlaceholderConfigurerinstances.

在您的配置中,您有 2 个PropertySourcesPlaceholderConfigurer实例。

applicationContext.xml

应用上下文.xml

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="environment">
        <bean class="org.springframework.web.context.support.StandardServletEnvironment"/>
    </property>
</bean>

infraContext.xml

基础上下文.xml

<context:property-placeholder location="classpath:context-core.properties"/>

By default a PlaceholderConfigureris going to fail-fast, so if a placeholder cannot be resolved it will throw an exception. The instance from the applicationContext.xmlfile has no properties and as such will fail on all placeholders.

默认情况下 aPlaceholderConfigurer会快速失败,因此如果无法解析占位符,它将抛出异常。applicationContext.xml文件中的实例没有属性,因此在所有占位符上都会失败。

Solution: Remove the one from applicationContext.xmlas it doesn't add anything it only breaks things.

解决方案:从applicationContext.xml 中删除一个,因为它没有添加任何东西,它只会破坏一些东西。

回答by user2910552

I had the same problem, resolved it by adding

我遇到了同样的问题,通过添加解决了它

<filtering>true</filtering> 

in pom.xml :

在 pom.xml 中:

before (didn't work):

之前(不起作用):

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>               
        </resource>
    </resources>
</build>

after(it worked):

之后(它有效):

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

After that you just run mvn clean installand deploy application.

之后,您只需运行mvn clean install并部署应用程序。

回答by Gani

This Issue occurs if the application is unable to access the some_file_name.properties file.Make sure that the properties file is placed under resources folder in spring.

如果应用程序无法访问 some_file_name.properties 文件,则会出现此问题。请确保在 spring 中将属性文件放置在资源文件夹下。

Trouble shooting Steps

故障排除步骤

1: Add the properties file under the resource folder.

1:在资源文件夹下添加属性文件。

2: If you don't have a resource folder. Create one by navigating new by Right click on the project new > Source Folder, name it as resource and place your properties file under it.

2:如果你没有资源文件夹。右键单击新建项目> 源文件夹,将其命名为资源并将您的属性文件放在其下。

For annotation based Implementation

对于基于注解的实现

Add @PropertySource(ignoreResourceNotFound = true, value = "classpath:some_file_name.properties")//Add it before using the place holder

添加@PropertySource(ignoreResourceNotFound = true, value = "classpath:some_file_name.properties")//在使用占位符之前添加

Example:

例子:

Assignment1Controller.Java

Assignment1Controller.Java

@PropertySource(ignoreResourceNotFound = true, value = "classpath:assignment1.properties")
@RestController  
public class Assignment1Controller {

//  @Autowired
//  Assignment1Services assignment1Services;
    @Value("${app.title}")
    private String appTitle;
     @RequestMapping(value = "/hello")  
        public String getValues() {

          return appTitle;

        }  

}

assignment1.properties

assignment1.properties

app.title=Learning Spring

回答by Semir Delji?

In my case, I was careless while merging the application.yml file, and I've unnecessary indented my properties to the right.

就我而言,我在合并 application.yml 文件时粗心大意,并且我不必要地将我的属性缩进到右边。

I've indented it like this:

我是这样缩进的:

spring:
    application:
       name: applicationName
............................
    myProperties:
       property1: property1value

While the code expected it to be like this:

虽然代码期望它是这样的:

spring:
    application:
        name: applicationName
.............................
myProperties:
    property1: property1value

回答by Ben G

My solution was to add a space between the $ and the {.

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

For example:

例如:

@Value("${project.ftp.adresse}")

becomes

变成

@Value("$ {project.ftp.adresse}")

回答by cdesmetz

With Spring Boot :

使用 Spring Boot:

In the pom.xml

在 pom.xml 中

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <addResources>true</addResources>
            </configuration>
        </plugin>
    </plugins>
</build>

Example in class Java

Java 类中的示例

@Configuration
@Slf4j
public class MyAppConfig {
    @Value("${foo}")
    private String foo;
    @Value("${bar}")
    private String bar;
    @Bean("foo")
    public String foo() {
        log.info("foo={}", foo);
        return foo;
    }
    @Bean("bar")
    public String bar() {
        log.info("bar={}", bar);
        return bar;
    }
    [ ... ]

In the properties files :

在属性文件中:

src/main/resources/application.properties

src/main/resources/application.properties

foo=all-env-foo

src/main/resources/application-rec.properties

src/main/resources/application-rec.properties

bar=rec-bar

src/main/resources/application-prod.properties

src/main/resources/application-prod.properties

bar=prod-bar

In the VM arguments of Application.java

在 Application.java 的 VM 参数中

-Dspring.profiles.active=[rec|prod]

Don't forget to run mvn command after modifying the properties !

修改属性后不要忘记运行 mvn 命令!

mvn clean package -Dmaven.test.skip=true

In the log file for -Dspring.profiles.active=rec :

在 -Dspring.profiles.active=rec 的日志文件中:

The following profiles are active: rec
foo=all-env-foo
bar=rec-bar

In the log file for -Dspring.profiles.active=prod :

在 -Dspring.profiles.active=prod 的日志文件中:

The following profiles are active: prod
foo=all-env-foo
bar=prod-bar

In the log file for -Dspring.profiles.active=local :

在 -Dspring.profiles.active=local 的日志文件中:

Could not resolve placeholder 'bar' in value "${bar}"

Oups, I forget to create application-local.properties.

糟糕,我忘记创建 application-local.properties。

回答by akasha

You can also try default values. spring-value-annotation

您也可以尝试使用默认值。弹簧值注释

Default values can be provided for properties that might not be defined. In this example the value “some default” will be injected:

可以为可能未定义的属性提供默认值。在这个例子中,值“some default”将被注入:

@Value("${unknown.param:some default}")
private String someDefault;

If the same property is defined as a system property and in the properties file, then the system property would be applied.

如果在属性文件中将同一属性定义为系统属性,则将应用系统属性。

回答by Ankush Thakur

I got same error in my Micro-service, whenever you declare @Value annotation in program i.e @Value("${project.api.key}")

我在我的微服务中遇到同样的错误,每当你在程序中声明 @Value 注释时,即@Value("${project.api.key}")

make sure that your application.properties file with same values should not be blankproject.api.key= add some values

确保具有相同值的application.properties 文件不应为空白project.api.key= 添加一些值

MostImp:otherwise it will through error "Error creating bean with name 'ServiceFTP': Injection of autowired dependencies"

MostImp :否则它将通过错误“创建名为'ServiceFTP'的bean时出错:注入自动装配的依赖项”

回答by Ankush Thakur

I got same error in my Micro-service, whenever you declare @Value annotation in program i.e @Value("${project.api.key}")

我在我的微服务中遇到同样的错误,每当你在程序中声明 @Value 注释时,即@Value("${project.api.key}")

make sure that your application.properties file with same values should not be blank project.api.key= add some values

确保具有相同值的 application.properties 文件不应为空白project.api.key= 添加一些值

MostIMP:otherwise it will throw error "Error creating bean with name 'ServiceFTP': Injection of autowired dependencies"

MostIMP:否则会抛出错误“创建名为'ServiceFTP'的bean时出错:注入自动装配的依赖项”

回答by Renato Vasconcellos

Deleting or corrupting the pom.xml file can cause this error.

删除或损坏 pom.xml 文件可能会导致此错误。