Spring Boot文档说我们可以在application.properties文件中设置属性 .
但是我找不到列出可以设置的可用属性的文档 .
我在哪里可以找到这样的文件?
例如,我想为嵌入式servlet设置documentRoot .
我发现setDocumentRoot()方法是在AbstractEmbeddedServletContainerFactory.java中实现的 .
但我不知道何时何地调用该方法,或者可以在application.properties中设置的属性名称 .
我认为应该很容易,因为Spring Boot的目的是简化配置 .
提前致谢 .
UPDATE:
正如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 = 2689020 , 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”将打印到控制台,并且实际设置了文档根目录 .
所以,我现在很高兴,但这是正确的方法吗?
3 回答
目前最原始问题的最正确答案是,在一个地方没有(并且在技术上不可能)一个详尽的清单 . 我们可以并且将尽可能多地记录(当时间允许时 - 感激地接受捐款) . 在用户指南中有一个手动策划的list of properties,其中包含非详尽且可能不准确的列表 . 最终列表来自搜索
@ConfigurationProperties
和@Value
注释的源代码,以及偶尔使用RelaxedEnvironment
(c.f . here) . howto docs中也有一些一般性的建议 .你的问题非常合适 . 当我在寻找ajp配置(http://www.appsdev.is.ed.ac.uk/blog/?p=525)时,我找到了另一种更简单的解决方案:
在application.properties中设置:
在您的应用程序类中:
1)使用@Value注释放置属性
2)并添加bean
你完成了!
Here 您可以在spring boot中找到可在 application.properties 文件中使用的所有属性的列表 .