0% found this document useful (0 votes)
21 views33 pages

Spring Framework and Maven Overview

This document provides an overview of the Spring Framework and Apache Maven, highlighting their roles in Java project management and development. It discusses the benefits of using Maven for dependency management and project structure, as well as the advantages of the Spring Framework, including simplified development and modular architecture. Additionally, it covers key components of Spring Boot, differences between Spring and Spring Boot, and various Spring annotations that facilitate configuration and dependency injection.
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)
21 views33 pages

Spring Framework and Maven Overview

This document provides an overview of the Spring Framework and Apache Maven, highlighting their roles in Java project management and development. It discusses the benefits of using Maven for dependency management and project structure, as well as the advantages of the Spring Framework, including simplified development and modular architecture. Additionally, it covers key components of Spring Boot, differences between Spring and Spring Boot, and various Spring annotations that facilitate configuration and dependency injection.
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

Pathway Course--Full Stack Development 2025-26

WEEK -7
Spring Framework
7.1 What is Apache Maven:
 Maven is a powerful project management tool that is based on POM (project object model).
 It is used for projects build, dependency and documentation.
 In short terms we can tell maven is a tool that can be used for building and managing any
Java-based project.
 Maven make the day-to-day work of Java developers easier and generally help with the
comprehension of any Java-based project

7.2 Problems without Maven:


There are many problems that we face during the project development. They are discussed below:
 Adding set of Jars in each project: In case of struts, spring, hibernate frameworks, we need
to add set of jar files in each project. It must include all the dependencies of jars also.
 Creating the right project structure: We must create the right project structure in servlet,
struts etc, otherwise it will not be executed.
 Building and Deploying the project: We must have to build and deploy the project so that it
may work.

7.3 Working of Maven and Maven Repository:

Computer Science and Engineering 1


Pathway Course--Full Stack Development 2025-26

<project xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link] [Link]
[Link]">
<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- [Link] -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>testng</artifactId>
<version>7.11.0</version>
<scope>test</scope>
</dependency>
<!-- [Link] -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>9.4.0</version>
</dependency>
</dependencies>
</project>

Computer Science and Engineering 2


Pathway Course--Full Stack Development 2025-26

7.4 Apache Maven Build Cycle:

7.5 7.5 7.5

7.5 Introduction to Spring Framework:


 The Spring framework provides comprehensive infrastructure support for developing Java
Enterprise edition applications.
 It's packed with some nice features like Dependency Injection, and out of the box modules
like:
 Spring JDBC
 Spring MVC
 Spring Security
 Spring AOP
 Spring ORMSpring Test
 These modules can drastically reduce the development time of an application.

7.6 Why Spring framework:


Java programs are complex and feature many heavyweight components. Heavyweight means
the components are dependent on the underlying operating system (OS) for their appearance and
properties.
 Spring is considered to be a secure, low-cost and flexible framework.

Computer Science and Engineering 3


Pathway Course--Full Stack Development 2025-26

 Spring improves coding efficiency and reduces overall application development time because
it is lightweight -- efficient at utilizing system resources -- and has a lot of support.
 Spring removes tedious configuration work so that developers can focus on writing business
logic.
Spring handles the infrastructure so developers can focus on the application.
7.7 Benefits of Spring Framework:
 Simplified Development: Spring reduces boilerplate code with features like Dependency
Injection and AOP, making development faster and easier.
 Loose Coupling: Dependency Injection ensures components are loosely coupled, improving
maintainability and testability.
 Modular: Spring's modular architecture allows developers to use only the required
components, improving flexibility and efficiency.
 Integration Support: Spring provides built-in support for various technologies like JDBC,
JMS and JPA, making integration with other systems seamless.
 Scalability: Spring's lightweight nature and support for various web and enterprise
components make it highly scalable for large applications.

7.8 Architecture of Spring Framework:


The Spring Framework architecture is modular, promoting loose coupling and enabling developers
to select only the necessary components for their applications. It is built around the core principles of
Inversion of Control (IoC) and Dependency Injection (DI).
The architecture contains following four main modules:
1. Core Container
2. Data Access/Integration
3. Web
4. Aspect Oriented Programming
5. Instrumentation
6. Testing

1. Core Container:
This is the foundation of the framework and includes:
 Core: Provides the fundamental IoC and DI functionalities, managing the lifecycle of
application components (beans).

Computer Science and Engineering 4


Pathway Course--Full Stack Development 2025-26

 Beans: Responsible for creating and managing Spring Beans, which are the objects forming
the backbone of a Spring application. It provides a sophisticated implementation of the factory
pattern, the BeanFactory.
 Context: Built upon the Core and Beans modules, it provides the ApplicationContext
interface, offering access to objects defined within the configuration and extending the
BeanFactory with enterprise-specific features like message resources and event handling.
 Spring Expression Language (SpEL): A powerful expression language for querying and
manipulating object graphs at runtime.

2. Data Access/Integration:
 JDBC: Provides an abstraction layer over raw JDBC, simplifying database interactions.
 ORM: Facilitates integration with Object-Relational Mapping (ORM) frameworks like
Hibernate, JPA, and JDO.
 OXM: Offers abstraction over Java Object XML Marshalling (OXM) implementations like
JAXB and XStream.
 JMS: Provides support for Java Message Service (JMS) for asynchronous messaging.
 Transactions: Offers a consistent transaction management abstraction across various
transaction APIs.

Computer Science and Engineering 5


Pathway Course--Full Stack Development 2025-26

3. Web:
 Web-MVC: Implements the Model-View-Controller (MVC) design pattern for building web
applications.
 Web-Socket: Provides support for WebSocket communication.
 Web-Portlet: Supports building web portlets for portal environments.
4. Aspect –Oriented Programming, Instrumentation and Testing:
Aspect-Oriented Programming (AOP):
 Enables the separation of cross-cutting concerns (e.g., logging, security) from business logic,
improving modularity.
Instrumentation:
 Provides support for class instrumentation and classloader enhancements.
Testing:
 Offers support for testing Spring applications with various testing frameworks.

7.9 Key Components of Spring Framework:

 Spring Boot Starters : It is one of the major key features or components of Spring
Boot Framework. The main responsibility of Spring Boot Starter is to combine a group
of common or related dependencies into single dependencies. When we add “spring-
boot-starter-web” jar file dependency to our build file, then Spring Boot Framework
will automatically download all required jars and add to our project classpath.
 Spring Boot AutoConfigurator: Traditional Spring Framework requires lots of
configurations and for this it uses either
• XML Configuration or
• Annotation Configuration.
Spring Boot Starter reduces build’s dependencies and Spring Boot AutoConfigurator
reduces the Spring Configuration.
 Spring Boot CLI Spring Boot CLI (Command Line Interface): It is Spring Boot
software to run and test Spring Boot applications from command prompt. When we
run Spring Boot applications using CLI, then it internally uses Spring Boot Starter and
Spring Boot AutoConfigurate components to resolve all dependencies and execute the
application.
 Spring Boot Actuator: When we run our Spring Boot Web Application, Spring Boot
Actuator automatically provides hostname as “localhost” and default port number as

Computer Science and Engineering 6


Pathway Course--Full Stack Development 2025-26

“8080”. We can access this application using “[Link] end point We


actually use HTTP Request methods like GET and POST to represent Management
Endpoints using Spring Boot Actuator. In simple words it connects our application to
some port and it is easily accessible we need not to deploy our application explicitly

7.10 Spring Boot:

 Spring Boot is a Java framework that makes it easier to create and run Java applications. It
simplifies the configuration and setup process, allowing developers to focus more on writing
code for their applications.
 Spring Boot, a module of the Spring framework, facilitates Rapid Application Development
(RAD) capabilities.
 Spring Boot is an open-source Java framework used to create a Micro Service.
 it provides a faster way to set up and an easier, configure, and run both simple and web-based
applications.
 It is a combination of Spring Framework and Embedded Servers. The main goal of Spring
Boot is to reduce development, unit test, and integration test time and in Spring Boot, there is
no requirement for XML configuration.

7.11 Why Spring Boot?:

Spring Boot is a powerful and widely-used framework in the Java ecosystem, designed to simplify the
development of web and enterprise applications.
 Rapid Development: Spring Boot enables Rapid Application Development (RAD) by
minimizing boilerplate code and configuration.
 Auto-Configuration: It automatically configures your application based on the dependencies
you include, saving time and reducing errors.
 Embedded Servers: You can run applications directly with embedded servers like Tomcat
or Jetty—no need for external deployment setups.

Computer Science and Engineering 7


Pathway Course--Full Stack Development 2025-26

 Microservices Ready: Spring Boot is ideal for building microservices architectures,


especially when paired with Spring Cloud.
 No XML Configuration: It eliminates the need for verbose XML configuration, using
annotations and Java-based configuration instead.
 Production-Ready Features: Includes built-in metrics, health checks, and externalized
configuration to support production environments.
 Spring Ecosystem Integration: Seamlessly integrates with other Spring projects like Spring
Data, Spring Security, and Spring Batch

7.12 Spring Vs Spring Boot:

[Link] Spring Spring Boot

1. Spring is an open-source lightweight Spring Boot is built on top of the


framework widely used to develop conventional spring framework, widely used
enterprise applications. to develop REST APIs.
2. The most important feature of the Spring The most important feature of the Spring
Framework is dependency injection. Boot is Autoconfiguration.
3. It helps to create a loosely coupled It helps to create a stand-alone application.
application.
4. To run the Spring application, we need Spring Boot provides embedded servers
to set the server explicitly. such as Tomcat and Jetty etc.
5. To run the Spring application, a There is no requirement for a deployment
deployment descriptor is required. descriptor.
6. To create a Spring application, the It reduces the lines of code.
developers write lots of code.
7. It doesn’t provide support for the in- It provides support for the in-memory
memory database. database such as H2.
8. Developers need to write boilerplate In Spring Boot, there is reduction in
code for smaller tasks. boilerplate code.
9. Developers have to define dependencies [Link] file internally handles the required
manually in the [Link] file. dependencies

7.13 Spring initializr:

 Spring Initializr is a Web-based tool that generates the Spring Boot project structure. The
spelling mistake in initializr is inspired by initializr.

Computer Science and Engineering 8


Pathway Course--Full Stack Development 2025-26

 It is very easy for the developers to select the necessary configuration for their projects.
 The Spring Initializr tool takes care of the following configuration for any Spring-based
project.
 Build tool(Maven or Gradle) to build the application.
 Spring Boot version(Dependencies are added based on the version).
 Dependencies required for the project.
 Language and its version.
 Project Metadata like name, packaging (Jar or War), package name etc.
The Spring Initializr UI has the following options,
 Project: Using this one can create Maven or Gradle project i.e; Maven or Gradle can be used
as a build tool. The default option is Maven Project. Maven project is used in the entire
tutorial.
 Language: Spring Initializr provide Java, Kotlin and Groovy as a programming language for
the project. Java is the default option.
 Spring Boot Version: Using this one can select the Spring Boot version for their project.
Spring Boot latest Version is 2.5.0. The SNAPSHOT versions are under development and are
not stable.
 Project Dependencies: Dependencies are artifacts that we can add to the project. I am
selecting Web Dependency.
 Project Metadata: It is the information about the project.

7.14 Spring annotation:

 Spring annotations are a form of metadata used in the Spring Framework to configure and
define the behavior of Spring-managed components.
 They provide a declarative way to express intentions and guide the Spring container in
processing classes, methods, and fields during runtime.
 This approach significantly simplifies configuration compared to older XML-based methods,
leading to cleaner, more readable, and easier-to-maintain code
Spring Annotation Description
@Entity Marks a class as a JPA entity (mapped to DB table)
@Id This annotation is the JPA is used for making specific variable
primary key.

Computer Science and Engineering 9


Pathway Course--Full Stack Development 2025-26

@SpringBootApplication This annotation is used to mark the main class of a Spring Boot
application. It is a combination of three annotations
@EnableAutoConfiguration, @ComponentScan, and
@Configuration.
@Autowired Used for dependency injection (field, setter, or constructor).
@GetMapping It maps the HTTP GET requests on the specific handler method. It is
used to create a web service endpoint that fetches
@PostMapping It maps the HTTP POST requests on the specific handler method. It
is used to create a web service endpoint that creates
@PutMapping It maps the HTTP PUT requests on the specific handler method. It is
used to create a web service endpoint that creates or updates
@DeleteMapping It maps the HTTP DELETE requests on the specific handler method.
It is used to create a web service endpoint that deletes a resource.
@RestController It can be considered as a combination of @Controller and
@ResponseBody annotations
@RequestBody Binds JSON/XML request body to a Java object.
@ResponseBody Converts method return value into JSON/XML.
@Table Specifies table details for an entity.
@GeneratedValue Specifies primary key generation strategy.
@Column Maps an entity field to a DB column.
@Transactional Defines transaction boundaries.
@ComponentSca Automatically scans and registers beans from the package.
@Qualifier Specifies which bean to inject when multiple candidates exist.
@Primary Marks a bean as the default when multiple beans of the same type are
available.

7.15 Inversion of Control and Dependency Injection:


What is 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. Instead of the developer explicitly creating and
managing object dependencies, the control is "inverted" and delegated to the Spring IoC container.
 It uses Dependency Injection (DI), based on configurations from XML files, Java-based
configuration, annotations or POJOs.
 Since the container, not the developer, controls object creating and wiring, it's called
Inversion of Control (IoC).
Key Features of IoC Container:
 Dependency Injection: Automatically injects dependencies into our classes.
 Lifecycle Management: Manages the lifecycle of beans, including instantiation,
initialization and deletion.
Computer Science and Engineering 10
Pathway Course--Full Stack Development 2025-26

 Configuration Flexibility: Supports both XML-based and annotation-based configurations.


 Loose Coupling: Promotes loose coupling by decoupling the implementation of objects
from their usage.
Types of IoC Container
1. BeanFactory
2. ApplicationContext
What is Dependency Injection?
Dependency injection (DI) is a design pattern that decouples code by providing dependencies to a
class from an external source rather than having the class create them itself. Instead of a class
instantiating its dependencies internally, they are "injected" into the class, typically through its
constructor, method parameters, or properties.
Types of Dependency Injection: There are three types of Dependency Injection:
 Constructor Injection
 Setter Injection
 Field Injection
1. Constructor Injection:
When we inject the dependency of one class into another class via the constructor, then it is called
constructor injection.
[Link] Injection(Property Injection):
When we inject the dependency of one class into another class via the setter method, then it is called
setter injection.
3. Field Injection:
Dependencies are injected directly into the field by using the @Autowired or manually

Example: Constructor Injection


//[Link]
public class StudentData {
private String regno;
private String sname;
private int age;
public StudentData(String regno, String sname, int age) {
super();
[Link] = regno;
[Link] = sname;

Computer Science and Engineering 11


Pathway Course--Full Stack Development 2025-26

[Link] = age;
}

public void showdata() {


[Link]("Student Register No:" +regno);
[Link]("Student name:" +sname);
[Link]("Student age:" +age);
}

//[Link]
public class StudentDetail {
private StudentData sdata;
public StudentDetail(StudentData sdata) {
super();
[Link] = sdata;
}
public void display() {
[Link]("=====Student Details====");
[Link]();
}
}
//[Link]
import [Link];
import [Link];

public class Student {

public static void main(String[] args) {


ApplicationContext context = new ClassPathXmlApplicationContext("[Link]");
StudentDetail sd = (StudentDetail) [Link]("s");
[Link]();

Computer Science and Engineering 12


Pathway Course--Full Stack Development 2025-26

}
//[Link]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="
[Link]
[Link]
<bean id ="s" class ="StudentDetail">
<constructor-arg name="sdata" ref="sd" />
</bean>

<bean id="sd" class="StudentData">


<constructor-arg name ="regno" value="12854" />
<constructor-arg name="sname" value="Satish Patil" />
<constructor-arg name="age" value="17" />
</bean>

</beans>
Example: Setter Injection
//[Link]
public class StudentData {
private String regno;
private String sname;
private int age;

public void setRegno(String regno) {


[Link] = regno;
}

public void setSname(String sname) {

Computer Science and Engineering 13


Pathway Course--Full Stack Development 2025-26

[Link] = sname;
}

public void setAge(int age) {


[Link] = age;
}

public void showdata() {


[Link]("Student Register No:" +regno);
[Link]("Student name:" +sname);
[Link]("Student age:" +age);
}

}
//[Link]
public class StudentDetail {
private StudentData sdata;

public void setSdata(StudentData sdata) {


[Link] = sdata;
}

public void display() {


[Link]("=====Student Details====");
[Link]();
}

}
//[Link]
import [Link];
import [Link];

public class Student {

Computer Science and Engineering 14


Pathway Course--Full Stack Development 2025-26

public static void main(String[] args) {


ApplicationContext context = new ClassPathXmlApplicationContext("[Link]");
StudentDetail sd = (StudentDetail) [Link]("s");
[Link]();

}
//[Link]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="
[Link]
[Link]
<bean id ="s" class ="StudentDetail">
<property name="sdata" ref="sd" />
</bean>

<bean id="sd" class="StudentData">


<property name ="regno" value="120CS24001" />
<property name="sname" value="Satish Patil" />
<property name="age" value="17" />
</bean>

</beans>
Example: Field Injection
//[Link]
public class StudentData {
private String regno;
private String sname;
private int age;
public void setRegno(String regno) {
[Link] = regno;

Computer Science and Engineering 15


Pathway Course--Full Stack Development 2025-26

public void setSname(String sname) {


[Link] = sname;
}
public void setAge(int age) {
[Link] = age;
}
public void showdata() {
[Link]("Student Register No:" +regno);
[Link]("Student name:" +sname);
[Link]("Student age:" +age);
}

}
//[Link]
import [Link];
public class StudentDetail {
@Autowired
private StudentData sdata;
public void display() {
[Link]("=====Student Details====");
[Link]();
}

}
//[Link]
import [Link];
import [Link];

public class Student {

public static void main(String[] args) {


ApplicationContext context = new ClassPathXmlApplicationContext("[Link]");

Computer Science and Engineering 16


Pathway Course--Full Stack Development 2025-26

StudentDetail sd = (StudentDetail) [Link]("s");


[Link]();

}
//[Link]
<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="[Link]
xmlns:xsi="[Link]
xmlns:context="[Link]
xsi:schemaLocation="[Link]
[Link]
[Link]
[Link] ">

<context:annotation-config />

<bean id="sdata" class="StudentDetail">


</bean>

<bean id="student1" class="StudentData">


<property name="sname" value="Satish Patil"></property>
<property name="regno" value="12354"></property>
<property name="age" value="17"></property>
</bean>

</beans>

7.16 ApplicationContext:

The ApplicationContext represents the Spring IoC (Inversion of Control) container within the Spring
Framework. It is a central component responsible for instantiating, configuring, and assembling the
various objects (known as "beans") that constitute a Spring application.
Key Characteristics of ApplicationContext:
 Spring IoC Container: The ApplicationContext is the primary interface for interacting with
the Spring IoC container. This container manages the lifecycle of application objects, from
creation to destruction, and handles their dependencies through Dependency Injection (DI).
 Configuration Metadata: The ApplicationContext obtains its instructions for creating and
managing beans from configuration metadata. This metadata can be defined in various
formats, including XML, Java annotations, or Java code.

Computer Science and Engineering 17


Pathway Course--Full Stack Development 2025-26

 Superset of BeanFactory: The ApplicationContext is a sub-interface of BeanFactory, which


is the root interface for accessing the Spring container. While BeanFactory provides basic
bean management functionalities, ApplicationContext extends these capabilities with
enterprise-specific features.

Implementation:

Implementation Description
ClassPathXmlApplicationContext Loads context from an XML file in the classpath

FileSystemXmlApplicationContext Loads context from an XML file in the file


system
AnnotationConfigApplicationContext Loads context from Java-based configuration
using annotations
XmlWebApplicationContext Specialized context for web applications (used in
Spring MVC)

7.17 ComponentScan:

 The @ComponentScan annotation tells Spring to scan specific packages for components
annotated with @Component, @Service, @Repository, or @Controller.
 Without arguments, the @ComponentScan annotation scans the package of the annotated
class and its sub-packages by default.
 The @ComponentScan annotation is used alongside @Configuration to define package
scanning in a configuration class.
 The @ComponentScan annotation helps spring automatically detect and register beans
without needing manual bean declaration.

Example:
//[Link]
package ComponentAnnotation;
import [Link];
@Component
public class Student {
public void test() {
[Link]("Student name is Satish Patil");

Computer Science and Engineering 18


Pathway Course--Full Stack Development 2025-26

}
}
//StudentConfig
package ComponentAnnotation;
import [Link];
import [Link];

@Configuration
@ComponentScan(basePackages = "ComponentAnnotation")
public class StudentConfig {

//[Link]
package ComponentAnnotation;

import [Link];
import [Link];

public class TestApp {

public static void main(String[] args) {


ApplicationContext context = new
AnnotationConfigApplicationContext([Link]);
Student s = [Link]([Link]);
[Link]();
}

7.18 Dependency Injection in Spring boot:


Dependency Injection (DI) is a core principle in Spring Boot, enabling the Spring IoC (Inversion of
Control) container to manage the dependencies between components. Instead of objects creating
their own dependencies, the container provides (injects) them at runtime.

Computer Science and Engineering 19


Pathway Course--Full Stack Development 2025-26

Spring Boot supports various ways to inject dependencies:


 Constructor Injection: Dependencies are provided through the class constructor.
 Setter Injection: Dependencies are injected using setter methods.
 Field Injection: Dependencies are injected directly into fields using annotations like
@Autowired.

Example: Field Injection


//[Link]
package [Link].StudentDemo1;

import [Link];

@Component
public class StudentName {

public void showname() {


[Link]("Student name is Satish Patil");
}

}
//[Link]
package [Link].StudentDemo1;

import [Link];
import [Link];

@Component
public class StudentDept {
@Autowired
StudentName sname;
public void studentdept() {
[Link]();
[Link]("Student Dept is EC");
}

Computer Science and Engineering 20


Pathway Course--Full Stack Development 2025-26

}
//[Link]
package [Link].StudentDemo1;

import [Link];
import [Link];
import [Link];

@SpringBootApplication
public class StudentDemo1Application {

public static void main(String[] args) {


ApplicationContext context =
[Link]([Link], args);
StudentDept s = [Link]([Link]);
[Link]();
}

Example: Constructor Injection


//[Link]
package [Link].StudentDemo1;

import [Link];

@Component
public class StudentName {

public void showname() {


[Link]("Student name is Satish Patil");
}

Computer Science and Engineering 21


Pathway Course--Full Stack Development 2025-26

}
//[Link]
package [Link].StudentDemo1;

import [Link];
import [Link];

@Component
public class StudentDept {

StudentName sname;

public StudentDept(StudentName sname) {


super();
[Link] = sname;
}

public void studentdept() {


[Link]();
[Link]("Student Dept is EC");
}

//[Link]
package [Link].StudentDemo1;

import [Link];
import [Link];
import [Link];

@SpringBootApplication
public class StudentDemo1Application {

Computer Science and Engineering 22


Pathway Course--Full Stack Development 2025-26

public static void main(String[] args) {


ApplicationContext context =
[Link]([Link], args);
StudentDept s = [Link]([Link]);
[Link]();
}

Example: Setter Injection


//[Link]
package [Link].StudentDemo1;

import [Link];

@Component
public class StudentName {

public void showname() {


[Link]("Student name is Satish Patil");
}

}
//[Link]
package [Link].StudentDemo1;

import [Link];
import [Link];
@Component
public class StudentDept {
StudentName sname;
@Autowired
public void setSname(StudentName sname) {
[Link] = sname;

Computer Science and Engineering 23


Pathway Course--Full Stack Development 2025-26

}
public void studentdept() {
[Link]();
[Link]("Student Dept is EC");
}

//[Link]
package [Link].StudentDemo1;

import [Link];
import [Link];
import [Link];

@SpringBootApplication
public class StudentDemo1Application {

public static void main(String[] args) {


ApplicationContext context =
[Link]([Link], args);
StudentDept s = [Link]([Link]);
[Link]();
}

}
7.19 Autowiring:

 Autowiring feature of spring framework enables us to inject the object dependency implicitly.
 It internally uses setter or constructor injection.
 Autowiring can't be used to inject primitive and string values. It works with reference only.

There are four modes of autowiring a bean using an XML configuration:

Computer Science and Engineering 24


Pathway Course--Full Stack Development 2025-26

1. no: the default value – this means no autowiring is used for the bean and we have to explicitly
name the dependencies.
2. byName: autowiring is done based on the name of the property, therefore Spring will look for a
bean with the same name as the property that needs to be set.
3. byType: similar to the byName autowiring, only based on the type of the property. This means
Spring will look for a bean with the same type of the property to set. If there's more than one
bean of that type, the framework throws an exception.
4. constructor: autowiring is done based on constructor arguments, meaning Spring will look for
beans with the same type as the constructor arguments.

Example:
//[Link]
public class StudentName {
public void showname() {
[Link]("Student name is Satish Patil");
}
}
//[Link]
import [Link];

public class StudentDept {


@Autowired
private StudentName sname;

public StudentDept(StudentName sname) {


super();
[Link] = sname;
}

public void showdept() {


[Link]();
[Link]("Department is Satish Patil");
}

Computer Science and Engineering 25


Pathway Course--Full Stack Development 2025-26

}
//[Link]
import [Link];
import [Link];

public class StudentDemo {

public static void main(String[] args) {


ApplicationContext context = new
ClassPathXmlApplicationContext("[Link]");
StudentDept d = (StudentDept) [Link]("sd");
[Link]();

}
//[Link]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="
[Link]
[Link]

<bean id="s" class="StudentName" >


</bean>
<bean id ="sd" class ="StudentDept" autowire="no">
</bean>
</beans>
7.19 Qualifier:

 @Qualifier annotation is used with @Autowired to resolve ambiguity when multiple beans
of the same type are present in the Spring container.
 It helps Spring determine which specific bean to inject by specifying the bean ID (name).

Computer Science and Engineering 26


Pathway Course--Full Stack Development 2025-26

 @Qualifier can be used on Setter methods, Fields and Constructor parameters.


 Without @Qualifier, if more than one matching bean exists, Spring throws a
NoUniqueBeanDefinitionException.
 Using @Qualifier("beanName") ensures that the correct and intended bean is injected during
autowiring
Example:
//[Link]
public class StudentData {
private String sname;
private String regno;
private int age;
public void setSname(String sname) {
[Link] = sname;
}
public void setRegno(String regno) {
[Link] = regno;
}
public void setAge(int age) {
[Link] = age;
}
public void display() {
[Link]("Student name:" +sname);
[Link]("Student Register No:"+regno);
[Link]("Student age:"+age);
}

}
//[Link]
import [Link];
import [Link];

public class StudentDetail {


@Autowired
@Qualifier("student2")

Computer Science and Engineering 27


Pathway Course--Full Stack Development 2025-26

private StudentData sdata;


public void show() {
[Link]("===Student Details===");
[Link]();
}

}
//[Link]
import [Link];
import [Link];

public class Student {

public static void main(String[] args) {


ApplicationContext context =
new ClassPathXmlApplicationContext("[Link]");
StudentDetail student = (StudentDetail) [Link]("sd");
[Link]();
}

}
//[Link]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="[Link]
xmlns:xsi="[Link]
xmlns:context="[Link]
xsi:schemaLocation="[Link]
[Link]
[Link]
[Link] ">
<context:annotation-config />
<bean id="sd" class="StudentDetail">
</bean>

Computer Science and Engineering 28


Pathway Course--Full Stack Development 2025-26

<bean id ="student1" class="StudentData">


<property name="sname" value="Satish Patil"></property>
<property name="regno" value="12345"></property>
<property name="age" value="17"></property>
</bean>

<bean id ="student2" class="StudentData">


<property name="sname" value="Preetam Patil"></property>
<property name="regno" value="12345"></property>
<property name="age" value="17"></property>
</bean>
</beans>
Output:
===Student Details===
Student name:Preetam Patil
Student Register No:12345
Student age:17

7.19 Bean Scope(Object Scope):

 Bean Scopes refer to the lifecycle of a Bean, which means when the object of a Bean is instantiated,
how long it lives, and how many objects are created for that Bean throughout its lifetime. Basically, it
controls the instance creation of the bean, and it is managed by the Spring container.
 The Spring framework provides five scopes for a bean. We can use three of them only in the context
of a web-aware Spring ApplicationContext, and the rest of the two are available for both an IoC
container and a Spring-MVC container. The following are the different scopes provided for a bean:

The following are the different scopes provided for a bean:


 Singleton: Only one instance will be created for a single bean definition per Spring IoC
container, and the same object will be shared for each request made for that bean.
 Prototype: A new instance will be created for a single bean definition every time a request is
made for that bean.
 Request: A new instance will be created for a single bean definition every time an HTTP
request is made for that bean. But only valid in the context of a web-aware Spring
ApplicationContext.

Computer Science and Engineering 29


Pathway Course--Full Stack Development 2025-26

 Session: Scopes a single bean definition to the lifecycle of an HTTP Session. But only valid
in the context of a web-aware Spring ApplicationContext.
 Global-Session: Scopes a single bean definition to the lifecycle of a global HTTP Session. It
is also only valid in the context of a web-aware Spring ApplicationContext

Example:Singleton
//[Link]
public class Student {
private String name;

public String getName() {


return name;
}

public void setName(String name) {


[Link] = name;
}

}
//[Link]
import [Link];
import [Link];

public class TestApp {

public static void main(String[] args) {


ApplicationContext context = new ClassPathXmlApplicationContext("[Link]");
Student s = (Student) [Link]("s");
[Link]("Student name is " +[Link]());

Student s1 = (Student) [Link]("s");


[Link]("Student name is "+[Link]());

Computer Science and Engineering 30


Pathway Course--Full Stack Development 2025-26

[Link]("s1 and s2 are both referring to the same object: " +(s == s1));

[Link]("Address of s1" +s);


[Link]("Address of s2" +s1);

}
//[Link]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
<bean id ="s" class="Student" scope="singleton" >
<property name="name" value="Satish Patil"></property>
</bean>

</beans>

Output:
Student name is Satish Patil
Student name is Satish Patil
s1 and s2 are both referring to the same object: true
Address of s1Student@318ba8c8
Address of s2Student@318ba8c8

Example:Prototype
//[Link]
public class Student {
private String name;

public String getName() {


return name;

Computer Science and Engineering 31


Pathway Course--Full Stack Development 2025-26

public void setName(String name) {


[Link] = name;
}

}
//[Link]
import [Link];
import [Link];

public class TestApp {

public static void main(String[] args) {


ApplicationContext context = new ClassPathXmlApplicationContext("[Link]");
Student s = (Student) [Link]("s");
[Link]("Student name is " +[Link]());

Student s1 = (Student) [Link]("s");


[Link]("Student name is "+[Link]());

[Link]("s1 and s2 are both referring to the same object :" +(s == s1));

[Link]("Address of s1" +s);


[Link]("Address of s2" +s1);

}
//[Link]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]

Computer Science and Engineering 32


Pathway Course--Full Stack Development 2025-26

[Link]
<bean id ="s" class="Student" scope="prototype" >
<property name="name" value="Satish Patil"></property>
</bean>

</beans>

Output:
Student name is Satish Patil
Student name is Satish Patil
s1 and s2 are both referring to the same object: false
Address of s1Student@33afa13b
Address of s2Student@7a4ccb53

Computer Science and Engineering 33

You might also like