Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of properties available for application.properties in Spring Boot?

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?

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.

Thanks in advance.

UPDATE:

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

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)

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.

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.

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()));
            }
        }
    }
}

And added following line to application.properties.

sample.server.documentRoot: someDirectoryName

...And it works!

"############## 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?

like image 996
zeodtr Avatar asked Nov 19 '13 10:11

zeodtr


People also ask

How do I get a list of spring boot application properties?

To see all properties in your Spring Boot application, enable the Actuator endpoint called env . This enables an HTTP endpoint which shows all the properties of your application's environment. You can do this by setting the property management.

How many spring boot application properties are there?

There are sixteen categories of Spring Boot Property are as follows: Core Properties.

What is application properties in spring boot?

Properties File Properties files are used to keep 'N' number of properties in a single file to run the application in a different environment. In Spring Boot, properties are kept in the application. properties file under the classpath. The application.properties file is located in the src/main/resources directory.

How do you read all properties from application properties in spring boot?

Another very simple way to read application properties is to use @Value annotation. Simply annotation the class field with @Value annotation providing the name of the property you want to read from application. properties file and class field variable will be assigned that value.


1 Answers

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.

like image 128
Dave Syer Avatar answered Oct 13 '22 07:10

Dave Syer