Spring Boot uses application.properties
or application.yml
for configuring the various application level settings. As we are aware that spring boot works with the opinionated default values that are more sensible to your applications. Most of the time you may prefer to override the default values with your own configurations.
In this tutorial I am going to explain about some of the important properties used for spring boot configurations that are frequently used in the spring boot application and you may wish to override those values.
Note: The application.properties
is optional and spring boot could start running without this configuration file.
Spring Boot Configurations
Table of Contents:
- Properties File Location
- Application Configuration File Name
- Server Port Number
- Context Path
- Application Reload
- Enable H2 Web Console
- Enable Actuator EndPoints
- Log File Configurations
- Enable Favicon Image
- View Resolver Configuration
1. Properties File Location
Before start looking at the configuration properties, lets understand the location of properties file. You must keep your application.properties
in any of the following locations.
Spring Boot Configuration File Locations:
/config
directory under the current directory.- Current Directory
/config
under the classpath- Root of the classpath
The search of the properties file start from the lower of the above list. If you have properties file in the multiple locations, then higher location in the above list will override the files found in the lower locations.
Any other locations other than the mentioned above will not be searched by spring boot to fetch the configuration files unless you explicitly modify the location of the properties file.
If you are looking for a more in-depth tutorial on spring boot configuration file, please read our tutorial that talks about External Configurations for Spring Boot.
2. Application Configuration File Name
By default spring boot searches for the file name application.properties
or application.yml
to load the application configurations. If you have only one spring boot application then there is no need to worry about changing the configuration properties file name. But, if you have more than one spring boot applications, then you may like to change the properties file name.
But, these names are decided at the very early stage of the spring boot startup, so we can not update the values directly in the application.properties
or application.yml
file. Instead, we have to pass the parameters spring.config.name
and spring.config.location
as an argument while starting the application.
Something like this:
$ java -jar springapp.jar --spring.config.name=myproject
or
$ java -jar springapp.jar --spring.config.location=classpath:/my.properties,classpath:/override.properties
3. Server Port Number
Most of the web servers, the default server port number is 8080. But, in some cases, you want to change the port number that is different from the default one. Please add the following lines to your application.properties
:
server.port=7070
If you are using YAML based configuration:
server: port: 7070
Programmatically customize the server port as follows:
@Component public class CustomizationBean implements EmbeddedServletContainerCustomizer { @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setPort(7070); } }
4. Context Path
Spring Boot sets /
as the default context root for the web applications if you are not configuring your own context root. If you are interested in updating the context path, please add the following lines in your application.properties
file:
server.contextPath=/webapp
If you are using yaml configuration:
server: contextPath:/webapp
For programmatic changes please add the following code:
@Component public class CustomizationBean implements EmbeddedServletContainerCustomizer { @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setContextPath("/webapp"); } }
5. Application Reload
Since spring boot 1.3.0, any changes to classpath will trigger an application restart. This feature is introduced as part of the Spring Boot DevTools module. You can enable or disable this feature by adding the following lines to application.properties
file:
spring.devtools.livereload.enabled=false
If you are interested in learning more about live reload and spring boot devtools features, please read our comprehensive tutorial on spring boot devtools module.
6. Enable H2 Web Console
H2 database is an in-memory database that is frequently used at development phase of the application due to its simplicity and easy configurations. This database offers web console module for managing the database used by your applications. With the use of web console, a developer can view the tables and run the query to manipulate database at runtime.
By default, spring boot disables the web console. You can enable and modify the path where web console is accessible by adding the following lines in your application.properties
:
spring.h2.console.enabled=false spring.h2.console.path=/h2-console
7. Enable Actuator EndPoints
One of the nice features in spring boot is actuator endpoints. This enables the metrics for your applications that are very useful for the production applications. By default, this is disabled by spring boot configurations. You can enable this feature by adding the following lines in your application.properties
:
endpoints.enabled=true
Once you have added the above entry into your configuration properties file, the default endpoints will be enabled and exposed as a REST Service that returns the JSON result back to the web browser.
Please read the more comprehensive tutorial on spring boot actuator endpoints that covers every important feature of spring boot actuator configurations.
8. Log File Configurations
It is very simple to configure the log file information. Some of the common logging configurations are log file location, log file name, configuration file, etc. Please add the following lines in your application properties file:
logging.config=# Location of the logging configuration file. For instance 'classpath:logback.xml' for Logback logging.file= # Log file name. For instance 'myapp.log' logging.level.*= # Log levels severity mapping. For instance 'logging.level.org.springframework=DEBUG' logging.path= # Location of the log file. For instance 'D:/log'
Apart from the above key properties, many other properties are available to customize the logging configurations. By default, spring boot supports the LogBack logging mechanism. We have written a detailed tutorial on logging configurations in spring boot that is worth reading to get a good understanding on spring boot logging.
9. Enable Favicon Image
Favicon is the small icon that is displayed on top of the browser, generally that is will be the tiny logo of the application. Add the following properties entry in your Spring MVC application to enable the favicon:
spring.mvc.favicon.enabled=true
10. View Resolver Configuration
When we are using the view resolver, we have to specify the prefix and suffix details to the InternalResourceViewResolver. That can be achieved by adding the following entries:
spring.mvc.view.prefix= /WEB-INF/JSP/ spring.mvc.view.suffix= .jsp
The above properties files are used for working with the Spring MVC application.
Summary
In this tutorial, I have walked you through the some of the important spring boot properties configurations for enabling and disabling the features such as logging, actuator, favicon, spring MVC view resolver, dev tools features, h2 web console, etc. with brief explanation that helps you to get started with that specific features.
Apart from the above list, this tutorial explained the steps to change the config file name, locations and the order of the precedence on finding the config files at spring boot startup (Also Read: Spring Boot Startup Events).
If you are using spring boot in your application, I am happy to help you on resolving any issues you are encountering with spring boot projects. Please send me a mail at Krishnas at javabeat.net with your questions.
Thank you for reading my blog!! Happy Reading!!