0% found this document useful (0 votes)
44 views18 pages

Spring Core 1

Uploaded by

Kavi Arasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views18 pages

Spring Core 1

Uploaded by

Kavi Arasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Spring Core

1. what is Spring Framework?

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

--> web application: view like jsp, html

--> Restful API for the Mobile application

--> Data streaming flow for a complex web application.

for all the above requirement, spring is providingsupporting tools.

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

-- fname, lname, email, mobile password.

Role

-- roleName, Id, description

Persmissions:

-- id, perissionName

Persmissions-->Role-->User

===========================================

Session Management:

we need to manage the logged in user session

for every request.

company-1: pf: 1lach


Spring Core

comany-2: pf: withdraw-->1lack: UAN

12-C:

mannulayy

postoffice

bangolere

==============================

EPF app:

UAN--> pF Details-->upload check leaf

mobile --> OTP--> pf application

every day:

Enterprise Web Application:

===========================

Use Case:

I have a hospital, every patient in my hospital can use

a mobile application/web application and register there.

User:

--> Fist Name

--> Last Name

--> Email: wrong email Id(@) [email protected]

--> password

-->Mobile: wrong mobile(10digits,only numbers)

user registration successfull, now you can Login here.

user will login there.


Spring Core

Patient is not recieving any email notifiations,

like report ready, follow ups.

====================================================

when the user registered,

send an email with the verification link,

changed password page:

sending OTP to his mobile number:

User:

--> Fist Name

--> Last Name

--> Email: wrong email Id

-->Mobile: wrong mobile

--> userId

Enterprise web application is also, a simple web application

with the Enterprise standards and needs to strengnthen

the user experience, app quality and security

simple mail service

simple messaging service

securtity service

transaction services

perfmance improvement techniques

=======================================

2010,2011, 2012, 2013(struts, jsf)

struts+spring integration

IRCTc(jsf):
Spring Core

struts/jsf

spring Framework?

spring introduced in the year 2002, by rod johnson

sping has been eveoved by making alot of changes to it self

according to the market trends and the developer needs.

By that time spring introduced in 2002,

there is technology called EJB(Enterprise Java Bean) in the market

since 2000.

application server

EAR: war+jar

now in the year 2022,

there is no application which are using the EJB

but there 90% of the applications using spring

fname: john

50%--50%

Spring Boot introduced with the auto features.

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.

Design Principle is like a theory whose implementation is the Design Pattern.

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

IOC Container is an object which will responsible for:

 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

 To Assemble the dependencies between the Objects using the DI concept.

In Spring Core Module we have two types of IOC Container:

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

1. What is the difference between the IOC, DI and IOC Container?

2. Are DI and IOC same?

3. How the DI will be implemented?

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?

Types Of Spring IOC Containers

In Spring Core Module, we have two types of Conatainers:

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:

XMLBeanFactory is an implementation class of BeanFactory Interface, that reads bean definitions


from an XML document using the BeanDefinitionReader.

To Create the BeanFactory Container we have to instantiate the XMLBeanFactory as shown below:

How BeanFactory will Creates the SpringBeans?

Flow: BeanRegistration
Spring Core

Flow: BeanCreation

Date: 19-May-2022

Topic: Circular Reference Exception

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.

What is Proxy Object?

Proxy Object is an extension of the original Object with the dummy implementation and with the
default constructor.

Example:

Customer.java

CustomerProxy.java

Hence in case of the Customer and HomeLoan Account,

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.

Autowiring of the dependencies can be done in the following ways:

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:

1. <context:annotaion-config/> and <context:component-scan/> were introduced in spring 2.5


onwards.

2. When we use <context:component-scan/> we no need to use <context:annotation-config/>


because this annotation registration is also supporting by the component scanning.

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

2. @Controller and @RestController

3. @Service

4. @Repository

5. @Indexed
Spring Core

Interview Questions

1. how we were eliminated the ref tag usage?

by introducing the autowire attribute in xml.

2. how we eliminated the autowire attribute in xml file?

by giving @Autowired annotation

3. how the @Autowired annotation will be enable?

<context:annotation-config/>

4. how we eliminated the defining the beans in a xml file?

<context:component-scan/>

5. how we eliminated the xmls?

using the annnotation @Configuration

Introduction to @Configuration Annotation

@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.

@Configuration classes are typically bootstrapped using either AnnotationConfigApplicationContext


class or its web-capable variant I.e. AnnotationConfigWebApplicationContext.
Spring Core

As an alternative to registering @Configuration classes directly against an


AnnotationConfigApplicationContext, the @Configuration classes may be declared as normal
<bean>}definitions within Spring XML files:

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.

In Spring Core lets see the below 2 bean scopes:

1. Singleton

2. Prototype

1. Singleton Bean Scope

Interview Question: what is the difference between the singleton scope in spring and the singleton
design pattern?

Singleton Design Pattern:

What is Pattern?

A Pattern is providing a solution for the real time problems which might occurs repeatedly.

Problem without Singleton:

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.

public Customer createCustomer(){

return new Customer()

}
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.

Solution: Singleton Design Pattern

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.

Lazy Vs Eager Loading

Solution: Circular Reference Error

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

You might also like