0% found this document useful (0 votes)
8 views51 pages

Unit I

The document provides an overview of the Spring Framework, highlighting its purpose, features, and various modules that facilitate Java application development. It emphasizes the importance of Dependency Injection (DI) and Inversion of Control (IoC) in creating loosely coupled, maintainable applications. Additionally, it outlines the differences between Constructor and Setter Dependency Injection methods, along with examples of configuring beans and managing dependencies in Spring applications.

Uploaded by

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

Unit I

The document provides an overview of the Spring Framework, highlighting its purpose, features, and various modules that facilitate Java application development. It emphasizes the importance of Dependency Injection (DI) and Inversion of Control (IoC) in creating loosely coupled, maintainable applications. Additionally, it outlines the differences between Constructor and Setter Dependency Injection methods, along with examples of configuring beans and managing dependencies in Spring applications.

Uploaded by

GANESH GANA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Spring

Unit I

Name of the Faculty


Bh Sai Ganesh
CSE
Aditya University
Spring
Spring is a popular open-source Java application development framework created by Rod
Johnson. Spring supports developing any kind of Java application such as standalone
applications, web applications, database-driven applications, and many more.
The basic objective of the framework was to reduce the complexity involved in the
development of enterprise applications. But today Spring is not limited to enterprise
application development, many projects are available under the Spring umbrella to develop
different kinds of applications of today’s need such as cloud-based applications, mobile
applications, batch applications, etc. Spring framework helps in developing a loosely coupled
application that is simple, easily testable, reusable, and maintainable.

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


2
Why Spring?
Spring is everywhere
Spring’s flexible libraries are trusted by developers all over the world. Spring also has contributions from
all the big names in tech, including Alibaba, Amazon, Google, Microsoft, and more.
Spring is flexible
Spring’s flexible and comprehensive set of extensions and third-party libraries let developers build almost
any application imaginable.
Spring is fast
With Spring, you’ll notice fast startup, fast shutdown, and optimized execution, by default. Increasingly,
Spring projects also support the reactive programming model for even greater efficiency.

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


3
Why Spring?
Spring is secure
Spring has a proven track record of dealing with security issues quickly and responsibly. Spring
Security makes it easier for you to integrate with industry-standard security schemes and
deliver trustworthy solutions that are secure by default.
Spring is supportive
No matter where you are on your journey, you can find the support and resources
Spring is light Weight
https://spring.io/why-spring

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


4
Spring Versions

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


5
API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.
6
The following are the main features of the Spring Framework.

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


7
Modules
Spring Framework 5.x has the following key module groups:
Core Container: These are core modules that provide key features of the Spring
framework.
Data Access/Integration: These modules support JDBC and ORM data access
approaches in Spring applications.
Web: These modules provide support to implement web applications.
Others: Spring also provides few other modules such as the Test for testing Spring
applications.

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


8
API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.
9
Core container
Core: This is the key module of Spring Framework which provides fundamental support on
which all other modules of the framework are dependent.
Bean: This module provides a basic Spring container called BeanFactory.
Context: This module provides one more Spring container called ApplicationContext which
inherits the basic features of the BeanFactory container and also provides additional features to
support enterprise application development.
Spring Expression Language (SpEL): This module is used for querying/manipulating object
values.
AOP (Aspect Oriented Programming) and aspects: These modules help in isolating cross-
cutting functionality from business logic.

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


10
Data Access/Integration
The following modules support Data Access/Integration:
Java Database Connectivity (JDBC): It provides an abstract layer to support JDBC calls to
relational databases.
Object Relational Mapping (ORM): It provides integration support for popular
ORM(Object-Relational Mapping) solutions such as Hibernate, JPA, etc.
Transactions: It provides a simple transaction API which abstracts the complexity of
underlying repository specific transaction API's from the application.

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


11
Web Layer
Spring Framework provides the following modules to support web application
development:
Web: This module has a container called web application context which inherits basic
features from ApplicationContext container and adds features to develop web based
applications.
Webmvc: It provides the implementation of the MVC(model-view-controller) pattern
to implement the serverside presentation layer and also supports features to implement
RESTful Web Services.

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


12
WebFlux: Spring 5.0 introduced a reactive stack with a web framework called Spring
WebFlux to support Reactive programming in Spring's web layer and runs on containers such as
Netty, Undertow, and Servlet 3.1+.
WebSocket: It is used for 2 way communication between client and server in WebSocket based
web applications.
Test Layer
Test: This module provides the required support to test Spring applications using TestNG or
JUnit.

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


13
Dependency Injection
Dependency Injection is a fundamental aspect of the Spring framework, through which the
Spring container “injects” objects into other objects or “dependencies”.

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


14
API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.
15
API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.
16
API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.
17
API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.
18
API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.
19
Dependency Injection
Dependency Injection is the main functionality provided by Spring IOC(Inversion of
Control). The Spring-Core module is responsible for injecting dependencies through
either Constructor or Setter methods.
The design principle of Inversion of Control emphasizes keeping the Java classes
independent of each other and the container frees them from object creation and
maintenance. These classes, managed by Spring, must adhere to the standard definition of
Java-Bean. Dependency Injection in Spring also ensures loose-coupling between the
classes.

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


20
Need for Dependency Injection
Suppose class One needs the object of class Two to instantiate or operate a method, then class
One is said to be dependent on class Two. Now though it might appear okay to depend a
module on the other but, in the real world, this could lead to a lot of problems, including system
failure. Hence such dependencies need to be avoided.
Spring IOC resolves such dependencies with Dependency Injection, which makes the
code easier to test and reuse. Loose coupling between classes can be possible by
defining interfaces for common functionality and the injector will instantiate the objects of
required implementation. The task of instantiating objects is done by the container according to
the configurations specified by the developer.

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


21
Here's how we would create an object dependency in traditional programming:
Class A
{
B b=new B();
b.show();
}
Class B
{
Void Show()
{
s.o.p(“Method of class B”);
}
}

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


22
By using DI, we can rewrite the example without specifying the implementation of the Class B that
we want:
public class A {
private B b;

// Proper constructor with dependency injection


public A(B b) {
this.b = b;
}

public void display() {


b.show();
}
} API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.
23
Two ways to perform Dependency Injection in Spring framework
Spring framework provides two ways to inject dependency
By Constructor
By Setter method
Constructer Dependency Injection
In this, the DI will be injected with the help of constructors. Now to set the DI as CDI
in bean,
 You provide the dependencies (objects your class needs) through the constructor of the
class.
 When you create an object of the class, you pass the required dependencies as arguments
to its constructor.
 This way, the class is given everything it needs to work right when it is created.
API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.
24
Setter Dependency Injection (SDI):
 This is the simpler of the two DI methods. In this, the DI will be injected with the help
of setter and/or getter methods.

 You provide the dependencies by calling setter methods on the object after it is
created.

 The class has special methods (setters) to receive the dependencies.

 You create the object first (using the default constructor), then inject the dependencies
by calling these setter methods.

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


25
Feature Constructor Injection Setter Injection

Through constructor
Injection Style Through setter methods
parameters

Mandatory Yes No

Immutability Supports Less suited

Readability Clear and concise Flexible

01/08/2025 API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.
26
A ready-to-use project folder structured like this:

SpringCDIProject/
├── src/
│ └── com/ramesh/Injection/
│ ├── App.java
│ ├── AppConstructor.java
│ ├── Employee.java
│ ├── EmployeeConstructor.java
│ ├── config.xml
│ └── configConstructor.xml
├── lib/ ← You will place your Spring JAR files here
├── bin/ ← Output folder for compiled classes

01/08/2025 API and Micro Services Bh sai Ganesh, Asst Professor, CSE.
27
•Employee.java defines the data structure or behavior of an Employee.

•Class.java (or any other main class) contains the program’s entry point and logic to create Employee
objects and run the application.

Spring xml file


•It’s an XML file where you declare beans and their dependencies.
•Spring reads this file at runtime to know how to create and wire your objects

01/08/2025 API and Micro Services Bh. Sai Ganesh, Asst Professor, CSE.
28
Constructor Dependancy Injection

EmployeeConstructor.java

public class EmployeeConstructor {


private String name;
private String phone;

public EmployeeConstructor(String name, String phone) {


this.name = name;
this.phone = phone;
}

public String getName() { return name; }


public String getPhone() { return phone; }
}
API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.
29
Application context.xml

<bean id="employeeBeanConstructor"
class="com.ramesh.Injection.EmployeeConstructor">
<constructor-arg value="Constructor Injection
Example"/>
<constructor-arg value="1234567890"/>
</bean>

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


30
AppConstructor.java

public class AppConstructor {


public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("com/ramesh/Injection/configConstructor.xml");
EmployeeConstructor emp = (EmployeeConstructor)
context.getBean("employeeBeanConstructor");
System.out.println(emp.getName());
System.out.println(emp.getPhone());
}
}

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


31
Step Command What happens

javac -cp "lib/*" -d bin


.class files are created
Compile src/com/ramesh/Injection/
inside bin/...
*.java

java -cp "bin;lib/*" JVM runs the


Run
com.ramesh.Injection.App compiled .class files

01/08/2025 API and Micro Services BH sai ganesh, Asst Professor, CSE.
32
Setter DI:

Employee.java

public class Employee {


private String name;
private String phone;

public void setName(String name) { this.name = name; }


public void setPhone(String phone) { this.phone = phone; }

public String getName() { return name; }


public String getPhone() { return phone; }
}

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


33
Application context.xml

<bean id="employeeBean"
class="com.ramesh.Injection.Employee">
<property name="name" value="Setter Injection Example"/>
<property name="phone" value="9876543210"/>
</bean>

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


34
App.java

public class App {


public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("com/ramesh/Injection/config.xml"
);
Employee emp = (Employee)
context.getBean("employeeBean");
System.out.println(emp.getName());
System.out.println(emp.getPhone());
}
}

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


35
IOC (Inversion of Control)

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


36
IOC (Inversion of Control)

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


37
IOC (Inversion of Control)

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


38
API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.
39
IOC (Inversion of Control)

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


40
IOC (Inversion of Control)

API and Micro Services K Vydehi, Asst Professor, CSE.


41
Inversion of Control
Inversion of Control is a principle in software engineering which transfers the control
of objects or portions of a program to a container or framework. We most often use it
in the context of object-oriented programming.
In contrast with traditional programming, in which our custom code makes calls to a
library, IoC enables a framework to take control of the flow of a program and make
calls to our custom code. To enable this, frameworks use abstractions with additional
behavior built in. If we want to add our own behavior, we need to extend the classes
of the framework or plugin our own classes.

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


42
The advantages of this are:
More reusable code: Switching different implementations without effecting the user of
an interface.
Loosely coupled dependencies: Depends only on abstraction hiding the
implementation.
greater modularity of a program
Because dependencies are reduced, it is simple to test and write.
We can achieve Inversion of Control through various mechanisms such as: Strategy
design pattern, Service Locator pattern, Factory pattern, and Dependency Injection
(DI).

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


43
• Application logic is provided through POJO classes.
• Configuration metadata consists of bean definitions that the container must
manage.
• IoC container produces objects required by the application using POJO
classes and configuration metadata. IoC container is of two types –
BeanFactory and ApplicationContext.
•The following diagram depicts that The Spring IoC container makes use of
Java POJO classes and configuration metadata to produce a fully configured
and executable application.

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


44
Spring provides two types of containers

BeanFactory:
It is the basic Spring container with features to instantiate, configure and manage the beans.

org.springframework.beans.factory.BeanFactory is the main interface representing a BeanFactory container.

ApplicationContext is another Spring container that is more commonly used in Spring applications.

org.springframework.context.ApplicationContext is the main Interface representing an ApplicationContext

container.
It inherits the BeanFactory features and provides added features to support enterprise services such as

internationalization, validation, etc.

ApplicationContext container includes all functionality of the BeanFactorycontainer. So,

ApplicationContext is preferred container for Spring application development.

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


45
Configuring IoC container using Java Based Configuration
The @Bean annotation is used to indicate that a method instantiates, configures, and initializes a new object to be
managed by the Spring IoC container.
Spring’s XML configuration, the @Bean annotation plays the same role as the element
You can use @Bean-annotated methods with any Spring @Configuration.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.companyname.projectname.customer.CustomerService;
import com.companyname.projectname.order.OrderService;
@Configuration
public class Application {
@Bean
public CustomerService customerService() {
return new CustomerService();
}
@Bean
public OrderService orderService() {
return new OrderService();
}
}

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


46
XML file
<beans>
<bean id="customerService"
class="com.companyname.projectname.CustomerService"/>
<bean id="orderService"
class="com.companyname.projectname.OrderService"/>
</beans>
Note that the method name and bean name in XML are exactly the same.

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


47
Auto Scanning: Auto scanning means Spring looks through your code (your packages) and automatically
creates and manages objects (beans) for the classes that are marked with specific annotations like
@Component, @Service, @Repository, or @Controller.
You don’t need to tell Spring about each class manually. It finds them and registers them for you.

Annotations:
@Component: To indicate that a class is a auto scan component
@Service: Indicates that a class is a service.(Business logic layer)
@Repository: Indicates that a class is a Data Access Object (Data Access Layer).
@Controller: Indicates that a class is a web controller in the MVC framework(Web Layer).

Without Auto Scanning With Auto Scanning

You manually write every bean definition You just add an annotation to your class

Tedious for big apps Saves time and effort

Easy to miss dependencies Spring handles all wiring automatically

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


48
@Service
public class CustomerService {
public void serve() {
System.out.println("Hello!");
}
}

Spring sees @Service and goes:


“Ah! This is a worker with a badge. I’ll register this class as a Spring bean called customer
service

You don’t need to do anything more.


Then in another class:

@Autowired
private CustomerService customerService;

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


49
Spring goes:
“You need a CustomerService? I already have one! Here it is.”

Concept Meaning
Spring automatically finds your classes
Auto scanning
and creates objects for them.
Use @SpringBootApplication in main
How to enable
class.
Use annotations like @Component,
How to tag a class
@Service, @Controller.
Use @Autowired to get the object
How to use the object
wherever you need it.

API and Micro Services Bh Sai Ganesh, Asst Professor, CSE.


50
Thank You

51

You might also like