JavaProgrammingForBeginners-Presentation-61-75
JavaProgrammingForBeginners-Presentation-61-75
61
Exploring Project Object Model - pom.xml
Let's explore Project Object Model - pom.xml
1: Maven dependencies: Frameworks & libraries used in a project
Ex: spring-boot-starter-web and spring-boot-starter-test
Why are there so many dependencies in the classpath?
Answer: Transitive Dependencies
(REMEMBER) Spring dependencies are DIFFERENT
62
Exploring Maven Build Life Cycle
When we run a maven command, maven build life
cycle is used
Build LifeCycle is a sequence of steps
Validate
Compile
Test
Package
Integration Test
Verify
Install
Deploy
63
How does Maven Work?
Maven follows Convention over Configuration
Pre defined folder structure
Almost all Java projects follow Maven structure (Consistency)
Maven central repository contains jars (and others) indexed
by artifact id and group id
Stores all the versions of dependencies
repositories > repository
pluginRepositories > pluginRepository
When a dependency is added to pom.xml, Maven tries to
download the dependency
Downloaded dependencies are stored inside your maven local repository
Local Repository : a temp folder on your machine where maven stores the
jar and dependency files that are downloaded from Maven Repository.
64
Important Maven Commands
mvn --version
mvn compile: Compile source files
mvn test-compile: Compile test files
OBSERVCE CAREFULLY: This will also compile source files
mvn clean: Delete target directory
mvn test: Run unit tests
mvn package: Create a jar
mvn help:effective-pom
mvn dependency:tree
65
Spring Boot Maven Plugin
Spring Boot Maven Plugin: Provides Spring Boot
support in Apache Maven
Example: Create executable jar package
Example: Run Spring Boot application
Example: Create a Container Image
Commands:
mvn spring-boot:repackage (create jar or war)
Run package using java -jar
mvn spring-boot:run (Run application)
mvn spring-boot:start (Non-blocking. Use it to run integration tests.)
mvn spring-boot:stop (Stop application started with start command)
mvn spring-boot:build-image (Build a container image)
66
How are Spring Releases Versioned?
Version scheme - MAJOR.MINOR.PATCH[-MODIFIER]
MAJOR: Significant amount of work to upgrade (10.0.0 to 11.0.0)
MINOR: Little to no work to upgrade (10.1.0 to 10.2.0)
PATCH: No work to upgrade (10.5.4 to 10.5.5)
MODIFIER: Optional modifier
Milestones - M1, M2, .. (10.3.0-M1,10.3.0-M2)
Release candidates - RC1, RC2, .. (10.3.0-RC1, 10.3.0-RC2)
Snapshots - SNAPSHOT
Release - Modifier will be ABSENT (10.0.0, 10.1.0)
67
REST API
REST API: Architectural Style for the Web
Resource: Any information (Example: Courses)
URI: How do you identify a resource? (/courses, /courses/1)
You can perform actions on a resource (Create/Get/Delete/Update). Different HTTP Request
Methods are used for different operations:
GET - Retrieve information (/courses, /courses/1)
POST - Create a new resource (/courses)
PUT - Update/Replace a resource (/courses/1)
PATCH - Update a part of the resource (/courses/1)
DELETE - Delete a resource (/courses/1)
Representation: How is the resource represented? (XML/JSON/Text/Video etc..)
Server: Provides the service (or API)
Consumer: Uses the service (Browser or a Front End Application)
68
Spring and Spring Boot Release Cycles
What is the difference between these?
2.5.0 (SNAPSHOT)
2.4.5 (M3)
2.4.4
Release Number: MAJOR.MINOR.FIX
Spring and Spring Boot Release Cycle:
SNAPSHOT (versions under development) > Mile Stones > Released Version
Recommendation - Do NOT use SNAPSHOTs or M1 or M2 or M3
Prefer released versions!
69
JDBC to Spring JDBC to JPA to Spring Data JPA
JDBC
Write a lot of SQL queries!
And write a lot of Java code
Spring JDBC
Write a lot of SQL queries
BUT lesser Java code
JPA
Do NOT worry about queries
Just Map Entities to Tables!
Spring Data JPA
Let's make JPA even more simple!
I will take care of everything!
70
JDBC to Spring JDBC
JDBC example
public void deleteTodo(int id) {
PreparedStatement st = null;
try {
st = db.conn.prepareStatement(DELETE_TODO_QUERY);
st.setInt(1, id);
st.execute();
} catch (SQLException e) {
logger.fatal("Query Failed : " + DELETE_TODO_QUERY, e);
} finally {
if (st != null) {
try {st.close();}
catch (SQLException e) {}
}
}
}
71
JPA Example
@Repository
@Transactional
public class PersonJpaRepository {
@PersistenceContext
EntityManager entityManager;
72
Spring Boot Auto Configuration Magic - Data JPA
We added Data JPA and H2 dependencies:
Spring Boot Auto Configuration does some magic:
Initialize JPA and Spring Data JPA frameworks
Launch an in memory database (H2)
Setup connection from App to in-memory database
Launch a few scripts at startup (example: data.sql)
73
Congratulations
Java keeps improving:
Java 10, Java 11, Java 12, ...
Java Project - REST API in Modern Approach:
Spring
Spring Boot
Do NOT forget to leave a Review!
74
What's Next? - Don't Stop Learning!
https://github.com/in28minutes/learn
75