Saturday, 19 October 2019

Spring Boot Dev tools: Property defaults



Below table summarizes the properties that are enabled or disabled by spring boot dev tools.

Property
Value
spring.freemarker.cache
False
spring.groovy.template.cache
False
spring.mustache.cache
False
server.servlet.session.persistent
True
spring.h2.console.enabled
True
spring.resources.cache.period
0
spring.resources.chain.cache
False
spring.template.provider.cache
False
spring.mvc.log-resolved-exception
True
server.error.include-stacktrace
ALWAYS
server.servlet.jsp.init-parameters.development
True
spring.thymeleaf.cache
false

spring.reactor.stacktrace-mode.enabled
True

Reference

Let’s try to find out the default properties using an application.
Step 1: Create a template project.
Go to 'https://start.spring.io/'.

Give the group as 'com.sample.app', Artifact as 'DevToolDemo'.
Add below dependencies.
a.   Spring Web Starter

b.   Spring Boot DevTools


Click on ‘Generate the project’ button.

Extract the downloaded zip file.

Step 2: Import the maven project from step 1 to Eclipse.
Imported project structure looks like below.

Step 3: Add controller.

Create a package 'com.sample.app.DevToolDemo.controller' and define HomeController.java like below.

HomeController.java
package com.sample.app.DevToolDemo.controller;

import java.util.Iterator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
public class HomeController {

    @Autowired
    private Environment env;

    @RequestMapping("/")
    public String homePage() {
        return "Welcome to Spring boot Application Development";
    }

    @RequestMapping("/configs")
    public Map<String, Object> configs() {
        Map<String, Object> properties = new HashMap<>();

        Iterator iterator = ((AbstractEnvironment) env).getPropertySources().iterator();

        while(iterator.hasNext()) {
            PropertySource propertySource = (PropertySource) iterator.next();
            
            if (propertySource instanceof MapPropertySource) {

                String[] propertyNames = ((MapPropertySource) propertySource).getPropertyNames();

                for (String propName : propertyNames) {
                    properties.put(propName, propertySource.getProperty(propName));
                }

            }

        }
        
        return properties;
    }

}

Step 4: Create ‘application.properties’ file under src/main/resources folder.

application.properties
server.port=9999


DevToolDemoApplication.java
package com.sample.app.DevToolDemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DevToolDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DevToolDemoApplication.class, args);
    }

}

Total project structure looks like below.


Run DevToolDemoApplication.java.   

Open the url 'http://localhost:9999/configs' in browser, you can see below configurations.

You can confirm the default properties from the same response.

Note
If you don’t want property defaults to be applied you can set spring.devtools.add-properties to false in your application.properties.

You can download complete working application from this link.


Previous                                                    Next                                                    Home

No comments:

Post a Comment