0% found this document useful (0 votes)
3 views

JavaProgrammingForBeginners-Presentation-46-60

The document outlines the deployment process for Spring Boot applications, highlighting the advantages of using embedded servers over traditional WAR deployment. It discusses Spring Boot features such as Actuator for monitoring, DevTools for productivity, and the differences between Spring Boot, Spring MVC, and the core Spring Framework. Additionally, it covers unit testing with JUnit, the use of JPA and Hibernate, and the benefits of Spring Data JPA for simplifying database interactions.

Uploaded by

gs23133
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

JavaProgrammingForBeginners-Presentation-46-60

The document outlines the deployment process for Spring Boot applications, highlighting the advantages of using embedded servers over traditional WAR deployment. It discusses Spring Boot features such as Actuator for monitoring, DevTools for productivity, and the differences between Spring Boot, Spring MVC, and the core Spring Framework. Additionally, it covers unit testing with JUnit, the use of JPA and Hibernate, and the benefits of Spring Data JPA for simplifying database interactions.

Uploaded by

gs23133
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Spring Boot Embedded Servers

How do you deploy your application?


Step 1 : Install Java
Step 2 : Install Web/Application Server
Tomcat/WebSphere/WebLogic etc
Step 3 : Deploy the application WAR (Web ARchive)
This is the OLD WAR Approach
Complex to setup!

Embedded Server - Simpler alternative


Step 1 : Install Java
Step 2 : Run JAR file
Make JAR not WAR (Credit: Josh Long!)
Embedded Server Examples:
spring-boot-starter-tomcat
spring-boot-starter-jetty
spring-boot-starter-undertow

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

Spring Boot DevTools: Increase developer


productivity
Why do you need to restart the server for every code
change?

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

Advantages: Get started quickly with production ready


features!

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)

Remember - H2 is in memory database


Does NOT persist data
Great for learning
BUT NOT so great for production

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) {}
}
}
}

Spring JDBC example


public void deleteTodo(int id) {
jdbcTemplate.update("delete from todo where id=?", id);
}

57
JPA Example
@Repository
public class PersonJpaRepository {

@PersistenceContext
EntityManager entityManager;

public Person findById(int id) {


return entityManager.find(Person.class, id);
}

public Person update(Person person) {


return entityManager.merge(person);
}

public Person insert(Person person) {


return entityManager.merge(person);
}

public void deleteById(int id) {........

Spring Data JPA Example


public interface TodoRepository extends JpaRepository<Todo, Integer>{

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

You might also like