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

Java-DUO.pdf.crmgft

Java Spring Boot

Uploaded by

suryasahoo396
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)
3 views

Java-DUO.pdf.crmgft

Java Spring Boot

Uploaded by

suryasahoo396
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
You are on page 1/ 24

DAY-28

Spring Project Structure and Core Concepts &


Introduction to Basic Spring Concepts

Tutor Mentor| 2024


1
Understanding the Spring Project Structure
A Spring project structure is critical for organizing code, configurations, and resources
efficiently.
Spring projects typically adhere to a standard Maven directory layout for consistency
and modularity.
Importance:
Enables separation of concerns by organizing code into logical parts.
Facilitates easier collaboration, testing, and scalability.

Tutor Mentor | 2024


2
Overview of the Basic Folder Structure
In a Spring project, the directory structure is generally as follows:
1. src/main/java: Contains the main application code, including controllers, services, and
repositories.
2. src/main/resources: Holds resource files like application.properties, XML
configurations, and templates.
3. src/test: Contains test files for unit testing and integration testing.

Tutor Mentor | 2024


3
Purpose of Each Directory
Directory Purpose
src/main/java Houses the application’s source code, organized into packages
like controller, service, and repository.

src/main/resources Stores configuration files and resources like


application.properties or XML configuration files.

src/main/webapp/WEB- Used in traditional Spring MVC projects to hold web.xml for


INF servlet configuration.

Tutor Mentor | 2024


4
Exploring src/main/java and Packages

Organize code into packages for modularity:


controller: Handles user requests and interacts with services.
service: Contains business logic and calls repositories.
repository: Manages database access.
Example Package Structure:
src/main/java
├── com.example.project
├── controller
├── service
├── repository
Tutor Mentor | 2024
5
Exploring src/main/resources

Stores configuration files like:


application.properties: Application-specific settings (e.g., database configuration).
applicationContext.xml: Spring XML configuration file (in older projects).

Tutor Mentor | 2024


6
Key File - web.xml in WEB-INF

web.xml (Deployment Descriptor):


Configures the DispatcherServlet.
Defines context parameters, servlet mappings, and listeners.

Tutor Mentor | 2024


7
Sample web.xml:
<web-app>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Tutor Mentor | 2024
8
Introduction to Basic Spring Concepts

Spring Framework provides powerful features like Inversion of Control (IoC) and
Dependency Injection (DI).
IoC: Spring controls object lifecycles and dependencies.
DI: Spring wires objects together automatically, reducing tight coupling.

Tutor Mentor | 2024


9
Overview of Inversion of Control (IoC)

IoC transfers control of object creation and management from the developer to the Spring
Container.
Benefits:
Improves modularity.
Enhances testability.
Simplifies object lifecycle management.

Tutor Mentor | 2024


10
Benefits of IoC in Spring

Decouples components, improving scalability.


Enables unit testing by replacing dependencies with mock objects.
Example of IoC in Spring:
@Component
public class ServiceExample {
private final RepositoryExample repository;

@Autowired
public ServiceExample(RepositoryExample repository) {
this.repository = repository; } }
Tutor Mentor | 2024
11
Introduction to Dependency Injection (DI)

DI is a design pattern used to inject dependencies at runtime.


Purpose: Eliminate hardcoded dependencies and improve code flexibility.

Tutor Mentor | 2024


12
How Dependency Injection Works in Spring

Spring manages object dependencies automatically via:


1. Constructor Injection: Dependencies provided via the constructor.
2. Setter Injection: Dependencies injected via setter methods.
3. Field Injection: Directly inject dependencies into fields (less recommended).

Tutor Mentor | 2024


13
Types of Dependency Injection

1.Constructor Injection:
@Component
public class MyService {
private final MyRepository repository;

@Autowired
public MyService(MyRepository repository) {
this.repository = repository;
}
}
Tutor Mentor | 2024
14

2. Setter Injection:
@Component
public class MyService {
private MyRepository repository;

@Autowired
public void setRepository(MyRepository repository) {
this.repository = repository;
}
}
Tutor Mentor | 2024
15

3. Field Injection (Discouraged):


@Component
public class MyService {
@Autowired
private MyRepository repository;
}

Tutor Mentor | 2024


16
Examples of DI in Spring

Constructor Injection Example:


@Component
public class OrderService {
private final OrderRepository orderRepository;

@Autowired
public OrderService(OrderRepository orderRepository) {
this.orderRepository = orderRepository;
}
}
Tutor Mentor | 2024
17
Introduction to Spring’s Layered Architecture
Spring applications are organized into three layers:
1. Controller Layer: Handles HTTP requests.
2. Service Layer: Processes business logic.
3. Repository Layer: Interacts with the database.

Tutor Mentor | 2024


18
Overview of Controller Layer
Purpose: Accept user input, process it, and return responses.
Example:
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, World!";
}
}

Tutor Mentor | 2024


19
Overview of Service Layer
Contains business logic.
Bridges the gap between the controller and repository layers.

Tutor Mentor | 2024


20
Overview of Repository Layer
Interacts with databases using tools like Spring Data JPA.
Example:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}

Tutor Mentor | 2024


21
Benefits of a Layered Architecture
1. Separation of Concerns: Each layer has a defined responsibility.
2. Scalability: Layers can be scaled independently.

Tutor Mentor | 2024


22
Summary of Key Concepts
Spring Project Structure:
Organized into logical parts for modularity.
Core Concepts:
IoC and DI simplify dependency management.
Layered Architecture:
Ensures clean, scalable, and maintainable code.

Tutor Mentor | 2024


Tutor Mentor | 2024

THANK YOU
Ask Me Anything!

You might also like