Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Spring-Quick-Revision

Uploaded by

Suresh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Spring-Quick-Revision

Uploaded by

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

Spring Framework

-> Spring is a dependency injection framework is to make loosely coupled application.


-> We can develop standalone & enterprise application.
-> Spring framework makes the easy development of Java EE application.
-> It was developed by Rod Johnson in 2003.

Spring Advantages

Modular and lightweight:


Allowing developers to use only the components they need,
making applications more lightweight and easier to maintain.
This modularity also promotes better code organization
Flexible Configuration:
Including xml, java-based config., and annotation-based
config. This flexibility allows developers to choose the suitable
approach for their projects.
DI:
It simplifies the management of component dependencies,
making code more testable and adaptable to changes.
AOP:
Allowing developers to separate cross cutting concerns like
logging, security and transactions from the core application
logic. This improves code modularity and maintainability.
Simplified DB access:
Reduce boilerplate code, improves data efficiency
Security:
Provides a robust framework for implementing authentication
& authorization, making it easier to secure web applications
and services.

Spring Container:
Heart of spring i.e. core (Jase JVM java application ko handle karta, usi tarah se spring spring
applications ko handle karta)
Responsibility:

1. Manage bean objects

 A Java bean class is just a standard. Often used to encapsulate data and adhere to
java beans conventions for easy integration with frameworks. It’s a regular java class,
except it follows certain conventions:
a. All properties are private (use getters & setters)
b. A public no argument constructor
c. Implements serializable
The object created of this java bean class is known as Bean Object.

 POJO Class:
a. A simple java class with fields and getters/setters, used for data representation
without framework dependencies
2. Manage bean life-cycle
3. Dependency Injection
4. AOP
5. Transaction Management
6. I18N (Internalization) etc.

IOC Container
 The IoC container is responsible to instantiate, configure, and assemble the objects.
 The IoC container gets information from the XML file and works accordingly. The
main tasks performed by IoC containers are:
1. To instantiate the application class
2. To configure the object
3. To assemble the dependencies between the objects.
 There are two types of IoC containers. They are:
4. BeanFactory (It became old)
5. ApplicationContext (we use this now): - It’s an interface
6. Application context (Spring container) is an interface in spring which is used
to manage beans, handle application events, and access resources
7. Some implemented classes of Application context are:
a. ClassPathApplicationContext (Used for XML config.)
b. AnnotationConfigApplicationContext (Used for Java config)
Spring Container
Different bean Objects

Configuration Metadata
1. XML- file config
2. Java file-config
3. Annotation-based config

 Java File-Config:
a. @Configuration: indicates that the class is spring configuration class. It means
that annotated java class contains beans definitions and configuration setting for
the spring application context.
b. @Configuration allows us to define beans and their dependencies in a Java-based
way instead of using XML config. Files.
c. In xml-based spring config, we use <bean> tag to define beans whereas in java-
based config, we use methods annotated with @Bean to define beans
d. In xml config., we provide an “id” attribute to specify the bean name and specify
the class name using “class” attribute whereas in java config., the “method
name” becomes the default bean name, and the “return type” of the method
determines the class of the object that will be created as the bean.

 Annotation based config:


a. If we are doing this config then still, we need to create either xml or java-based
file.
b. @Component annotation (is also known as stereotype annotation) is used to
declare a class as a Spring bean, which is a managed component in the Spring
application context
c. It helps spring automatically detect and manage these beans during application
startup, making them available for dependency injection and other spring
features.
d. In a spring xml config file, <context: component-scan> is used to instruct the
spring framework to perform component scanning.
e. The @Value annotation in spring is used to inject values into Spring bean fields or
methods.
f. It scans specified packages in the application’s classpath for classes annotated
with annotations like @Component, @Service, @Repository & @Controller.
These discovered classes are then registered as Spring beans, enabling automatic
dependency injection and other spring features.
g. With java file we need to use @Configuration @ComponentScan

Lifecycle of Bean Object:


1. Loading bean definitions:
a. Spring loads bean definitions from various sources such as XML configuration
files, java-based config classes or component scanning
b. These bean definitions contain information about the bean class, dependencies
and other configuration details.
2. Bean Instantiation:
a. After bean definitions are loaded, the spring container creates instances of beans
based on these definitions.
b. This involves invoking the bean class constructor, either the default or a specified
constructor, to create an actual instance of the bean.
c. The newly created bean instance is now ready for configuration and initialization.
3. Bean initialization:
a. Once the bean is instantiated, spring proceeds to configure and initialize it.
b. Property values are set using setters, constructor arguments, or field injection,
populating the bean’s state.
4. Use bean object:
a. With the help of context (object of interface), we can access the object.
5. Bean destruction:
a. Bean object is destroyed or deleted.

Dependency Injection:
1. It is a design Pattern used in spring framework to achieve Inversion of Control (IoC).
2. Its main task is to “inject” the “dependency” means inject one object into another
object.
3. It’s used to achieve loose coupling.
We can achieve DI in two ways:
a.) Setter method DI b.) Constructor DI
We can achieve these DI Patterns with two ways:
a. XML File
b. Java File

1. Setter Method DI:


In xml file when we inject object into another, then we use “ref” attribute to
reference that bean “id”
In Java Bean/POJO we don’t create constructor, only create setter methods.

<bean class=” FULL_PATH” id=” #“>


<property name = “ “ value = “ “ />
<property name = “ “ value = “ “ />
</bean>

<bean class = “FULL_PATH “ id = “ “ >


<property name = “ “ ref = “ID_OF_INJECTED_OBJECT“ />
</bean>

2. Constructor DI:
In xml file we will not use property tags we will use <constructor-arg value =” #”/>
Here also we will use “ref” attribute if we need to inject the object of other class.
In CDI Java bean/POJO will not have setter methods , only have constructors.

<bean class=” FULL_PATH” id=” #“>


< constructor-arg value = “ “ />
<constructor-arg value = “ “ />
</bean>

<bean class = “FULL_PATH “id = “ “ >


<constructor-arg ref = “ID_OF_INJECTED_OBJECT“ />
</bean>

Setter Method DI: - Constructor DI: -

1. Dependencies are injected into a class through Dependencies are injected into a class through
setter methods. Constructor.

2. Setter method DI is more readable Constructor DI is less readable


3. Setter method DI is more flexible Constructor DI is less flexible

AUTOWIRING:
1. Feature of Spring Framework by which we can achieve DI automatically.
2. The @Autowired annotation in Spring is used for automatic dependency injection.
3. It tells spring to take the bean and plug it where we need it
4. It lets spring to handle object connections for us.
5. Autowiring can be achieved by: -
a. Annotation-based Autowiring – Used @Autowired & @Qualifier
b. XML-based Autowiring: - Used “autowire” attribute
6. @Qualifier annotation in spring helps pick the right bean among multiple beans of
the same type. It helps spring to know which bean you want to inject and resolves
ambiguity.

Disadvantage of Autowiring:
1. It can be achieved only on non-primitive or user-defined datatypes, not on primitive
data types.

You might also like