Spring Boot文档说我们可以在application.properties文件中设置属性。
但我找不到列出可设置属性的文档。
我该去哪里找这样的文档呢?
例如,我想为嵌入式servlet设置documentRoot。
我发现AbstractEmbeddedServletContainerFactory.java中实现了setDocumentRoot()方法。
但我不知道何时或在哪里调用该方法,或者可在application.properties中设置的属性名称。
我认为这应该很容易,因为Spring Boot的目的就是使配置更加简单。谢谢!
提前致谢。
更新:
如M. Deinum所建议,我将'server.document-root:someDirectoryName'添加到application.properties中,但出现了以下错误。
我认为这是由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方法。
但我找不到列出可设置属性的文档。
我该去哪里找这样的文档呢?
例如,我想为嵌入式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. DeinumServerProperties
(以及所有标记为@ConfigurationProperties
的bean)绑定到外部属性,正如您所发现的那样。它们还以“松散”的方式绑定(因此连字符和下划线可以代替驼峰式)。但是ServerProperties
没有documentRoot
(因此会出现错误消息)。您究竟想要实现什么目标? - Dave Syer