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

标签 spring tomcat spring-boot

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/

相关文章:

Tomcat 上的 Java Web 应用程序 - 显示用户上传的文件

session - 如何在同一个tomcat下共享存储在不同webapps session 中的数据

java - Spring Boot JPA Hibernate CRUD 存储库中的查找表

java - 使用 Spring 初始化 SqlFactory,但在将数据源转换为其底层类时出现异常

java - JdbcTemplate 抛出异常

java - 如何在spring-boot中提供静态html内容页面

java - Kafka 多个消费者监听多个主题

java.lang.NumberFormatException : For input string XX while fetching the data from the database

javascript - Angular 和 Spring Rest api 通过 https 进行通信

java - 如何使用 Spring Security 对 Active Directory 服务器进行身份验证?