Spring-Quick-Revision
Spring-Quick-Revision
Spring Advantages
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:
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.
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
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.
1. Dependencies are injected into a class through Dependencies are injected into a class through
setter methods. Constructor.
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.