Spring Boot中可用于application.properties的属性列表? [英] List of properties available for application.properties in Spring Boot?

查看:88
本文介绍了Spring Boot中可用于application.properties的属性列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Spring Boot document says that we can set properties in application.properties file.
But I cannot find a document that lists available properties that can be set.
Where can I find such a document?

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

For example, I want to set documentRoot for embedded servlet.
I found that the setDocumentRoot() method is implemented in AbstractEmbeddedServletContainerFactory.java.
But I don't know when or where to call the method, or the name of property that can be set in application.properties.
I think it should be easy, since Spring Boot's very purpose is to ease the configuration.

谢谢.

更新:

正如Deinum先生建议的那样,我在application.properties中添加了"server.document-root:someDirectoryName",但是发生了以下错误.

As M. Deinum sugggested, I added 'server.document-root: someDirectoryName' to the application.properties, but following error occured.

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实现的方式. (请参阅

I think it is because of the way org.springframework.boot.context.embedded.properties.ServerProperties implemented. (See 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.

It declares '@ConfigurationProperties(name = "server", ignoreUnknownFields = false)'. So, it manages the application properties that starts with 'server', and disallowes unknown property name.
And it does not support documentRoot getter/setter.

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

BTW, ServerProperties class is made to a Bean by org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration (See https://github.com/spring-projects/spring-boot/blob/97cb7f096798ecd016de71f892fa55585d45f5eb/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.java) so that it can participate in the configuration process.

因此,我尝试自己实现类似ServerProperties的服务器和类似ServerPropertiesAutoConfiguration的服务器.
代码如下:

So, I tried to implement ServerProperties-like one and ServerPropertiesAutoConfiguration-like one myself.
The code is as follows:

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.

And added following line to application.properties.

sample.server.documentRoot:someDirectoryName

sample.server.documentRoot: someDirectoryName

...而且有效!

...And it works!

将"############## setDocumentRoot"打印到控制台,并实际设置了文档根目录.

"############## setDocumentRoot" is printed to the console, and the document root is actually set.

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

So, I'm happy now, but is this the right way to do it?

推荐答案

当前对原始问题的最正确答案是,单个位置没有(从技术上来说不可能)详尽的列表.我们会并且会尽可能地记录下来(在时间允许的情况下-非常感谢您的捐款).有一个手动编排的属性列表在用户指南中列出了不完整且可能不准确的列表.权威列表来自搜索源代码中的@ConfigurationProperties@Value批注以及偶尔使用的RelaxedEnvironment(cf howto docs .

The most correct answer to the original question at the moment is that there is not (and technically cannot be) an exhaustive list in a single location. We can and will document as much of it as we can (when time permits - contributions gratefully accepted). There is a manually curated list of properties with a non-exhaustive and possibly inaccurate list in he user guide. The definitive list comes from searching the source code for @ConfigurationProperties and @Value annotations, as well as the occasional use of RelaxedEnvironment (c.f. here). There is also some general advice in the howto docs.

这篇关于Spring Boot中可用于application.properties的属性列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆