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

JavaProgrammingForBeginners-Presentation-31-45

The document provides an overview of the Spring Framework, covering key concepts such as components, dependency injection, and the IoC container. It highlights the advantages of using Spring, including loose coupling, reduced boilerplate code, and architectural flexibility, as well as the evolution of Spring projects like Spring Boot and Spring Cloud. Additionally, it explains the importance of Maven for managing dependencies and introduces different types of dependency injection and Spring modules.

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-31-45

The document provides an overview of the Spring Framework, covering key concepts such as components, dependency injection, and the IoC container. It highlights the advantages of using Spring, including loose coupling, reduced boilerplate code, and architectural flexibility, as well as the evolution of Spring projects like Spring Boot and Spring Cloud. Additionally, it explains the importance of Maven for managing dependencies and introduces different types of dependency injection and Spring modules.

Uploaded by

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

Question 2: Spring Framework - Important Terminology

@Component (..): Class managed by Spring framework


Dependency: GameRunner needs GamingConsole impl!
GamingConsole Impl (Ex: MarioGame) is a dependency of GameRunner
Component Scan: How does Spring Framework find
component classes?
It scans packages! (@ComponentScan("com.in28minutes"))
Dependency Injection: Identify beans, their dependencies
and wire them together (provides IOC - Inversion of Control)
Spring Beans: An object managed by Spring Framework
IoC container: Manages the lifecycle of beans and dependencies
Types: ApplicationContext (complex), BeanFactory (simpler features - rarely used)
Autowiring: Process of wiring in dependencies for a Spring Bean

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!

With Spring Framework:


INSTEAD of FOCUSING on objects, their dependencies and wiring
You can focus on the business logic of your application!
Spring Framework manages the lifecycle of objects:
Mark components using annotations: @Component (and others..)
Mark dependencies using @Autowired
Allow Spring Framework to do its magic!

Ex: Controller > BusinessService (sum) > DataService (data)!

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

All configuration in pom.xml


Maven artifacts: Identified by a Group Id, an Artifact Id!
Important Features:
Defines a simple project setup that follows best practices
Enables consistent usage across all projects
Manages dependency updates and transitive dependencies
Terminology Warning: Spring Dependency vs Maven Dependency

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

Spring Framework is divided into modules:


Core: IoC Container etc
Testing: Mock Objects, Spring MVC Test etc
Data Access: Transactions, JDBC, JPA etc
Web Servlet: Spring MVC etc
Web Reactive: Spring WebFlux etc
Integration: JMS etc
Each application can choose the modules they want to make use of
They do not need to make use of all things everything in Spring framework!

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

Architectural Flexibility: Spring Modules and Projects


You can pick and choose which ones to use (You DON'T need to use all of
them!)
Evolution with Time: Microservices and Cloud
Spring Boot, Spring Cloud etc!

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

Spring JDBC example


public void deleteTodo(int id) {
jdbcTemplate.update(DELETE_TODO_QUERY, id);
}

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"
}
]

Let's Build a Hello World App using Spring Initializr


Setup BooksController

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

You might also like