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

11
Spring Boot文档说我们可以在application.properties文件中设置属性。
但我找不到列出可设置属性的文档。
我该去哪里找这样的文档呢?
例如,我想为嵌入式servlet设置documentRoot。
我发现AbstractEmbeddedServletContainerFactory.java中实现了setDocumentRoot()方法。
但我不知道何时或在哪里调用该方法,或者可在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将其转换为Bean的(请参见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”被打印到控制台,并且文档根目录实际上已经设置。

所以,我很满意,但这是正确的方法吗?


使用server.document-root作为属性名称。基本上,将属性名称加上-即可得到属性名称。因此,server.context-path设置了应用程序的基本URL。另请参见https://github.com/spring-projects/spring-boot/issues/121。 - M. Deinum
谢谢@M.Deinum,但是它不起作用。我已经更新了我的问题,并附上了新代码。 - zeodtr
还要感谢@M.Deinum提到了Spring Boot问题。 - zeodtr
ServerProperties(以及所有标记为@ConfigurationProperties的bean)绑定到外部属性,正如您所发现的那样。它们还以“松散”的方式绑定(因此连字符和下划线可以代替驼峰式)。但是ServerProperties没有documentRoot(因此会出现错误消息)。您究竟想要实现什么目标? - Dave Syer
@DaveSyer 谢谢您提供的信息。我想要能够通过在application.properties中指定目录来更改文档根目录,而不受类路径和当前目录的限制。请参见我的另一个问题(https://dev59.com/VXnZa4cB1Zd3GeqPoUxD),您已经阅读过了。 - zeodtr
2个回答

8
原问题的最正确答案是,没有(并且在技术上不能有)一个详尽的列表在单个位置。我们可以和将会尽可能地记录它(当时间允许时 - 欢迎贡献)。在用户指南中有一个属性列表,几乎包含Spring Boot本身支持的所有内容(但不包括其他构建在其之上的库),请参见此处。权威的列表来自于搜索源代码中的@ConfigurationProperties@Value注释。还有一些一般性的建议,请参见这里的文档。

非常感谢您,这正是我想知道的。 - Whiteship
application.yml 的链接现在无效。 - TheKojuEffect
Dave,有没有一种方法可以在不实例化整个SpringApplication实例的情况下获取所有已解析属性的列表?也就是说,在应用程序完全启动并创建对象图之前的某个时刻,必须知道所有已解析属性的完整集合。你能在运行时获取吗?我想要一个属性列表来检查。 - ae6rt
使用当前的代码(1.1.5),我认为启动应用程序是获得完整洞察力的最佳方式。如果您想要一些超出范围的东西,可以查看Stephane开始的工作:https://github.com/snicoll/spring-boot/tree/gh-1001(针对https://github.com/spring-projects/spring-boot/issues/1001)。 - Dave Syer
Spring Boot本身支持的属性在文档中有详细说明:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#appendix.application-properties - Dave Syer
显示剩余4条评论

0

您的问题非常恰当。当我在寻找ajp配置(http://www.appsdev.is.ed.ac.uk/blog/?p=525)时,我发现了一种更简单的替代解决方案:

在application.properties中设置:

tomcat.server.document-root=/your/document/root/

在你的应用程序类中:
1)使用@Value注释放置属性
@Value("${tomcat.server.document-root}")
String documentRoot;

2) 然后添加 bean

@Bean
public EmbeddedServletContainerFactory servletContainer() {

    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();

    if (StringUtils.isNotBlank(documentRoot)) {
        tomcat.setDocumentRoot(new File(documentRoot));
    }

    return tomcat;
}

你完成了!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接