Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Message

To be clear. The content here is a content based on a free course. Noone can reproduce this material. If you wanna to know this material, my advice is to you access the free course. And if they end the course or start to ask money to study in the course? Well, this considerations are out of my control, but the DIO material is excelent and the course is amazing! If you acquire a DIO paln, you will have access to amazing courses.

General

Spring framework is an Java open source framework based on the concepts of inversion of control and dependency inversion.

Your structure is based on modules.

Overview of modules:

modules

Java EE mainly in version preview of 5 was very verbose and burocratic and Spring Framework born as a more simple solution.

Some concepts of Spring Framework

IOC

Overview:

ioc - IOC

Traditional way to work without IOC:

without IOC

With IOC:

with IOC

Dependency injection

Overview:

overview dependency injection

Beans

beans

Scopes

scopes

More details:

  • Singleton: the IOC container creates only one instance of the object;
  • Prototype: it will be created a new object on each request to the container;
  • HTTP - request: a bean will be created to each HTTP request;
  • HTTP - session: a bean will be created to each user session;
  • HTTP - global: application scope creates a bean to the context lifecycle of the application. In other words, while the application works, this bean will be avaliable.

Autowired

Overview:

autowired

Spring boot

Overview:

Spring Boot - overview

Before Sprint Boot:

before Spring Boot

The role of Spring Boot is to aid in the configuration step. Dependency injection and inversion of control are concepts more related to Spring Framework, not to Spring Boot. The general idea is let you to put focus on the things related to the business.

Starters:

starters

Starters are dependency descriptors. In the previous image, in the left part, please see the comments in green. To me (my comment and not a comment of the teacher, the left part of the image seemed a part of a pom.xml of Maven). In the right part is showed a more easy way to do the same thing using a starter. As you can see in the right part of the image, the Spring Boot is used in the right part of the image.

Benefits:

  • Cohesion;
  • Compatibles versions;
  • Optimization of time;
  • Simple configuration;
  • Focus on business.

Some starters:

some starters

More starters:

more starters

Creating a simple Spring Boot project

Basic steps:

creating a simple Spring Boot project

  • "initializr" is also known as Spring Initializr.

Site: https://start.spring.io/

We generated this starter there:

initializer generated

I had to generated a little different starter because some version change between my generation and the generation of the teacher while he was recording the class:

my first steps initializer

I versioned the generated starter with the name generated-initializer.zip in the root directory. I extracted this zip file with the name project-generated-with-initializer, also in the root directory.

To run this project, see the file and the run button that I pressed in VSCode in the next image:

running

We made a simple Calculadora class that we stored in the root directory of the package who has a simple "somar" method that add two integers. This is the wrong strategy to use this class, insatantiating manually the class. The class is not avaliable in the "Spring world" using dependency injection ans inversion of control. See:

wrong startegy

Using Spring

Please see this video.

Singleton x prototype

Class

As I need to be more quick in my studies, I recorded this video trying to save some time.

application.properties

Intruductory slide.

The teacher talked about application.properties when he talked about providing some values to the application.

He talked also that this is used when we will not have change in the values.

My video talking about this class: video.

Configuration properties

In this class the teacher also talked about the use of application.properties, but through a ** bean** in this case. See my understainding of this class.

JPA - Java Persistence API

Some keywords:

some JPA keywords

Definition of JPA:

JPA definition

JPA - mapping:

JPA - mapping

I made a video about this class.

Spring data JPA

We will talk about interfaces and methods.

Here teacher used Spring Initializr to generate the project with the dependencies to use Spring Data JPA, see:

using Initializr for JPA

Pay attention that he both added the dependency of JPA as the dependency related to the RDBMS, in this case H2, but he could added the depenedency for MySQL or PostegreSQL if he wanted.

I recorded [this video](/home/andre/Área de Trabalho/spring-data-jpa.mp4) about this class.

Using PostgreSQL

Teacher configured some details of the connection with the PostgreSQL database in application.properties:

configuring data of a specific PostgreSQL connection

In the previous image you can see how JPA made the "magic" of updating the relational side with updating the Spring side.

The configurations that teacher get from the previous slide he pasted in application.properties:

database configuration in application.properties

Teacher also inserted the PostegreSQL dependency in pom.xml, but he can added this dependency when he generated the basic project in Initializr:

PostegreSQL dependency in applicaion.properties

I made this video about this class.

JPA repository

Teacher teached us that Repository is similar to DAO (Data Access Object), see:

Repository similar to DAO

When we use the connection with a table in a "JPA world", we use AOP (aspect oriented programming, please see the result in Google, as example this link). We will use annotations. and when implement some interfaces we will can use some methods, like:

  • save();
  • findById();
  • count();
  • delete();
  • and others.

Searching in the JPA world:

searching in JPA world

JPQL is the language used in the "JPA world".

An example of JPQL. As you can see in the previous image, the name findByEmailAddressAndLastname follows a standard. as you can see, you have "emailAddress" and "lastname" as the parameters of this function. The parts:

  • find: the operation;
  • By: indicates that the fields will be passed;
  • EmailAddress: a field;
  • And: connector;
  • Lastname: another field.

Some JPA naming patterns:

some JPA naming patterns

Teacher also talked about query method x query override. In the first case, the defition of the way to opperate of the method is defined by its name, but in the second case we override the default functionality inseting a @query annotation and the query that we want. Please see the next image about finding user data based on its name using the SQL like operator:

query method x query method override

You can see that in the query override, in the interface of the image we inserted what to do with the @Query annotation the @Param annotation of the param. JPA will map this value to the %:name in the query of the previous line.

I make this simple video about the class.

Slides of this class.

Github code related to this class.

JPA - summary

JPA - summary

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages