In some
scenarios, we may not want to restart the application for specific files, and
we may want to include additional folders to be considered for restarting. We
can customize this kind of behavior using 'spring.devtools.restart.exclude' and
'spring.devtools.restart.additional-paths' properties.
Example
spring.devtools.restart.exclude=static/**,public/**
spring.devtools.restart.additional-paths=labels/**
Let’s see
the same by developing 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.
server.port=9999 spring.devtools.restart.exclude=static/**,public/** spring.devtools.restart.additional-paths=labels/**
Step 5:
Generated DevToolDemoApplication
Looks like below.
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);
}
}
Step 6:
Create staticFile.txt
under src/main/resources/static folder.
staticFile.txt
Hello
Step 7:
Create message.properties
file under src/main/resources/labels folder.
message.properties
appName=Chat
Server
version=1.23.4
Total
project structure looks like below.
Run
‘DevToolDemoApplication.java’.
Update
‘staticFile.txt’ and save the changes, you can observer no restart is triggered
by spring boot.
Now update
‘message.properties’ and save the changes, you can observe a restart. You can
see below kind of messages in console.
2019-09-04 13:38:57.847 INFO 85732 --- [ Thread-6] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.7.RELEASE)
2019-09-04 13:38:57.959 INFO 85732 --- [ restartedMain] c.s.a.D.DevToolDemoApplication : Starting DevToolDemoApplication on C02X902SJGH5 with PID 85732 (/Users/krishna/Downloads/DevToolDemo/target/classes started by krishna in /Users/krishna/Downloads/DevToolDemo)
2019-09-04 13:38:57.959 INFO 85732 --- [ restartedMain] c.s.a.D.DevToolDemoApplication : No active profile set, falling back to default profiles: default
2019-09-04 13:38:58.119 INFO 85732 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9999 (http)
2019-09-04 13:38:58.120 INFO 85732 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-09-04 13:38:58.120 INFO 85732 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.22]
2019-09-04 13:38:58.125 INFO 85732 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-09-04 13:38:58.125 INFO 85732 --- [ restartedMain] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 164 ms
2019-09-04 13:38:58.166 INFO 85732 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-04 13:38:58.199 INFO 85732 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2019-09-04 13:38:58.210 INFO 85732 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9999 (http) with context path ''
2019-09-04 13:38:58.210 INFO 85732 --- [ restartedMain] c.s.a.D.DevToolDemoApplication : Started DevToolDemoApplication in 0.269 seconds (JVM running for 76.506)
2019-09-04 13:38:58.212 INFO 85732 --- [ restartedMain] .ConditionEvaluationDeltaLoggingListener : Condition evaluation unchanged
You can
download complete working application from this link.
No comments:
Post a Comment