Spring Boot Project Initialization Guide
Spring Boot Project Initialization Guide
from Spring to Spring Boot - the Spring Boot conductor (Orange internal)
Spring and its ecosystem
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
Spring and its ecosystem
advantages
Spring brings a whole ecosystem of tools to facilitate application development. It supports (at least in part) certain
the most commonly encountered issues by developers:
The goal is to let Spring handle as much as possible the technical mechanisms of development that are costly to implement.
place (plumbing). Thus, the developer focuses on the business rules of their application.
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
Spring and its ecosystem
disadvantages
A project that is based on the Spring ecosystem requires:
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
Spring and its ecosystem
the solution
Spring Bootis a hat tool. Its purpose is to simplify:
– the configuration: conventions and default settings (Maven dependencies, Spring configuration), which ensure a good
integration of different frameworks
- execution: an application server (Tomcat, JettyorUndertow) is embedded in the application executable lejar/war
the implementation of microservices: support for libraries and a microservices platform
– deployment in a Cloud: help with configuration, tools, Cloud clients. Integration with Cloud Foundry
Spring Boot takes the logic of simplification and integration initiated by Spring to its maximum.
Note: Spring Boot does not provide new solutions to issues already addressed by other frameworks.
Spring.
from Spring to Spring Boot - the Spring Boot conductor (Orange internal)
Spring and its ecosystem
Spring Boot
Spring Boot relies on 3 main components:
from Spring to Spring Boot - the Spring Boot conductor (Orange internal)
initialization of a Spring Boot project
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
initialization of a Spring Boot project
Several methods (of your choice) are available to generate a Spring Boot project skeleton. They all rely on Spring Initializr, via:
aweb interface
the IDESpringToolsorIntelliJ
– Spring Boot CLI (Command Line Interface)
– cURL
Based on the choices expressed by the user (Web, JPA, Security, Cloud...), the project skeleton is created, particularly by adding the
necessary Maven dependencies (the starters).
Note:
The generated project is not a multi-module Maven project, as it is more microservices oriented.
There is no business code, so the project cannot be tested directly, unlike the old Orange Maven.
Archetypes
from Spring to Spring Boot - the Spring Boot conductor (Orange internal)
initialization of a Spring Boot project
Spring Initializr via une interface web
Spring Initializr is a web page that allows you to generate a Spring Boot project in the form of a .zip file. This file can be imported into a
IDE once unzipped.
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
initialization of a Spring Boot project
Spring Initializr via Spring Tools or IntelliJ
SpringTools relies on the IDEEclipse, customized to simplify Spring application development.
from Spring to Spring Boot - the Spring Boot conductor (Orange internal)
initialization of a Spring Boot project
Spring Initializr via Spring Tools or IntelliJ
Whether in SpringTools or IntelliJ, a wizard integrates Spring Initializr: File/New/Spring Starter Project
from Spring to Spring Boot - the Spring Boot conductor (Orange internal)
initialization of a Spring Boot project
Spring Initializr via Spring Tools or IntelliJ
Next, you need to fill in the information (Maven among others) requested:
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
initialization of a Spring Boot project
Spring Initializr via Spring Tools or IntelliJ
Enfin, il faut sélectionner les briques Spring dont on a besoin :
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
initialization of a Spring Boot project
Spring Initializr via Spring Boot CLI
Spring Boot CLI is a tool that allows you to run tasks from the command line (no need to open an IDE), for example:
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
initialization of a Spring Boot project
Spring Initializr via cURL
cURL is an open-source library and a command-line tool for transferring data using various protocols (HTTP,
HTTPS, FTP, SMTP, LDAP...
The commands are in the form: curl [URL] [options] or curl [options] [URL]
It can be used to access the creation of a Spring Initializr project, for example:
curl [Link]
-d applicationName=MyProjectApplication
-d dependencies=web,data-jpa
-o [Link]
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
generated Spring Boot project
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
generated Spring Boot project
structure of a Spring Boot project
The project generated by Spring Initializr is a Maven (or Gradle) project containing at least:
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
Spring Boot project generated
Maven configuration without corporate pom
[Link] of the application inherits from a parent (spring-boot-starter-parent) provided by Spring Boot:
<parent>
<groupId>[Link]</groupId>
spring-boot-starter-parent
<version>[Link]</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
Reminder: the <dependencyManagement> section in [Link] declares dependencies but does not import them. It needs to specify.
those that we really want to use in the <dependencies> section.
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
generated Spring Boot project
Maven configuration with corporate pom
If we are to inherit from a corporate parent, it is no longer possible to inherit from the spring-boot-starter-parent (only one parent is possible).
<dependencyManagement>
<dependencies>
<dependency>
Import dependency management from Spring Boot
[Link]
spring-boot-dependencies
<version>[Link]</version>
pom
import
</dependency>
</dependencies>
</dependencyManagement>
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
generated Spring Boot project
Maven configuration: the dependencies
Then we find starters offered by Spring as dependencies, based on the choices made in Spring Initializr:
Note: it is not necessary to specify the dustarter version, it is configured in the spring-boot dependency.
dependencies.
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
generated Spring Boot project
Maven configuration: property overriding
By default, many properties, including library versions, are set by Spring Boot.
It is possible to change them in the <properties> section of [Link] of the application, by overriding those declared in the [Link].
spring-boot-dependencies:
<properties>
...
<[Link]>11</[Link]>
<[Link]>1.7.29</[Link]>
...
</properties>
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
generated Spring Boot project
Maven configuration: executable jar/war
By default, an executable jar (fat jar) is generated, but it is also possible to have an executable war (fat war):
<packaging>jar</packaging>or war
...
<build>
<plugins>
<plugin>
[Link]
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
generated Spring Boot project
Java code
The main class of the application (annotated with @SpringBootApplication) has been generated in the base package.
This annotation will among other things scan the current package and sub-packages to detect Spring beans (annotated @Named,
Component, Controller, Service...
This main class contains the main() method which is used to run the application.
[Link];
import [Link];
import [Link];
@SpringBootApplication
public class ToDoListDemoApplication {
from Spring to Spring Boot - the Spring Boot conductor (Orange internal)
tests
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
tests
Maven configuration
With Spring Initializr, thespring-boot-starter-testis automatically added in [Link]:
dependency
<groupId>[Link]</groupId>
spring-boot-starter-test
test
<exclusions>
<exclusion>
<groupId>[Link]</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
tests
unit tests
We can create unit test classes using JUnit:
@SpringBootTest(webEnvironment=[Link].RANDOM_PORT,
properties="classpath:[Link]")
public class ToDoList_API_Test {
...
@LocalServerPort
private int port;
BeforeEach
public void setUp() throws Exception {...}
@Test
public voidfindAllToDoLists_OK()throwsException {...}
...
}
from Spring to Spring Boot - the Spring Boot conductor (Orange internal)
static pages
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
static pages
To add static files (HTML pages...) to a Spring Boot application, you need to place them in the directory
src/main/resources/static(ousrc/main/resources/public).
We can override the default error page of Spring Boot with customized pages. They must:
For more information on page error overload, visitthe Spring Boot documentation.
from Spring to Spring Boot - the Spring Boot conductor (Orange internal)
properties/.yml files and profiles
from Spring to Spring Boot - the Spring Boot conductor (Orange internal)
properties/.yml files and profiles
The configuration of an application and the embedded frameworks by Spring Boot can be done using .properties and/or .yml files.
The [Link] looks a lot like .json. It's useful for defining hierarchical configuration data (but be careful of
respect for indentation !).
The main configuration file [Link] (or [Link]) can, for example, define:
from Spring to Spring Boot - the Spring Boot conductor (Orange internal)
properties/.yml files and profiles
This configuration file can be divided into several files according to the nature of the information contained, and to prevent it from being too
bulky.
to provide different configurations depending on the target environments (development, testing, integration, production...)
–which will overload those defined in the default file.
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
properties/.yml files and profiles
To create profiles, you need to create a file application-<profileName>.properties/.yml for each profile:
- [Link] or [Link]
[Link] or [Link]
#[Link]
[Link]
#[Link]
[Link]
#[Link]
server
port:4431
#[Link]
server
port:4432
from Spring to Spring Boot - the Spring Boot conductor (Orange internal)
properties/.yml files and profiles
In yaml format, we can also declare everything in the main [Link] file, separating the profiles with ---:
spring
application:
name: demoservice
server:
port:8080
---
spring:
profiles: dev
server
port:4431
---
spring:
profiles: prod
server
port:4432
from Spring to Spring Boot - the Spring Boot conductor (Orange internal)
properties/.yml files and profiles
profile selection
To run an application with a specific profile, we have 2 solutions:
#[Link]
[Link]=prod
When a profile is launched, the corresponding file is read after the main configuration file [Link]/.yml, which allows
to overload one's properties.
Note: If you deploy an application in Cloud Foundry, the [Link]/.yml file will be loaded by
default. It is therefore not necessary to specify the active profile.
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
properties/.yml files and profiles
best practices
Voici l'ordre de priorité des [Link]/.yml:
Advice:
collect the common (or default) properties for all environments in one or more files
.properties/.yml, to be deployed in the jar/war (messages...)
externalize the environment-dependent configuration into one or more profile files deployed in
outside of war.
from Spring to Spring Boot - the Spring Boot conductor (Orange internal)
log management
from Spring to Spring Boot - the Spring Boot conductor (Orange internal)
log management
configuration in the .properties/.yml file
Spring Boot has a default configuration for Logback (rolling policy, pattern...) that can be modified (partially) directly in
the file [Link]/yml.
#properties
[Link]= WARN
[Link]= DEBUG
[Link]= /tmp/[Link]
#yml
logging:
level:
root: WARN
[Link]: DEBUG
file: /tmp/[Link]
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
log management
configuration in the .xml file
It is of course also possible to modify the default configuration of Logback by using its own .xml file:
[Link]= /tmp
[Link]= classpath:[Link]
<?xml version="1.0"encoding="UTF-8"?>
<configuration>
<includeresource=org/springframework/boot/logging/logback/[Link]
<loggername=[Link] level="DEBUG"/>
<loggername=[Link] level="INFO"/>
<loggername=[Link] level="INFO"/>
</configuration>
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
log management
externalization and centralization of logs
In the case of an application or microservices deployed on PaaS, it is necessary to be able to externalize the logs, in order to:
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
log management
externalisation et centralisation des logs
This centralized log management system is directly available in the Cloud Foundry and Kermit PaaS.
– Elasticsearch:
NoSQL database
massive data storage, especially logs
search engine, indexing and data analysis
– Logstash:
pipeline accepting input data from different sources
makes transformations, aggregations... on this data
export the data to other tools, including Elasticsearch for storage
– Kibana:
web interface allowing exploration of data stored in Elasticsearch
very varied graphic renditions (scatter plots, curves, graphs...)
Cloud Foundry uses a serviceLogsearchopen source above ELK, allowing to manage an ELK environment in a context
cloud.
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
log management
externalization and centralization of logs
In addition to these platforms on the application side:
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
log management
outsourcing and centralization of logs
The integration with Spring Boot is simple and transparent at the code level.
<properties>
<[Link]>6.4</[Link]>
</properties>
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
[Link]
logstash-logback-encoder
<version>${[Link]}</version>
</dependency>
</dependencies>
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
log management
externalization and centralization of logs
It is also necessary to add the following dependencyManagement (the same as for OpenFeign):
<dependencyManagement>
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${[Link]}</version>
pom
import
</dependency>
</dependencies>
</dependencyManagement>
By default, Sleuth only sends 10% of the logs to the remote platform. It is possible to change this percentage in the file.
[Link]:
between0and1
[Link] =0.5
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
log management
externalization and centralization of logs
Finally, you need to use the class LogstashEncoder to transform the logs into JSON, using the configuration file [Link]:
<appendername=JSON
<encoderclass=[Link]>
</appender>
<rootlevel=INFO
<appender-ref ref=JSON
</root>
</configuration>
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
monitoring
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
monitoring
initialization with Spring Initializr
With Spring Initializr, you need to select the Actuator option in the Ops section.
The other options are available for previous versions of Spring Boot. Notably Actuator Docs which added the
Actuator documentation directly in the application in the form of a predefined endpoint (URL).
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
monitoring
Maven configuration
Whether it's with Spring Initializr or manually, you need to have the dependency for spring-boot-starter-actuator:
<dependency>
<groupId>[Link]</groupId>
spring-boot-starter-actuator
</dependency>
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
monitoring
features provided
Actuator addendpoints (URLs)to your application, in order to interact with it and supervise it:
By default, all these endpoints are allowed (except/shutdown) but not necessarily exposed (thus not accessible). We can change their
individual authorization in the [Link]/yml file:
[Link]=true
[Link]=false
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
monitoring
features provided
These endpoints can be exposed (whether authorized or not) via JMX and/or HTTP. It is possible to modify their exposure on one
or the other technology:
By default, on HTTP:
Attention: do not expose and allow certain endpoints without securing them, as they may provide sensitive information.
on the application. If Spring Security is present, they are secured by default (except /health/info) and require a
authentication. It is possible to modify the access rights of each endpoint based on roles.
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
monitoring
features provided
It is possible to modify certain default parameters (ports, endpoint URLs...) in the [Link]/yml file:
Finally, it is possible to create your own endpoints (exposed via JMX or HTTP or both).
from Spring to Spring Boot - the Spring Boot conductor (Orange internal)
monitoring
added features
To display information for the endpoint/info, simply add your own keys (free) by prefixing them with info. in the file
[Link]/yml:
Other information about the application, taken from the file [Link] (name, groupId, artifactId, build date...), can be added directly.
thanks to the configuration of the pluginspring-boot-maven-plugin:
<plugin>
<groupId>[Link]</groupId>
spring-boot-maven-plugin
<executions>
<execution>
<goals>
Generates build information (used by actuator info)
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
deployment with Spring Boot
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
deployment with Spring Boot
fat jar or fat war?
As we have already mentioned, Spring Boot offers the possibility to create a fat jar or a fat war, including an embedded Tomcat.
-a PaaS: the use of the jar file is recommended, as we are sure that the Tomcat/jar pair is identical from one end of the chain to the other.
production. Supervision is the responsibility of the PaaS.
- of a Plato platform: it is better to rely on a fat deployed in a Plato-ized Tomcat.
Note that an unfat war can be both self-executable and deployed in a classic Tomcat. The embedded Tomcat is then ignored.
–development: on the developer's position, the use of embedded Tomcat is very practical
–from continuous integration: deploy this same fat war but in an external Tomcat as close as possible to the production one.
This is to avoid platform differences between testing and production.
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
deployment with Spring Boot
deploy the jar locally via command line
First, you need to build the application with Maven to obtain the executable jar (with an embedded Tomcat).
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
deployment with Spring Boot
deploy the jar locally via command line
After a few seconds, the application is deployed and available at the address: [Link]
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
deployment with Spring Boot
deploy the jar locally from an IDE
In SpringTools: right-click on the project Run As Spring Boot App
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
deployment with Spring Boot
deploy the jar locally from an IDE
In SpringTools (and IntelliJ?), there is a viewSpring Boot Dashboard.
It allows to:
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
deployment with Spring Boot
deploy the war on an external server
To generate and deploy a WAR in an external container (e.g., Tomcat), it is preferable to create a specific Maven profile in the
[Link]. By default, the jar is produced, and the war only when the profile is activated:
<profiles>
<profile>
<id>deploy-war</id>
<properties>
war
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>[Link]</groupId>
spring-boot-starter-tomcat
provided
</dependency>
</dependencies>
</dependencyManagement>
...
</profile>
</profiles>
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
deployment with Spring Boot
deploy the war on an external server
In addition, the main class of the application must extend the SpringBootServletInitializer class and override the method
configure():
@SpringBootApplication
public class ToDoListDemoApplication extends SpringBootServletInitializer {
@Override
protectedSpringApplicationBuilderconfigure(SpringApplicationBuilder builder) {
[Link]([Link]);
}
}
This replaces the traditional configuration file [Link], and possibly adds configuration (default profile,
launch arguments of the application...
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
deployment with Spring Boot
deploy the war on an external server
TheMaven Cargo pluginallows you to simplify the deployment of the war on the platform of your choice (locally or remotely):
<plugin>
[Link]
cargo-maven2-plugin
<version>${[Link]}</version>
Server information: url, port, user, pwd...
...
</plugin>
This plugin would be useful in the deploy-war profile presented earlier for example.
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
documentation
Official siteSpring Boot
Spring BootReference Guide
Documentation on theMaven configuration of Spring Boot
Documentation onSpring Actuator
SiteSpring Initializr
-SiteSpringTools
Articles onSpring Boot Dashboard
GitHubSpring Initializr CLI
SiteElastic
–SiteLogsearch
SiteSpring Cloud Sleuth
-SiteSpring Framework GURUmany tutorials
–SiteBaeldungmany tutorials
–TutorialSpringa lot of information on the installation and use of tools related to Spring (Spring Core, Spring MVC, Spring
Security, SpringTools, Spring Boot, Spring Boot CLI, cURL...)
From Spring to Spring Boot - the Spring Boot conductor (Orange internal)
thank you