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

标签 java xml spring spring-boot properties-file

我有一个 spring boot 应用程序

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

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

src/main/resources 下,我有包含以下内容的 application.properties 文件:

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

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

原因:java.lang.IllegalArgumentException:无法解析字符串值“${log.directory}/file.ext”中的占位符“log.directory”

最佳答案

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

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

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

  1. 当前目录的/config 子目录。
  2. 当前目录
  3. 类路径/配置包
  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());
    }
}

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

应用程序属性

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

希望这对您有所帮助。

关于java - Spring Boot - 无法在 application.properties 的 xml 中解析属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26406982/

相关文章:

java - 使动态 Web 项目(jsp、servlet)在本地 wifi 网络上可用

Java 枚举类型名称反射

java - Log4J2:替换参数无法正常工作

java - 匹配的通配符是严格的,但找不到元素 'context:annotation-config' 的声明

javascript - 使用 xml 使用 jquery 创建无序列表

java - xml如何定义数据?

Java "not removing"队列

java - 使用 Spring Data Rest 时公开所有 ID

java - 如何消费物联网设备产生的数据?

java - 为什么会引发 'deleted entity passed to persist' -异常?