Spring Core 1
Spring Core 1
Spring Framework is a most powerful, flexible and easy to use, to build the java based Enterprise
web applications.
Building Enterprise Webapplications using more powerful, flexible, easy to use Framework called
spring.
we are devloping
Framework:
Framework is said to be a semi-finished application. that can be used as per our requirement and
can also be customizable.
For Example:
-->
Login
User
Role
Persmissions:
-- id, perissionName
Persmissions-->Role-->User
===========================================
Session Management:
12-C:
mannulayy
postoffice
bangolere
==============================
EPF app:
every day:
===========================
Use Case:
User:
--> password
====================================================
User:
--> userId
securtity service
transaction services
=======================================
struts+spring integration
IRCTc(jsf):
Spring Core
struts/jsf
spring Framework?
since 2000.
application server
EAR: war+jar
fname: john
50%--50%
in memory database: h2
80%,20%
Handed in
Spring Core
91
Assigned
Inversion Of Control
Inversion Of Control is a software design Principle, which is an independent of any language and
which will describes about the how the objects being created meaning the way how the Objects are
creating is explain by the IOC Design Principle.
Note:
IOC never Creates the Objects because its a just design principle. The implementations of the this
Design Principle I.e. IOC will creates the Objects with the help of the IOC Container.
Design Principle
Design Principles are the guidelines/suggesions with the best practices to achive the good Object
Oriented Designing of an application.
As IOC is a Design Principle, examining the principle of inverting the Objects and its dependencies to
the framework rather the programmer will manage it. That means instead of the programmer
controlling the flow of program like Customer when need HomeLoanAccount/PersonlLoanAccoiunt
Objects, then being programmer, we have instantiated the Home/PersoanlLoan Account Objects
while the Customer Object is being creating.
So now, if we use the IOC Implementation, rather the programmer managing this program flow, the
IOC Implementation will manages the flow of creating the Customer, HomeLoan and PersonalLoan
account Objects and managing their dependencies.
IOC implementation here is the Dependency Injection(DI) which is a design pattern and which will
manages the Object dependencies from the IOC Container by letting the container know even to
create the Objects inside the IOC Container if the objects were not being created when the DI needs
to manage them.
Spring Core
IOC Container
To instantiate the classes defined inside the application if they will be the suitable candidates.
To Configure the Object like its Scope and its Life Cycle Management
The Spring Container uses the DI concept to manage the Objects dependencies inside an application
Dependency Injection(DI)
Dependency Injection is a Design Pattern through which the Inversion of Control is achieved.
Inversion of Control meaning Inverting the Process of the Objects creation to the Framework rather
being managing the by the Programmer/program code.
Through DI, the responsibility of Creating the Objects is shifted from the application program to the
spring framework provided IOC Container. Meaning externalizing the Object Creation Process from
the Application Program to reduce the coupling between the multiple Objects and to achieve the
Spring Core
loosely coupling by dynamically providing the required Object to the application Program from an
external entity.
Here IOC Container is an external entity using for the objects creation by the DI pattern.
Interview Questions
4. How many type of the IOC Containers we have and what are the differences between them?
5. What are the Difference between the JavaBean and Spring Bean?
6. What are the differences between the Java EE and Spring Framework?
1. BeanFactory
2. AppliationContext
BeanFactory
BeanFactory is a central registry of application components and are called as IOC Container. Spring's
Dependency Injection functionality is implemented using this BeanFactory interface and its
subinterfaces.
Normally a BeanFactory will load bean definitions stored in a configuration source (such as an XML
document), and use the org.springframework.beans package to configure the beans.
Spring XML Beans Schema defines a simple and consistent way of creating a namespace of
JavaBeans objects, managed by a Spring BeanFactory, read by XmlBeanDefinitionReader
Spring Core
Each "bean" element in this xml document defines a JavaBean. Typically the bean class is specified,
along with JavaBean properties and/or constructor arguments.
Container Creation
In the case of the web applications, while the application is starting up , the IOC Container called
ApplicationContext will be created.
In the case of the stand-alone applications, we have to instantiate the BeanFactory thorugh its
implementation classes I.e. XMLBeanFactory.
XMLBeanFactory:
To Create the BeanFactory Container we have to instantiate the XMLBeanFactory as shown below:
Flow: BeanRegistration
Spring Core
Flow: BeanCreation
Date: 19-May-2022
When the Customer bean has the dependency on the Home Loan Account and at the same time
when the HomeLoan Account also has the dependency on the Customer, then we will go into the
circular reference exception when it is constructor injection.
That means, Assume Customer has the constructor argument as the HomeLoanAccount and when
customer bean is trying to create by the spring core container, first it tries to resolve the customer
dependency I.e. HomeLoanAccount and tries to created it if not already a bean of HomeLoanAccount
is not available.
At the same time, assume the HomeLoanAccount has also a constructor arguement I.e. Customer
and when an Spring Bean is trying to create for HomeLoanAccount first it tries to resolve the
Customer Object by creating if it is not already created.
So the Customer required the HomeLoanAccount which already was not created because it also has
the constructor afgunent dependency on customer, so this situation leads to Circular Reference.
Spring Core
This Problem Can be avoided by taking one of the bean creation I.e. either Customer or
HomeLoanAccount through the setter injection. Because Setter Injection will creates the Proxy
Objects.
Proxy Object is an extension of the original Object with the dummy implementation and with the
default constructor.
Example:
Customer.java
CustomerProxy.java
Autowiring
The process of wiring/injecting the Object dependencies automatically by the spring core container,
then it is called as autowiring. Autowiring will works only for dependencies in the form of Objects.
1. byName
2. byType
3. Constructor
4. Default
5. No
Spring Core
1. byName:
In autowire byName, the java bean the dependency propery name should be match with the bean id
or name of the spring core container as shown below:
If the java bean dependency proerty name is not matching with the spring core container bean id or
name then the dependency wont be injected/wired into the dependent bean.
2. byType
In autowire byType, the java bean, dependency propery type should be match with the bean class of
the spring core container as shown below:
Spring Core
3. Constructor
@Autowired Annotation
Annotation support was introduce I spring from 2.5 onwards. Using @Autowired attribute, we even
no needed to use the Autowiring attribute in an xml file, to wire the object dependencies.
@Autowired annotation can be used on field level, method Level and Constructor Level.
When @Autowired anotation is used on the field level or method level, it will use the autowired by
Type method and when it used on a constructor it will use the autowire by constructor method.
@Autowired annotation by default wont be enable when we are using the xml. To enable it we have
to use <context:annotation-config/> element in xml file as shown below. This element will register
the annotations available inside the spring core container.
Component Scanning
So far we have defined the bean definitions inside the xml file. Now with this component-scan no
need of even defining the bean definitions inside an xml file. Rather we can use the stereo type
annotations on the java classes and we can locate the stereo type annotated classes from the xml
file by configuring the <context:component-scan/> element as shown below:
Spring Core
In the above xml file we have given the base-package in which the spring core container look up for
the classes anotated with the sterio type annotations and detect them as a right candidate and load
them into the spring application context container to instantiate them as spring beans.
Here Candidate meaning, the class which is annotated with the @Component or its sub annotations
then we called it as a candidate.
Note:
Steriotype Annotations
The word Stereo type meaning fixed at a particular position. In Spring Core Container, the stereotype
annotations meaning the annotations whose position is fixed at the class level.
Application Context will detect and creates the stereo types classes as spring beans using auto
detecting mechanism happening through the component scan.
Component Scan will do a scanning of the packages that were defined using @ComponentScan
annotation or using the xml tag <context:component-scan/>, and looks for the following stereotype
annotations classes to load them in to the spring application context for which the application
context will create the spring beans.
1. @Component
3. @Service
4. @Repository
5. @Indexed
Spring Core
Interview Questions
<context:annotation-config/>
<context:component-scan/>
@Configuration annotation is an alternate to xml but not a replacement of an xml introduced since
the spring version 3.0
@Configuration annotated class Indicates that we can declares one or more @Bean methods which
may be processed by the Spring container to generate bean definitions.
In this way we can mix both the java annotations and xml approaches.
Interview Question
When we are using xml , can we use the java annotation configurations as well at a time? Can you
please explain the steps to configure it?
Bean Scopes
Bean Scope will define how and till where we can access the spring beans created inside the spring
core container.
1. Singleton
2. Prototype
Interview Question: what is the difference between the singleton scope in spring and the singleton
design pattern?
What is Pattern?
A Pattern is providing a solution for the real time problems which might occurs repeatedly.
When we use new operator to create an object, and when every time we called the method which is
creating an object using this new operator, it will creates a new object everytime in JVM.
}
Spring Core
and as we know sweeping out the un used objects from JVM is not guaranteed. As a result, heap out
of memory exception might come if we keep on creates the new objects every time and if we don’t
sweep them out from the memory if we unused.
Create only one instance of the class per JVM that means create only one object for the Customer
and use the same object every time. So that we can avoid the creating of the many customer objects
inside the JVM. And always as we will have only one Object Per JVM, we never get into heap out of
memory issues.
Prototype Scope
Prototype Beans
Interview Questions
1. What happens if the singleton scope bean will be injected to the prototype scope bean?
when a Singleton scope bean is injecting into the Prototype scope bean, then the Single scope bean
will be injected at only once into the Prototype beans, no matter how many times the prototype
beans are creating. Because singleton bean will create only once throughout the container.
2. What happens if the prototype scope bean will be injected into the singleton scope bean?
when a prototype scope bean is injecting into the singleton scope bean, then the prototype scope
bean will be injected at only once into the singleton bean while singleton bean creation. Because
singleton bean will create only once and during that time only one the prototype bean will be
injected.
Spring Core
Spring Core