SPRING
06 April 2023 09:18
➢ the Spring Framework is an open-source Light weight framework for building enterprise Java application
➢ It was developed by Rod Johnson in 2003.
➢ Spring framework makes the easy development of JavaEE application.
➢ Spring is a Dependency Injection and Inversion of Control frame work
➢ With the help of spring we can develop loosely coupled application
HAS A RELATIONSHIP(ASSOCIATION)
• The processing of Associating one object with another object is called Association
• Association or has a relationship is categorized into two types
1. Composition(Tight coupling)
2. Aggrigation (loose coupling)
Composition(Tight coupling)
• If two object completely dependent on each other we refer it as tight coupling
Aggrigation (loose coupling)
If two object are not completely dependent on each other we refer it as loose coupling
Inversion of Control (IoC)
the process of transfering the controller of creating object and injecting object to the spring frame work
IOC makes the code loosely coupled
Dependency Injection
➢ Dependency Injection (DI) is a design pattern to achieve or implement inversion of controller
SPRING Page 1
Design pattern:
➢ A design pattern is a general repeatable solution to a commonly occurring problem in software design
eg : SingleTon Design Pattern,Factory Design pattern.
Spring container
➢ Spring Container is responsible for managing the lifecycle of an object(bean) Spring
Spring Container does the following task
➢ Create an object
➢ Stores the object in the memory
➢ Manages the object(injecting object to another object)
The Spring container is refered as
➢ Application Context
➢ Bean Factory which is predefined interface
Application Context:
The important implementation class of application Context
➢ ClassPathXmlApplicationContext("applicationContext.xml");
➢ fileSystemXmlApplicationContext
➢ WebApplicationContext
➢ AnnotationConfigApplicationContext
SPRING Page 2
Dependencies :
are nothing but ,they are data memebers;
Types of dependencies
Primitive ,non Primitive;
Collection
➢ LIST
➢ SET
➢ MAP
Referance type
Types of dependency injection
➢ Setter injection
➢ Constructor injection
➢ Interface
Configuration
the process of configuring the dependency
Types of configuration
➢ Xml based configuration
➢ Annotation based configuration
Xml configuration
<beans> </beans> -- > used to config xml file
<bean></bean> --> used to create object
Setter injection
It is a type of dependency injection where setter() method is used to
inject the dependencies
property tag is used to inject the dependencies in setter injection
➢ value as element
<bean id="ref" class="FQCN">
<property name="propertyName" >
<value></value>
</property>
</bean>
SPRING Page 3
</bean>
➢ value as attribute
<bean id="ref" class="FQCN">
<property name="propertyName" value="urOwnValue"></property>
</bean>
➢ value as p-schema -- > p:attribute="value"
Constructor injection
It is a type of dependency injection where constructor is used to inject
the dependencies
Constructor - arg is used to inject the dependencies in constructor
injection
➢ value as element
<bean id="ref" class="FQCN">
<constructor-arg name="propertyName" >
<value></value>
</constructor-arg>
</bean>"?>:,l;kmlj`
➢ value as attribute
<bean id="ref" class="FQCN">
<constructor-arg name="propertyName" value="urOwnValue">
</constructor-arg>
</bean>
➢ value is c-schema
c:attribute="value"Va
Annotation
@Component
it is class level annotation which is used to create object of the class
A class which is assigned with component annotation is called componenet
class
@value
it is data memebers level annotation which is used to inject primitive datatype
@autowired
it is data member level annotation which is used to identifie particular
dependency as a referance type of dependency and it is used to wire the object of
Implementation class
@qualifier
It is data member level annotation which is used to carry the object of patricular
Implementation class
Lifecycle of spring
SPRING Page 4
spring lifecycle is managed by spring container'
Spring container is the core of the spring framework
Spring container will get excuted when we run the program
Spring container will create instance per request
Spring will inject the dependencies
Init-method
it is used to intilize bean object
init () is called after injecting the dependencies by spring container
Destroy-method
it is used to destroy bean object
Destroy () is called before closing the jvm
SPRING Page 5