How does Spring Boot load changes in code without restarting the server - spring

This was an interview question to me that , is it possible that some changes you made in your code and it is an spring boot application, and without restarting the server you are able to get those changes.?
if yes, then how is it possible in spring boot.
I want to know that how is it possible in Spring Boot.?

Add spring-boot-devtools module to your project, which includes LiveReload server which can be used to trigger a browser refresh whenever a resource has been changed.You can download browser extensions from livereload.com.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>

You only need to add devtool dependency in pom.xml and yml file property:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
# for not restarting the server every time
spring.devtools.restart.enabled: false

you may want to check here spring dev tool
Notice:
no matter what, when you have change to your java code, the server need to be restarted, spring dev tool just help you to reload it
if it is jsp then you do not have to restart server.

I'm using intellij, the follow dependency setting works for me.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>

Try the following steps and it should work
Add the following to pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
In application.properties
spring.devtools.restart.enabled=true
Note : Ensure spring boot is restarted once after making changes to the application.properties file.
For additional information please check
https://docs.spring.io/spring-boot/docs/1.5.16.RELEASE/reference/html/using-boot-devtools.html

Related

spring-boot - Changes to make it available to all spring-cloud-config-client

We have large number of microservices which are spring cloud config client.
Each of them have bootstrap.properties which contains configuration for config server, such as URI, Username, Password for config server.
We want to overcome a problem where config client starts without actually getting the specific property resource file from config server ( some of the cases are config server was not available when client was getting started.)
So to overcome this I am thinking to use this configuration spring.cloud.config.failFast=true or to enable retry
I can do that in the bootstrap.properties for specific cloud config client or microservice.
But We have so many of them and editing the bootstrap.properties seems too much overhead
Does anyone has any other solution to the problem?
Or to apply above solution in any other place.
I am looking for a centralized solution.
Version details are as below.
<spring.version>2.4.4</spring.version>
<spring.cloud.version>2020.0.2</spring.cloud.version>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.4.4</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.3.1</version>
</dependency>

change default web server on spring-boot 2.4.4

I'm working with Spring Boot 2.4.4 and I would change the default web server Tomcat to undertow orJHetty but I find it very difficult using both Gradle or Maven.
An old documentation exposes how do it but I'm sure all is changed because now tomcat, undertow and jetty configuration is embedded in the core library:
https://docs.spring.io/spring-boot/docs/2.1.9.RELEASE/reference/html/howto-embedded-web-servers.html
How do it in the 2.4.4 version?
There are no changes between the versions. This is well described right at the Spring Boot 2.4.4 Reference guide, right in 3.1. Use Another Web Server section. Basically, the change consists of two steps:
Exclude embedded Tomcat server dependency from the spring-boot-starter-web artifact:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- Exclude the Tomcat dependency -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
Include your embedded server as a separate dependency instead:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
Just don't forget to notice the following quote from the reference guide in the very same section which might or might not be relevant to you:
The version of the Servlet API has been overridden as, unlike Tomcat 9 and Undertow 2.0, Jetty 9.4 does not support Servlet 4.0.
follow three steps to change the default web server, change configuration in pom.xml.
1.exclude the default web server.
2.Include the necessary web server.
3.Maven update.
For example,
Instead of this
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
Add this one
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
for the necessary server add the appropriate web server dependency.

Automatically Deploy Spring Boot Application file changes without manual restart

Automaticall deploy my spring boot application file changes without manually restarting the server.
Use the following dependency:
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>1.3.0.RELEASE</version>
</dependency>

ValidationException with Spring boot and Jetty

I set up a spring boot application(1.4.0.RELEASE) with the following configuration
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
As expected, this ran with embedded Tomcat. I then thought of trying the same with a Jetty server and followed the steps mentioned in the documentation here:
Using Jetty instead of Tomcat
Basically excluding Tomcat and adding dependency for Jetty.Running mvn clean install from the command line or running the main method resulted in the following exception:
Caused by: javax.validation.ValidationException: HV000183: Unable to
load 'javax.el.ExpressionFactory'. Check that you have the EL
dependencies on the classpath, or use ParameterMessageInterpolator
instead
I could solve this by adding the following dependency in the pom.xml:
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
I am not directly using any validation related code but I suspect this is getting pulled from the spring boot jpa starter which pulls in Hibernate. I have also seen a discussion around this here: Similar issue
Questions:
1) Is this the right fix?
2) If it is the right fix, should the documentation be updated to add this dependency as well?
You are correct in using the javax.el dependency. When the JPA pulls in Hibernate as you stated, it will use the Hibernate Validator. It's specified here. This is the right fix. As for the documentation, I personally would raise it but I suspect not everyone will have the same issue. I still can run my mvn clean install without errors however if I run mvn spring-boot:run it starts up and shuts-down straight after.

Log4J2 in Spring Boot with embedded Tomcat

I am trying to let the embedded Tomcat in Spring Boot log to my Log4J2 configuration, but it doesn't work.
According to this answer that copes with an external Tomcat: https://stackoverflow.com/a/28639068/1845463 there seems to be the need to move some jars to $Catalina_home/libs etc. I think this is not possible with Spring Boot, isn't it?
Has someone managed to get log4j2 running and be able to configure appenders for catalina log?
Thanks in advance
EDITED: The simplest way to do is to add spring-boot-starter-log4j2.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
N.B. Make sure that the other components don't need different version of log4j. This may cause run-time errors. e.g. elasticsearch java API requires 2.6+ and spring-boot-starter-log4j2:1.3.8 provides log4j:2.4.1, if we're building an app that connects elasticsearch and uses spring boot too. Then we will end up getting NoSuchMethodError or similar errors. For resolution of those errors we should add log4j2:2.6+ in our pom.
Which Spring Boot version are you using? I believe 1.4.x.RELEASE uplifted it to log4j2.
As #M.Deinum mentioned including:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
brings in:
log4j-core, log4j-api and a few more. See https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-log4j2/1.4.6.RELEASE
It might be advisable to exclude logging starter using:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

Resources