Spring Boot 中 application.properties 可用的属性列表?

2024-05-13

Spring Boot文档说我们可以在application.properties文件中设置属性。
但我找不到列出可以设置的可用属性的文档。
我在哪里可以找到这样的文档?

例如,我想为嵌入式servlet设置documentRoot。
我发现setDocumentRoot()方法是在AbstractEmbeddedServletContainerFactory.java中实现的。
但我不知道何时何地调用该方法,也不知道可以在 application.properties 中设置的属性名称。
我认为这应该很容易,因为 Spring Boot 的目的就是简化配置。

提前致谢。

UPDATE:

正如 M. Deinum 所建议的,我将“server.document-root: someDirectoryName”添加到 application.properties 中,但发生了以下错误。

Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'document-root' of bean class [org.springframework.boot.context.embedded.properties.ServerProperties]: Bean property 'document-root' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1057)
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:915)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:82)
    at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:730)
    at org.springframework.validation.DataBinder.doBind(DataBinder.java:626)
    at org.springframework.boot.bind.RelaxedDataBinder.doBind(RelaxedDataBinder.java:78)
    at org.springframework.validation.DataBinder.bind(DataBinder.java:611)
    at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:232)
    at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:204)
    at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessAfterInitialization(ConfigurationPropertiesBindingPostProcessor.java:312)
    ... 31 more

我认为这是因为 org.springframework.boot.context.embedded.properties.ServerProperties 的实现方式。 (看https://github.com/spring-projects/spring-boot/blob/97cb7f096798ecd016de71f892fa55585d45f5eb/spring-boot/src/main/java/org/springframework/boot/context/embedded/properties/ServerProperties.java https://github.com/spring-projects/spring-boot/blob/97cb7f096798ecd016de71f892fa55585d45f5eb/spring-boot/src/main/java/org/springframework/boot/context/embedded/properties/ServerProperties.java)

它声明“@ConfigurationProperties(name =“server”,ignoreUnknownFields = false)”。 因此,它管理以“server”开头的应用程序属性,并且不允许未知的属性名称。
并且它不支持 documentRoot getter/setter。

顺便说一句,ServerProperties 类由 org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration 创建为 Bean(请参阅https://github.com/spring-projects/spring-boot/blob/97cb7f096798ecd016de71f892fa55585d45f5eb/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.java https://github.com/spring-projects/spring-boot/blob/97cb7f096798ecd016de71f892fa55585d45f5eb/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.java)以便它可以参与配置过程。

因此,我尝试自己实现类似 ServerProperties 的一个和类似 ServerPropertiesAutoConfiguration 的一个。
代码如下:

package com.sample.server;

import java.io.File;

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SampleConfiguration
{
    @Bean
    public SampleServerProperties sampleServerProperties()
    {
        return new SampleServerProperties();
    }

    @ConfigurationProperties(name = "sample.server")
    public static class SampleServerProperties
        implements EmbeddedServletContainerCustomizer 
    {
        private String documentRoot;
        public String getDocumentRoot()
        {
            return documentRoot;
        }
        public void setDocumentRoot(String documentRoot)
        {
            System.out.println("############## setDocumentRoot");
            this.documentRoot = documentRoot;
        }

        @Override
        public void customize(ConfigurableEmbeddedServletContainerFactory factory)
        {
            if (getDocumentRoot() != null)
            {
                factory.setDocumentRoot(new File(getDocumentRoot()));
            }
        }
    }
}

并将以下行添加到 application.properties。

Sample.server.documentRoot: someDirectoryName

...而且它有效!

“############## setDocumentRoot”打印到控制台,文档根目录实际上已设置。

所以,我现在很高兴,但这是正确的做法吗?


对原始问题的最正确答案是,在单个位置中没有(而且从技术上讲也不可能)有详尽的列表。我们可以并且将会尽可能多地记录这些内容(如果时间允许 - 衷心接受贡献)。有一个属性列表 https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#appendix.application-properties用户指南中的内容对于 Spring Boot 本身支持的几乎所有内容都是准确的(但不适用于构建在其之上的其他库)。最终列表来自搜索源代码@ConfigurationProperties and @Value注释。中还有一些一般性建议如何文档 https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto.properties-and-configuration.

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

Spring Boot 中 application.properties 可用的属性列表? 的相关文章

随机推荐

  • NHibernate Projections - 如何投影集合

    有一个场景 我只需要从实体中选择单个 几列 但在查询中选择多个子项 我一直在尝试投影 但在集合属性上出现错误 这是很正常的情况 但找不到有关投影集合的信息 仅属性 Customer customerAlias null Order orde
  • '用户“postgres”的密码验证失败'

    我已经安装了 PostgreSQL 8 4 Postgres 客户端和 Pgadmin 3 控制台客户端和 Pgadmin 的用户 postgres 身份验证失败 我输入了用户 postgres 和密码 postgres 因为它以前有效 但
  • 绘制具有数据子集的图层时,因子水平的顺序会发生变化

    我试图控制图例中项目的顺序ggplot2我查找了其他一些类似的问题 并发现了如何更改我正在绘制的因子变量的水平顺序 我正在绘制 12 月 1 月 7 月和 6 月 4 个月的数据 如果我只对所有月份执行一个绘图命令 它会按预期工作 图例中排
  • 永远不会在 ios 的 google API 中获取上传数据进度

    我在我的应用程序中使用 Google Drive API 从我的应用程序上传文件 到目前为止 我成功了 并找到了上传所有类型文件的良好结果 我跟着谷歌示例 https developers google com drive examples
  • 如何自定义 Mailboxer 电子邮件的模板?

    它会自动发送电子邮件 其中写着 You have a new message subject You have received a new message Body Visit http example com and go to you
  • 如何通过 IPTables 阻止一些特殊的用户代理

    我需要阻止来自任何来源的数据包 其中包括使用 IPTables 的一些特殊用户代理 但我不想通过管理它 htaccess或阿帕奇 有什么办法吗 你可以这样做 iptables A INPUT p tcp dport 80 m string
  • 如何组合 3 个或更多 CompletionStages?

    如果有 2 个 CompletionStages 我可以将它们与thenCombine method CompletionStage a aCompletionStage getA CompletionStage b bCompletion
  • 有没有办法在 VSCode 中保存时运行 go 测试,并将其输出到终端?

    现在我有几个项目在VSCode中运行 运行起来相当繁琐go test每次我编写新代码时 我宁愿立即看看我是否破坏了某些东西 我知道在 Javascript 中我可以在每次保存文件时运行测试 并将输出发送到终端 现在我正在使用 保存时运行 h
  • Rstudio 命令历史记录

    这些天我经常使用 Rstudio 但最近注意到我的命令不再存储在历史记录中 我不知道这是从什么时候开始的 但可能是在安装最新版本时发生的 关于问题可能是什么的任何想法吗 Thanks 这是我们在 v0 93 73 中引入并在 v0 93 7
  • 实体框架 - 循环更新属性

    我正在尝试找到一种方法来循环 EF 对象的属性并更新这些属性的值 更具体地说 我有 50 个字段 其中最多填充 50 个下拉列表 所有 50 个可能都需要填充 也可能不需要填充 为了解决这个问题 我有一个中继器 最多可以创建 50 个 DD
  • Jupyter 笔记本中未显示绘图

    我正在尝试为 Anscombe 数据集创建 2x2 图 加载数据集并分离数据集中的每个类 import seaborn as sns import matplotlib pyplot as plt anscombe sns load dat
  • 如何使用movntdqa避免缓存污染?

    我正在尝试编写一个 memcpy 函数 该函数不会将源内存加载到 CPU 缓存中 目的是避免缓存污染 下面的 memcpy 函数可以工作 但会像标准 memcpy 一样污染缓存 我正在使用带有 Visual C 2008 Express 的
  • 每个 CPU 核心处于 C0 电源状态的时间

    任何帮助弄清楚如何做到这一点都会很棒 在过去一秒内 每个 CPU 核心处于 C0 电源状态的时间有多少 这是针对 Mac 应用程序的 因此需要 Objective C cocoa 和 c OS X 没有任何公开 CPU c 状态的 API
  • Keras ImageDataGenerator 相当于 csv 文件

    我在文件夹中排序了一堆数据 如下图所示 我需要构建一个 DataIterator 以便将数据放入神经网络模型中 当数据是图像时 我找到了很多例子来解决这个问题 使用 Keras 类图像数据生成器及其方法流自目录 但当数据是 csv 结构时则
  • PHP:从 POP3 或 IMAP 下载传入电子邮件,解析它,并将其标记为服务器上的已读/删除

    我正在尝试将传入电子邮件添加到我的网络应用程序中 它是基于 CodeIgniter 和 PHP 构建的 据我所知 我还没有找到任何 CI 库来执行此操作 我想要做的是有一个控制器通过 POP3 或 IMAP 连接到我的邮箱 并检索消息 解析
  • 如何将 pem 公钥转换为 openssl RSA* 结构

    假设我必须像这样公开 pem 密钥 BEGIN PUBLIC KEY MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC7vbqajDw4o6gJy8UtmIbkcpnk O3Kwc4qsEnSZp TR fQi
  • 将数组从 C# 编组到 C++ 并返回:PInvokeStackImbalance

    我有一个 C 函数 我想从 C 访问它 问题是我不断收到 PInvokeStackImbalance 异常 但我不知道为什么 当检查异常被关闭时 一切都运行良好并且符合预期 我的 C 函数的签名是 extern C double solve
  • 我刚刚在 Visual Basic 中运行的 COM 对象的 CLSID 是什么

    我需要知道我刚刚运行的 COM 对象的 CLSIDCreateObject xxx xxx 我怎么才能得到它 蒂亚 拉法尔 我认为您无法直接从 VB 本身获取该信息 例如 作为调用的副作用或次要结果 CreateObject 但你可以阅读注
  • UIView 和 UITableView 中的 UITapGestureRecognizer 冲突

    我有一个UIView我在其中添加了一个UITapGestureRecognizer 在该视图中 我还有一个子视图 其中基本上是某种UITableView 问题是为什么不UITableView识别连续点击 而是始终转到点击手势识别器的处理程序
  • Spring Boot 中 application.properties 可用的属性列表?

    Spring Boot文档说我们可以在application properties文件中设置属性 但我找不到列出可以设置的可用属性的文档 我在哪里可以找到这样的文档 例如 我想为嵌入式servlet设置documentRoot 我发现set