Java Spring Boot - 无法在 application.properties 的 xml 中解析属性

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

Spring Boot - property could not be resolved in xml from application.properties

javaxmlspringspring-bootproperties-file

提问by mangusbrother

I have a spring boot application

我有一个弹簧启动应用程序

My @Configuration classloads the xml Configuration using @ImportResource("path/to/xml"), which contains the following line

我使用@Configuration class加载 xml 配置@ImportResource("path/to/xml"),其中包含以下行

<property name="bla" value="${log.directory}/file.ext" />

Under src/main/resourcesI have the application.propertiesfile with the following content:

src/main/resourcesapplication.properties下面有包含以下内容的文件:

log.directory=C:/path/I/Need

However when I run It fails to load the property as follows:

但是,当我运行时,它无法加载该属性,如下所示:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'log.directory' in string value "${log.directory}/file.ext"

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'log.directory' in string value "${log.directory}/file.ext

采纳答案by Pedro Lopez

You can solve it adding context:property-placeholder to you xml. That way you will tell Spring to load your specific properties file.

您可以通过向您的 xml 添加 context:property-placeholder 来解决它。这样,您将告诉 Spring 加载您的特定属性文件。

However another more in line with Spring Boot solution is just using application.properties as the name for your properties file, having it in one of the expected locations, and use the @EnableAutoconfiguration annotation.

然而,另一个更符合 Spring Boot 解决方案的方法是使用 application.properties 作为属性文件的名称,将其放在预期位置之一,并使用 @EnableAutoconfiguration 注释。

Spring Boot expects the application.properties in the following location in order of preference.

Spring Boot 期望 application.properties 按优先顺序位于以下位置。

  1. A /config subdir of the current directory.
  2. The current directory
  3. A classpath /config package
  4. The classpath root
  1. 当前目录的 /config 子目录。
  2. 当前目录
  3. 一个类路径 /config 包
  4. 类路径根

I have tried this and it works.

我试过这个,它的工作原理。

pom.xml

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>sample</groupId>
    <artifactId>sample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Sample</name>
    <description>Spring Boot sample</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.8.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Sample.java

示例.java

package sample;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@EnableAutoConfiguration
@ComponentScan
@ImportResource("classpath:beans.xml")
public class Sample implements CommandLineRunner {

    @Value("${sample}")
    private String sample;

    @Autowired
    SampleService service;

    public static void main(String[] args) {
        SpringApplication.run(Sample.class, args);
    }

    public void run(String... args) {
        System.out.println(service.greetings());
    }
}

SampleService.java

示例服务.java

package sample;


public class SampleService {

    private String field;

    public String greetings() {
        return field;
    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }
}

beans.xml

bean.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"
  xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <bean class="sample.SampleService">
        <property name="field" value="${sample}-world"></property>
    </bean>
</beans>

application.properties

应用程序属性

sample=hello

In the output you will get hello worldif you run the program.

如果您运行该程序,您将在输出中得到hello world

Make sure that you have enabled the autoconfiguration. If you did not, it will not work as expected. In order to do so, add the @EnableAutoconfiguration annotation as in the example.

确保您已启用自动配置。如果你没有,它不会按预期工作。为此,请像示例中一样添加 @EnableAutoconfiguration 注释。

Please note that you are using Spring Boot, so you are encouraged to avoid XML configuration. You can get the same result with no beans.xml at all. Although, if you still need it, you can mix XML with annotations.

请注意,您使用的是 Spring Boot,因此鼓励您避免使用 XML 配置。您可以在根本没有 beans.xml 的情况下获得相同的结果。不过,如果您仍然需要它,您可以将 XML 与注释混合使用。

I have uploaded both sample projects to GitHub, please check.

我已将两个示例项目上传到 GitHub,请检查。

https://github.com/plopcas/example-spring-boot/tree/master/spring-boot-xml

https://github.com/plopcas/example-spring-boot/tree/master/spring-boot-xml

https://github.com/plopcas/example-spring-boot/tree/master/spring-boot

https://github.com/plopcas/example-spring-boot/tree/master/spring-boot

Hope this helps.

希望这可以帮助。