JavaProgrammingForBeginners-Presentation-31-45
JavaProgrammingForBeginners-Presentation-31-45
31
Question 3: Does the Spring Framework really add value? -
In Game Runner Hello World App, we have very few classes
BUT Real World applications are much more complex:
Multiple Layers (Web, Business, Data etc)
Each layer is dependent on the layer below it!
Example: Business Layer class talks to a Data Layer class
Data Layer class is a dependency of Business Layer class
There are thousands of such dependencies in every application!
32
Question 4: What if I want to run Super Contra game?
Try it as an exercise
@Primary
Playing with Spring:
Exercise:
Dummy implementation for PacMan and make it Primary!
Debugging Problems:
Remove @Component and Play with it!
33
Question 5: How is Spring JAR downloaded? (Maven)
What happens if you manually download Spring JAR?
Remember: Spring JAR needs other JARs
What if you need to upgrade to a new version?
Maven: Manage JARs needed by apps (application dependencies)
Once you add a dependency on Spring framework, Maven would download:
Spring Framework and its dependencies
34
Exploring Spring - Dependency Injection Types
Constructor-based : Dependencies are set
by creating the Bean using its Constructor
Setter-based : Dependencies are set by
calling setter methods on your beans
Field: No setter or constructor.
Dependency is injected using reflection.
Which one should you use?
Spring team recommends Constructor-based
injection as dependencies are automatically set
when an object is created!
35
Spring Modules
36
Spring Projects
Spring Projects: Spring keeps evolving (REST API > Microservices > Cloud)
Spring Boot: Most popular framework to build microservices
Spring Cloud: Build cloud native applications
Spring Data: Integrate the same way with different types of databases : NoSQL and Relational
Spring Integration: Address challenges with integration with other applications
Spring Security: Secure your web application or REST API or microservice
37
Why is Spring Popular?
Loose Coupling: Spring manages beans and dependencies
Make writing unit tests easy!
Provides its own unit testing project - Spring Unit Testing
Reduced Boilerplate Code: Focus on Business Logic
Example: No need for exception handling in each method!
All Checked Exceptions are converted to Runtime or Unchecked Exceptions
38
Spring JDBC - Example
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) {}
}
}
}
39
Spring Framework - Review
Goal: 10,000 Feet overview of Spring Framework
Help you understand the terminology!
Dependency
Dependency Injection (and types)
Autowiring
Spring Beans
Component Scan
IOC Container (Application Context)
We will play with other Spring Modules and Projects later in the course
Advantages: Loosely Coupled Code (Focus on Business Logic),
Architectural Flexibility and Evolution with time!
40
Getting Started with Spring Boot - Goals
Build a Hello World App in Modern Spring
Boot Approach
Get Hands-on with Spring Boot
Why Spring Boot?
Terminology
Spring Initializr
Auto Configuration
Starter Projects
Actuator
Developer Tools
41
Hands-on: Understand Power of Spring Boot
// http://localhost:8080/courses
[
{
"id": 1,
"name": "Learn Microservices",
"author": "in28minutes"
}
]
42
World Before Spring Boot!
https://github.com/in28minutes/SpringMvcStepByStep/blob/master/Step15.md#pomxml
Setting up Spring Web Projects before Spring Boot was NOT easy!
Define maven dependencies and manage versions for frameworks
spring-webmvc, jackson-databind, log4j etc
Define web.xml (/src/main/webapp/WEB-INF/web.xml)
Define Front Controller for Spring Framework (DispatcherServlet)
Define a Spring context XML file (/src/main/webapp/WEB-INF/todo-servlet.xml)
Define a Component Scan (<context:component-scan base-package="com.in28minutes" />)
Install Tomcat or use tomcat7-maven-plugin plugin (or any other web server)
Deploy and Run the application in Tomcat
How does Spring Boot do its Magic?
Spring Boot Starter Projects
Spring Boot Auto Configuration
43
Spring Boot Starter Projects
Goal of Starter Projects: Help you get a project up and running
quickly!
Web Application - Spring Boot Starter Web
REST API - Spring Boot Starter Web
Talk to database using JPA - Spring Boot Starter Data JPA
Talk to database using JDBC - Spring Boot Starter JDBC
Secure your web application or REST API - Spring Boot Starter Security
Manage list of maven dependencies and versions for different
kinds of apps:
Spring Boot Starter Web: Frameworks needed by typical web applications
spring-webmvc, spring-web, spring-boot-starter-tomcat, spring-boot-starter-json
44
Spring Boot Auto Configuration
Spring Boot provides Auto Configuration
Basic configuration to run your application using the
frameworks defined in your maven dependencies
Auto Configuration is decided based on:
Which frameworks are in the Class Path?
What is the existing configuration (Annotations etc)?
An Example: (Enable debug logging for more details):
If you use Spring Boot Starter Web, following are auto
configured:
Dispatcher Servlet (DispatcherServletAutoConfiguration)
Embedded Servlet Container - Tomcat is the default
(EmbeddedWebServerFactoryCustomizerAutoConfiguration)
Default Error Pages (ErrorMvcAutoConfiguration)
Bean to/from JSON conversion
(J k H M C C fi i )
45