我有一个Spring Boot项目,我想通过前缀映射这些属性,在本例中,前缀是根:

应用程序.属性:

root.prop = xxxx
root.prop2 = yyyy
root.prop3 = zzzz

我不想将我的文件类型从属性更改为YAML.

推荐答案

"轻松过瘾":

package com.example.demo;

import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

@SpringBootApplication 
@EnableConfigurationProperties(MyProps.class) // !
public class DemoApplication {

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

    @Bean // a simple test bean
    CommandLineRunner cmd(/*@Autowired*/ MyProps props) {
        return (args) -> {
            System.err.println(props.getRoot());
        };
    }
}

@ConfigurationProperties // ! ..no prefix, because we are "close to"/root! 
class MyProps {

    // this gets our "root." prefix
    private final Map<String, ?> root = new HashMap<>();

    // since initialized, getter sufficient 
    public Map<String, ?> getRoot() {
        return root;
    }

}

使用,应用程序.属性:

root.prop = xxxx
root.prop2 = yyyy
root.prop3 = zzzz

和pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

...控制台打印:

...
2022-10-05 18:12:20.101  INFO 9684 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 0.834 seconds (JVM running for 1.111)
{prop2=yyyy, prop=xxxx, prop3=zzzz}
------------------------------------------------------------------------
...

要将属性驻留在"非默认位置"(例如foo.properties),只需:

//@ ... ion
@EnableConfigurationProperties(MyProps.class)
@PropertySource("classpath:/foo.properties") // ! ...

..但请注意:application.properties人优先!

回头见:

Java相关问答推荐

Java 8中的多个字段和计数

如何调用Firebase Realtime Database中的子图像列表到android studio中的回收器视图?

@ IdClass with @ Inheritance(策略= InheritanceType. SINGLE_TABLE)

上下文初始化期间遇到异常-使用Java配置配置HibernateTemplate Bean时

Spring Boot 3.2.2中的@Inject和@Resource Remove

如何让DTO接受空字符串字段,但如果它们不为空,则应用JPA验证?

如何使用值中包含与号的查询参数创建一个java.net.URI

格式中的特定回录键-值对

如何在JUNIT测试中覆盖ExecutorService?

删除打印语句会影响功能...腐败在起作用?

Quarkus:运行时出现EnumConstantNotPresentException

如何在ImageIO或十二只猴子中旋转TIFF CMYK图像?

除0错误/抱歉我的句子是PT

JavaFX标签中的奇怪字符

模拟JUnit未检测到返回字符串的方法的任何声纳覆盖

运行我的模块化程序时,ModuleLayer 找不到我的资源,但它可以找到我的模块化 jar

没有 Stream API 的 Collection GroupBy 聚合

如何操作 h2 中的数组值?

使用 SLF4J 时如何动态 Select 消息的日志(log)记录级别?

使用 dfs 迭代在有向图中查找循环的代码的时间和空间复杂度是多少