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

2024-04-22

我有一个 Spring Boot 应用程序

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

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

Under 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"


您可以通过在 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

在输出中你会得到你好世界如果你运行该程序。

确保您已启用自动配置。如果不这样做,它将无法按预期工作。为此,请添加 @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-xml

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

希望这可以帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Boot - 无法从 application.properties 在 xml 中解析属性 的相关文章

随机推荐

  • 在 SQL Server 中的韩语单词(任何 Unicode 单词)中查找韩语字母表(任何 Unicode 字符)的索引

    我需要按姓名搜索人员 这里的人名可以是英文 韩文或中文 为此我使用了Like搜索条件Name如下 select from MyTable where Name like N t 上述声明是给所有包含字母的用户t 但这不适用于韩语或中文 就像
  • chrome扩展后台页面中的jQuery使用

    Goal 我正在尝试使用此样板代码使用在线词典 API 进行查找 以查找所选单词并返回定义 Problem 我已经单独测试了实际的 jQuery ajax 调用 效果很好 另外 我可以在页面上获取所选的单词 然而 由于某种原因 我实际上遇到
  • 从无符号除法结果分配时有关符号转换的警告

    我编译了以下代码 Wsign conversion int main unsigned int a 8 int b a 8u warning implicit conversion changes signedness unsigned i
  • 如何防止“CoreData无法完成故障”?

    我们偶尔会收到 CoreData 无法完成故障 的信息 我们已阅读 Apple 文档 但不清楚允许保留哪些内容 我们非常小心地为每个线程创建一个上下文等 但是 我们的应用程序正在做的一件事是我们在 UIViewController 上保留
  • PHP 空 $_POST

    我通过 HTTP POST 向 PHP 发送数据 这对于短于 8MB 8192KB 的数据来说效果很好 但是当发送的数据量更大时 PHP 会显示 POST变量为空 我强调的是 POST变量甚至不包含帖子字段的名称 它作为空数组存在 临界点似
  • Python RuntimeError:字典在迭代期间更改了大小[重复]

    这个问题在这里已经有答案了 我正在练习一个非常简单的Python代码 我试图找到它的解决方案 但找不到 def del contacts for name number in d1 items if del name name del d1
  • 查找两个数组中的重复值,Python

    我有两个数组 A 和 B 每个数组约有 50 000 个值 每个值代表一个 ID 我想创建一个包含三列的 pandas 数据框 col1 数组 A 中的值 col2 数组 B 中的值 col3 带有标签 唯一 或 重复 的字符串 在每个数组
  • Process.Start 返回 null 时查找进程 ID?

    如果我尝试通过调用打开图像文件 视频或网站Process Start filepath 直接 那么它通常会成功 但是 Process Start 的返回值有时将为 null 正如这里所讨论的 https stackoverflow com
  • 将硬编码文件路径更改为 VBA 中提示的用户?

    现在 我有一个用于 Word 的 VBA 宏 它可以解析文档中的某种字体 并将所选类型的所有字体输出到文本文件 我打开文本文件的硬编码行是这样的 Open C Documents and Settings Output txt For Ou
  • 实现共享功能的标准方法

    我需要在 iOS 中实现 共享为 功能 例如 一个按钮名为 共享为 并弹出一个对话框 其中包括电子邮件 短信 Facebook Twitter 等项目 我想知道是否有一个标准对话框可以完成这项工作 经过搜索 我发现在 iOS6 中使用 UI
  • 如何使用 C 客户端通过 ActiveMQ 启用 SSL

    我已经配置了 ActiveMQ http activemq apache org http activemq apache org 通过以下方式使用 ssl 上下文RedHat 的 SSL TLS 教程 https access redha
  • HTML 树全宽悬停效果

    我有一个 html 树 主要由嵌套的无序列表组成 我需要为每个叶子创建全宽悬停效果 类似于 Windows 文件菜单树悬停效果 悬停效果是 div div 具有背景颜色和边框 我做了一个模拟全宽度的黑客 但侧面边框不再可见 我可以使用 CS
  • C++/CLI:CA2123:需要 SecurityCriticalAttribute?

    我对这样的错误有点迷失 警告 7 CA2123 Microsoft Security 添加以下安全属性 到 RithmicConnector 连接 字符串 为了匹配基地的 LinkDemand 方法 IConnector Connect S
  • 如何向 Monaco 编辑器添加新的语言语法?

    我添加了我的语言并按照此处的说明进行构建https github com Microsoft monaco languages https github com Microsoft monaco languages npm run prep
  • 选择日期最高的行

    我的表中有一些重复的值 我只想选择那些具有最新 最高日期的值 即 ID Type Name Value Date 1 FRUIT APPLE Imported 2011 03 19 22 08 13 5 FRUIT LEMON Import
  • Promise.all:解析值的顺序

    看着MDN https developer mozilla org en US docs Web JavaScript Reference Global Objects Promise all它看起来像values传递给then Promi
  • C 基本头命令

    我正在尝试为我的编程课从 Linux 重新创建 head 和 tail 命令 我们刚刚开始使用 C 所以我对分配内存和指针的想法很陌生 我想知道为什么这不起作用 include
  • C#:如何将 long 转换为 ulong

    如果我尝试使用 BitConverter 它需要一个字节数组 但我没有 我有一个 Int32 我想将其转换为 UInt32 在 C 中这没有问题 您只需要一个简单的演员阵容即可 由于这样做可能会丢失精度 因此转换是显式的 long x 10
  • 将具有 key=value 对的字符串解析为 JSON

    我的节点应用程序接收以下格式的一系列字符串 a x b y c z 即包含多个空格分隔的字符串key value pairs 将此类字符串转换为以下形式的 JSON 对象的最巧妙方法是什么 a x b y c z 我打赌有一个单行解决方案
  • Spring Boot - 无法从 application.properties 在 xml 中解析属性

    我有一个 Spring Boot 应用程序 My Configuration class使用加载 xml 配置 ImportResource path to xml 其中包含以下行