gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:46:38 25 4
gpt4 key购买 nike

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

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

提前致谢。

更新:

按照 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)

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

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

因此,我尝试自己实现类 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”打印到控制台,文档根目录实际设置。

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

最佳答案

对原始问题最正确的答案是,在一个位置没有(技术上也不可能)详尽无遗的列表。我们可以并且将尽可能多地记录它(如果时间允许 - 感激地接受贡献)。有一个 list of properties在用户指南中,它几乎适用于 Spring Boot 本身支持的所有内容(但不适用于构建在它之上的其他库)。最终列表来自搜索 @ConfigurationProperties@Value 注释的源代码。 howto docs 中还有一些一般性建议.

关于spring - Spring Boot 中 application.properties 可用的属性列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20069130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com