JavaProgrammingForBeginners-Presentation-46-60
JavaProgrammingForBeginners-Presentation-46-60
46
More Spring Boot Features
Spring Boot Actuator: Monitor and manage
your application in your production
Provides a number of endpoints:
beans - Complete list of Spring beans in your app
health - Application health information
metrics - Application metrics
mappings - Details around Request Mappings
47
Spring Boot vs Spring MVC vs Spring
Spring Framework Core Feature: Dependency Injection
@Component, @Autowired, IOC Container, ApplicationContext, Component Scan etc..
Spring Modules and Spring Projects: Good Integration with Other Frameworks
(Hibernate/JPA, JUnit & Mockito for Unit Testing)
Spring MVC (Spring Module): Build web applications in a decoupled approach
Dispatcher Servlet, ModelAndView and View Resolver etc
Spring Boot (Spring Project): Build production ready applications quickly
Starter Projects - Make it easy to build variety of applications
Auto configuration - Eliminate configuration to setup Spring, Spring MVC and other projects!
Enable production ready non functional features:
Actuator : Enables Advanced Monitoring and Tracing of applications.
Embedded Servers - No need for separate application servers!
Default Error Handling
48
Spring Boot - Review
Goal: 10,000 Feet overview of Spring Boot
Help you understand the terminology!
Starter Projects
Auto Configuration
Actuator
DevTools
49
JUnit
In 5 Steps
50
Introduction to Unit Testing with JUnit
Large applications can have 1000s of code files and
millions of lines of code
Testing: Check app behavior against expected behavior
Option 1: Deploy the complete application and test
This is called System Testing or Integration Testing
Option 2: Test specific units of application code independently
Examples: A specific method or group of methods
This is called Unit Testing
Advantages of Unit Testing
Finds bug early (run under CI)
Easy to fix bugs
Reduces costs in the long run
Most Popular Java Frameworks: JUnit and Mockito
Recommended: Option 1 + Option 2
51
JPA and Hibernate
in 10 Steps
52
Getting Started with JPA and Hibernate
Build a Simple JPA App using
Modern Spring Boot Approach
Get Hands-on with JPA, Hibernate
and Spring Boot
World before JPA - JDBC, Spring JDBC
Why JPA? Why Hibernate? (JPA vs
Hibernate)
Why Spring Boot and Spring Boot Data
JPA?
JPA Terminology: Entity and Mapping
53
Learning JPA and Hibernate - Approach
01: Create a Spring Boot Project
with H2
02: Create COURSE table
03: Use Spring JDBC to play with
COURSE table
04: Use JPA and Hibernate to play
with COURSE table
05: Use Spring Data JPA to play
with COURSE table
54
Spring Boot Auto Configuration Magic
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,
schema.sql)
55
JDBC to Spring JDBC to JPA to Spring Data JPA
JDBC
Write a lot of SQL queries! (delete from todo where id=?)
And write a lot of Java code
Spring JDBC
Write a lot of SQL queries (delete from todo where id=?)
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!
56
JDBC to Spring JDBC
JDBC example
public void deleteTodo(int id) {
PreparedStatement st = null;
try {
st = db.conn.prepareStatement("delete from todo where id=?");
st.setInt(1, id);
st.execute();
} catch (SQLException e) {
logger.fatal("Query Failed : ", e);
} finally {
if (st != null) {
try {st.close();}
catch (SQLException e) {}
}
}
}
57
JPA Example
@Repository
public class PersonJpaRepository {
@PersistenceContext
EntityManager entityManager;
58
Hibernate vs JPA
JPA defines the specification. It is an API.
How do you define entities?
How do you map attributes?
Who manages the entities?
Hibernate is one of the popular
implementations of JPA
Using Hibernate directly would result in a
lock in to Hibernate
There are other JPA implementations (Toplink, for
example)
59
Maven
60