Spring 5 training (Spring guru) - personal notes
I recently (2018) subscribe to an online training related to Spring 5 on Udemy. This page is a sum-up of my personal notes related to Spring 5.
Contents
Links
- Spring initializr - to generate a quick start project based on the libraries you choose
Very simple web-application
- Go to Spring initializr
- Select
- web > web
- SQL > JPA
- SQL > H2
- Templates > Thymeleaf
- Ops > actuator
H2 hint
When using H2 on a web application, don't forget to enable the console! Edit application.properties
as follow:
spring.h2.console.enabled=true
# replace "spring5webapp" by your own application
spring.datasource.url=jdbc:h2:mem:spring5webapp;DB_CLOSE_ON_EXIT=FALSE
Then you can reach the console at http://localhost:8080/h2-console
Hibernate
General notes
Hibernate recommends to implement hashCode()
and equals()
on all JPA entities. They also recommend to use a particular Business ID when possible, if not then use the ID. Only the key fields should be in these methods.
IntelliJ configuration
Plugins
Useful plugings:
- File > Settings > Plugins > Browse repositories
- .ignore
- Ideolog
- Sonarlint
- Save actions
- GenerateSerialVersion
Enable toolbars! View > Toolbar
Import optimization
IntelliJ IDEA offers 2 options:
- Settings | Editor | General | Auto Import > Optimize imports on the fly
- Commit Project dialog > Optimize imports option
Outline like Eclipse (structure view)
see https://www.jetbrains.com/help/idea/viewing-structure-of-a-source-file.html
To view the file structure, do one of the following
- On the main menu, choose View | Tool Windows | Structure.
- Pres StructureTool Button
- Press Alt+7.
- Press Ctrl+F12.
Create new Spring project
On the main menu, choose File | New | Project
- Left side: Spring initalizr (IntelliJ ultimate)
Spring 5
How to run a spring project (command line)
Spring boot provides some out-of-the-box tools.
To run the application:
# Using the maven installation (outside the project)
mvn spring-boot:run
# Using the embedded tool
./mvnw spring-boot:run
Spring MVC
Implementation of MVC concept in Spring:
Here is a short description of the schema - as I understood it + based on my experience with Spring so far (v2 to v4):
- Client submits a request to the server (HTTP request)
- Dispatcher receives the request
- Dispatcher asks the Handler WHO should take care of the request? i.e:
- What is the Controller for the requested endpoint?
- Is there any java Method that match the given HTTP request type (GET, POST, PUT, ..) + arguments?
- Dispatcher invokes the Controller and forwards query
- Controller calls a Service to process the query, interact with the database and other systems.
- Once the Service processing is complete, it returns corresponding data to the Controller
- The Controller will convert the data into a DTO (communication Model (pojo)
- Controller sends the pojo back to the Dispatcher
- Data disposal: The Dispatcher disposes of the data. i.e:
- Send back the data directly to the client [HTTP REST calls]
- Forward the data to a View for rending
- View generates the page thanks to UI technology (server side) such as JSP, JSF or Themeleaf to generate HTML / Javascript / CSS
- View sends back generated content to Dispatcher
- Dispatcher forwards content to the Client
Spring beans lifecycle
This section describes Spring's beans lifecycle. It is an extract of Spring 5 training from John Tompson
Creation
This is how beans are created with Spring:
- instanciation of the Class. Spring will inject dependencies through the constructor, if available.
- populate properties. Spring will inject the @Value properties - and it will actually perform placeholder resolution against environment, properties files, profiles settings, etc.
- BeanPostProcessing : set of operations that are performed once the bean has been initalized (the Object composition is complete from Spring point of view)
- Pre-initialization phase: to customize the bean (and its properties) for some advanced tricks. This is usually not used at all.
- After properties. To get notify once the properties are set you have to implements
Initializing.afterPropertiesSet()
- You can implement some custom loader. This is usually not used.
- Post-initialization phase: to customize the bean once properties are set and custom loader is complete. This is usually not used at all.
- Bean ready to use
There is one extra step that is not on the schema:
- Last is the Post processing: spring will run the
@PostConstruct
method.
Summary:
Out of the lifecyle, only the following steps are really important:
- instanciation through the Spring bean's constructor
- properties by setting the @Value attributes
- bean ready to use
- Post processing by executing the
@PostConstruct
Destroy
This is how beans are destroyed with Spring:
- Container (Tomcat, Jetty, Bash, etc.) will send the shutdown signal to the application
- Disposable phase: Spring will call the @PreDestroy method
- You can also specify a custom destroy method that will be call at the end by implementing
DisposableBean.destroy()
. This is useful for custom socket connections close (example). Ideally, on new applications you should NOT use that custom part at all.