【问题标题】:Spring Boot - property could not be resolved in xml from application.propertiesSpring Boot - 无法从 application.properties 在 xml 中解析属性
【发布时间】:2014-12-11 23:20:16
【问题描述】:

我有一个 Spring Boot 应用程序

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

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

src/main/resources 下我有application.properties 文件,内容如下:

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

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

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

【问题讨论】:

  • 你有一个属性占位符解析器bean吗?
  • 没有。 Spring Boot不应该自动加载application.properties吗?
  • 我不太了解boot。您是否希望它从类路径中加载所有 .properties
  • 加载属性(让它们可用)与解析占位符(使用它们)不同。这就是我要开始寻找的地方。

标签: java xml spring spring-boot properties-file


【解决方案1】:

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

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

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

  1. 当前目录的 /config 子目录。
  2. 当前目录
  3. 类路径 /config 包
  4. 类路径根

我已经试过了,效果很好。

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>

示例.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

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

<?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

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

确保您已启用自动配置。如果您不这样做,它将无法按预期工作。为此,请添加 @EnableAutoconfiguration 注释,如示例中所示。

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

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

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

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

希望这会有所帮助。

【讨论】:

  • 我已经有了一个可以工作的 beans.xml 配置,只需要使用 spring-boot 来执行它。问题是我需要添加
  • 好的,是的,使用 context:property-placeholder 它同样有效。当您的属性文件被称为与 application.properties 不同的东西时,这特别有用。此外,当您拥有多个属性文件时,您的额外属性 order="1" ignore-unresolvable="true" 很有用。
  • 但正如您在我的 GitHub 项目 st-springboot 中看到的那样,由于 Spring Boot 支持的配置约定,这不是必需的。而且,你可以有一个 beans.xml,并将注解与 xml 配置混合在一起(st-springboot-xml),Spring Boot 会自动选择 application.properties 文件,假设你已经启用了自动配置(@EnableAutoconfiguration)。我已经更新了答案以强调这一点。无论如何,很高兴你解决了这个问题。如果您有时间,请检查项目,因为我认为这是对您问题的更具体的答案。问候。
猜你喜欢
  • 1970-01-01
  • 2019-09-18
  • 2021-05-17
  • 2015-12-19
  • 2018-12-18
  • 2023-04-11
  • 1970-01-01
  • 2021-12-25
  • 2017-09-19
相关资源
最近更新 更多