Spring boot - Application.properties 中的用户定义变量

标签 spring spring-boot spring-mvc spring-data

我正在尝试在 application.properties 文件中添加自定义变量。当我添加时,IDE(Eclipse 2019-06 (4.12.0) 和 Spring Tool Suite 4.3.1)显示黄色警告“abc.def 是未知属性”。

问题类似于Spring boot - custom variables in Application.properties . 我在答案中尝试了@StephaneNic​​oll 给出的方法。它仍然显示相同的警告。其实我打开项目https://github.com/snicoll-demos/spring-boot-configuration-binding在 Eclipse 中。它仍然给出相同的警告。还有什么我需要做的吗?

这是一个小麻烦。但是想摆脱它。

最佳答案

您必须将 spring-boot-configuration-processor 添加到您的项目中:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>

然后使用注释 @ConfigurationProperties 及其前缀创建您的属性类,例如:

@Data
@ConfigurationProperties(prefix = "abc")
@Validated
public class AbcProperties {

    /**
     * Description of the property 'def'.
     */
    @NotNull private Long def = 0L;  // <- default value of the property

    // other props...
}

然后启用它:

@SpringBootApplication
@EnableConfigurationProperties(AbcProperties.class)
public class AbcApplication {
    //...
}

然后重建您的项目。完成这些步骤后,IDE 将正确看到您的 Prop 。

更多信息:

关于Spring boot - Application.properties 中的用户定义变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57116707/

相关文章:

java - spring auth错误没有AuthenticationProvider

tomcat - 如何使用 Tomcat 部署(和访问)Spring Boot war? (使用入门指南)

java - Keycloak roleResource.getRoleUserMembers();抛出 403 错误

java - @ModelAttribute 像 Swagger 一样显示在 body 中

spring - Spring MVC-HttpSession.setAttribute和model.addObject之间的区别

java - Freemarker + Spring MVC 教程

java - 每层集成测试是一个好习惯吗?

java - 如何使用 github、eclipse、maven 为多个开发人员设置动态 Web 应用程序?

java - 如何实现 Multi-Tenancy Spring Boot 应用程序(每个用户都有自己的数据库)

spring - 在 SpringExtension 之前执行扩展