Spring说我们可以在application.properties文件中设置属性。
但我找不到列出可以设置的可用属性的文档。
我在哪里能找到这样的文件?
例如,我想为嵌入式servlet设置documentRoot。
我发现setDocumentRoot()方法是在AbstractEmbeddedServletContainerFactory.java中实现的。
但我不知道何时何地调用该方法,也不知道可以在application.properties中设置的属性名称。
我认为这应该很容易,因为Spring的主要目的是简化配置。
提前谢谢。
更新:
正如M. Deinum暗示的那样,我在application.properties中添加了‘server.Documt-root: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。
顺便说一句,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)。
守则如下:
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“被打印到控制台,文档根实际上被设置。
所以,我现在很高兴,但这样做对吗?
发布于 2013-11-19 15:42:35
发布于 2016-03-05 18:46:02
你的问题非常恰当。在寻找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;
}你完蛋了!
https://stackoverflow.com/questions/20069130
复制相似问题