0% found this document useful (0 votes)
2 views29 pages

Lect7 SpringBoot2

The document covers core concepts of Spring Boot, focusing on Inversion of Control (IoC) and Dependency Injection (DI), which allow developers to manage object creation and dependencies more efficiently. It discusses various Spring annotations such as @Component, @Service, and @Controller, which help in configuring applications and improving code readability. Additionally, it outlines the structure of a Simple Task Planner project, including application properties and the role of different components like models, controllers, and services.

Uploaded by

malak21afifi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views29 pages

Lect7 SpringBoot2

The document covers core concepts of Spring Boot, focusing on Inversion of Control (IoC) and Dependency Injection (DI), which allow developers to manage object creation and dependencies more efficiently. It discusses various Spring annotations such as @Component, @Service, and @Controller, which help in configuring applications and improving code readability. Additionally, it outlines the structure of a Simple Task Planner project, including application properties and the role of different components like models, controllers, and services.

Uploaded by

malak21afifi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CCS2304 – Advanced

Programming Applications
Lecture 7
SpringBoot Core Concepts
• Inversion of Control (IoC)
• Dependency Injection (DI)
• @Component, @Service, @Controller
• Constructor injection
• Application properties
• @Configuration
Inversion of Control (IoC)
• Framework controls object creation
• Developer focuses on logic
• Loose coupling
Inversion of Control (IoC)
What problem does IoC solve?
In traditional programming, your code controls
everything:
• You create objects
• You decide when they’re created
• You decide which class depends on which
• You manage their lifecycle
This leads to:
• Tight coupling
• Hard-to-change code
• Hard-to-test code
Inversion of Control (IoC)
What’s wrong here?

Traditional code:

public class OrderService {

private PaymentService paymentService = new PaymentService();

public void placeOrder() {


[Link]();
}
}
Inversion of Control (IoC)
IoC means The control of object creation and dependency management is
moved outside your code
Instead of “I create what I need”
You say: “I declare what I need”
And something else provides it.

With IoC (Spring):

public class OrderService {

private PaymentService paymentService;

public OrderService(PaymentService paymentService) {


[Link] = paymentService;
}
}
Inversion of Control (IoC)
Who controls things now?

• The Spring IoC Container.


– Creates objects (beans)
– Wires dependencies
– Manages lifecycle
– Decides when and how objects exist
Dependency Injection (DI)
What is a dependency?
• Any object a class needs to do its job. Example
student class can contain course object.

What is a dependency injection?


• Dependencies are provided (injected) into a
class instead of the class creating them itself
• DI is one implementation of IoC
SpringBoot Annotations
Annotations in Spring Boot are metadata that provide
instructions to the Spring framework at runtime or compile time

Annotations allow developers to configure applications directly


in Java code
• Reduces XML-based configuration
• Improves code readability and maintainability
• Enables auto-configuration
• Simplifies dependency injection
• Speeds up application development
SpringBoot Annotations
@SpringBootApplication Annotation
• Used to mark the main class of a Spring Boot application

Example:
SpringBoot Annotations
@Component Annotation
• Marks a class as a generic Spring-managed object (bean)

• Spring automatically detects and registers it during


component scanning
• When Spring starts:
• It scans the project
• Finds @Component
• Creates an object
• Stores it in Application Context

• Example:
Reminder: Spring Boot Architecture
SpringBoot Annotations
@Service Annotation
• Used in the service layer to define business logic and
improve code readability

• Example:
SpringBoot Annotations
@Repository Annotation
• Used in the DAO layer to interact with the database.

• Automatically translates database-related exceptions


into Spring exceptions.

• Example:
Controllers in Spring
• @Controller
– used with HTML View
• @RestController
– used with JSON
• URL mappings
SpringBoot Application
The most common and efficient method to create a Spring Boot project is by using the web-
based Spring Initializer tool [Link] and importing the generated project in your IDE
Project Structure

• Lect7Application class is the starting point of Spring


• Runs with embedded Tomcat
• No need to install server
Simple Task Planner Project: Form
[Link]
Simple Task Planner Project: Model
• Task is a simple entity class (POJO)
• Note no annotations here so it will NOT be managed by Spring
• This is a simple class used to hold and transfer data between Spring layers
• Then, how is the object created??
Simple Task Planner Project: Controller
• Model is a Spring-provided
interface that acts as a
container for your data

• What is
“@ModelAttribute”?

• Spring creates the object on


the fly and fills it with form
data

• The object creation is the


responsibility of SpringMVC
and NOT Spring IoC
Container

• Then, will we ever need to


annotate the Task class?
Simple Task Planner Project
• When to annotate an entity class?
– when it becomes something more than a simple data
container
For example, if it becomes a database entity

• If we add @Component to Task, what happens?


– Spring will create ONE shared instance
– This is WRONG for user data
– Leads to data corruption between requests
Simple Task Planner Project: Controller
• This class acts as a servlet
controller

• The controller contacts the


service layer

• calling doGet() through link


“/new” will output the
previous input form

• calling the doPost() will use


the service class and output
the result page with an
appropriate message

• calling default doGet() will


output all created tasks
Simple Task Planner Project: Service
• This service class is only
focused on business logic

• This service class provides


logic for adding a new
task, getting all created
tasks, and calculating
urgency

• The service class can call


Model classes (Task, and
TaskValidator) to perform
the required logic

• The “TaskValidator” acts


as a utility class for the
“Task” Entity
Simple Task Planner Project: Model
• TaskValidator is a simple utility class the validates user input
• It’s managed by Spring (Spring bean) through the @Component
• Spring creates only 1 bean of this class and injects (uses) it
whenever required by other classes
Simple Task Planner Project: Output
[Link]
Simple Task Planner Project: Output
[Link]
Application Properties
• “[Link]” file is where we
control how the application behaves without
changing the code

• Spring Boot automatically reads it when the


app starts
Simple Task Planner Project:
Application Properties
We can:
• Change server settings
– [Link]=8081
• Change Thymeleaf properties
– [Link]=false
• Configure databases
– [Link]=jdbc:mysql://localhost:3306/test
– [Link]=root
– [Link]=1234
Simple Task Planner Project: Maven
• The created project will automatically include
all Spring dependencies

• However, we still need to add Thymeleaf


dependency

You might also like