首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >application.properties在Spring中可用的属性列表?

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

Stack Overflow用户
提问于 2013-11-19 10:34:52
回答 2查看 12.5K关注 0票数 10

Spring说我们可以在application.properties文件中设置属性。

但我找不到列出可以设置的可用属性的文档。

我在哪里能找到这样的文件?

例如,我想为嵌入式servlet设置documentRoot。

我发现setDocumentRoot()方法是在AbstractEmbeddedServletContainerFactory.java中实现的。

但我不知道何时何地调用该方法,也不知道可以在application.properties中设置的属性名称。

我认为这应该很容易,因为Spring的主要目的是简化配置。

提前谢谢。

更新:

正如M. Deinum暗示的那样,我在application.properties中添加了‘server.Documt-root:application.properties’,但随后出现了错误。

代码语言:javascript
复制
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。

顺便说一句,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)将ServerProperties类创建为Bean,以便它能够参与配置过程。

所以,我试着自己实现ServerProperties(比如一个和ServerPropertiesAutoConfiguration)。

守则如下:

代码语言:javascript
复制
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

...And,它起作用了!

"############## setDocumentRoot“被打印到控制台,文档根实际上被设置。

所以,我现在很高兴,但这样做对吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-19 15:42:35

对最初问题的最正确的答案是,在一个地点没有(而且在技术上不可能)一个详尽的清单。我们可以并将尽可能多地记录它(当时间允许的时候用户指南中有一个属性列表,对于Spring本身所支持的几乎所有东西都是准确的(但其他构建在它之上的库)。最终的列表来自于搜索@ConfigurationProperties@Value注释的源代码。如何操作文档中也有一些一般性的建议。

票数 8
EN

Stack Overflow用户

发布于 2016-03-05 18:46:02

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

设置在application.properties中:

代码语言:javascript
复制
tomcat.server.document-root=/your/document/root/

在您的应用程序类中:

1)使用@Value分析将属性放置在

代码语言:javascript
复制
@Value("${tomcat.server.document-root}")
String documentRoot;

2)并添加bean

代码语言:javascript
复制
@Bean
public EmbeddedServletContainerFactory servletContainer() {

    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();

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

    return tomcat;
}

你完蛋了!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20069130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档