0% found this document useful (0 votes)
25 views

Spring 1_Introduction and IOC

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

Spring 1_Introduction and IOC

Ioc spring
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Spring frameworks-

Framework-

It is nothing but to provide the common things which is required for software
development called as framework.

Spring was developed by Red Johnson in 2003.

Advantages of spring frameworks-

1. Predefined template-
It provides templates for hibernate, JPA, JDBC, etc. there is no need
to write too much code.
Example- in JDBC template, you don’t need to write code for
exception handling, creating connection, creating statement, closing
connection. Only the thing is that you need to write the code for executing
the query only.

2. Loosing coupling-
Spring applications are loosely coupled because of dependency
injection.

3. Easy to test-

Spring does not require server to run the program. It only need JDK
and Jar file.

4. Light weight-
Spring is light weight component due to its POJO implementation. It
does not force any programmer to inherit the class or implement any
interface.

5. Development is very fast.

CORE JAVA- SPRING INTRODUCTION & IOC BY JEEVAN SIR 1


Difference between tightly dependency and loosely dependency.

Tightly coupled dependency-


Suppose I have two classes A & B, I have created the object of B
class into A class as B b=new B () called as tightly coupled dependency.

Example-

package com.test

public class A {

B b = new B ();
}

public class B{

Loosely coupled dependency-


Suppose I have two classes A & B which will implements interface
test.

package com.test

public class A extends B {

Demo i= new B ();


}

CORE JAVA- SPRING INTRODUCTION & IOC BY JEEVAN SIR 2


package com.test

public class B implements Demo {

package com.test

public interface Demo {

Here class A is compatible with any class. In future, if you don’t want B
class then you can create the one more class named as class C and pass the object
as below. Demo i= new C (); // i is the interface reference.

So in this way, we can achieve loosely coupled dependency.

Spring IOC or Spring Core

It is a base module for all the spring framework.

What is Inversion of Control?

It stands for Inversion of Control. IOC container is responsible to instantiate,


configure and assemble this object.

Why we use IOC?

Let’s take example here, Suppose I have two classes A & B.

Class A {

Class B {

CORE JAVA- SPRING INTRODUCTION & IOC BY JEEVAN SIR 3


Now I want to create the object of class B into class A then we will write it as

B b=new B ();

Again, In future, I want to create the object of Class C then what will happen here?

I will write the code as below

C c=new C ();

It means every time I need to do some changes in java code,

To overcome the above issue, we should go with Inversion of Control (IOC)

What IOC does in the above scenario?

So here we are giving control to spring that you have to create the object on the
behalf of me. Means no need to worry about object creation.

Why container?

If you want to pass / apply any input to pojo classes. You must have container
supports.

CORE JAVA- SPRING INTRODUCTION & IOC BY JEEVAN SIR 4


What does IOC container?

1. Read the XML file.


2. Create the instantiation or object of xml bean (java classes)
3. It will manage the life cycle of your bean classes.
4. Passing the dynamic parameter to bean (java classes).

Example of Bean Class-

//This is spring bean class


public class Employee {

private String firstName;


private String lastName;
private String emailId;
private String city;

public String getFirstName() {


return firstName;
}

public void setFirstName(String firstName) {


this.firstName = firstName;
}

public String getLastName() {


return lastName;
}

public void setLastName(String lastName) {


this.lastName = lastName;
}

public String getEmailId() {


return emailId;
}

public void setEmailId(String emailId) {


this.emailId = emailId;
CORE JAVA- SPRING INTRODUCTION & IOC BY JEEVAN SIR 5
}

public String getCity() {


return city;
}

public void setCity(String city) {


this.city = city;
}

There are two types of IOC container as

BeanFactory-

 It is called as core container.


 By using this, we can develop the standalone application.
 Standalone application means the application which we need to install on
every machine (computer or laptop) called as standalone application.

 Examples are examples of standalone application

1. Calculator

CORE JAVA- SPRING INTRODUCTION & IOC BY JEEVAN SIR 6


2. VLC Media Player

3. Antivirus

 XMLBeanFactory is the implementation class for BeanFactory(I).


 To use BeanFactory, we need to create the instance of XmlBeanFactory class.

Resource resource= new classPathResource(“applicationContext.xml”);


BeanFactory factory= new XmlBeanFactory(resource);

The constructor of XmlBeanFactory class receives resource object so we need


to pass resource object to create the object of BeanFactory.

Note- BeanFactory contain collection of beans

CORE JAVA- SPRING INTRODUCTION & IOC BY JEEVAN SIR 7


ApplicationContext-

 It is called as J2EE container.


 By using this, we can develop web application.
 Web application mean the application which we need to access through web
browser called as web application
 Below are examples of ApplicationContext
1. espncricinfo.com

2. onlinesbi

 ClassPathXmlApplicationContext is implementation class for


ApplicationContext(I).
 To use applicationcontext, we need to create the instance of
ClassPathXmlApplicationContext as given below-

ApplicationContext context= new


ClassPathXmlApplicationContext(“applicationcontext.xml”);

The constructor of ClassPathXmlApplicationContext class receives string


object so we can pass name of xml file to create the object of
ApplicationContext.

CORE JAVA- SPRING INTRODUCTION & IOC BY JEEVAN SIR 8


Internal Working of BeanFactory

Fig- BeanFactory

1. At a time of user request, it will create object of bean class by using


getBean() method.
2. It is called as lazy because it creates object when user request it.
3. Because default scope of bean is singleton.

Note- BeanFactory does not create object at loading time.

Internal working of ApplicationContext

1. At time of class loading. It will create object of bean class.


2. It is called as eager because it creates object before sending request.
3. Suppose you have 10 declarations of bean then it will create the object of,
if and only if scope is singleton.
4. If scope is prototype, at loading time, it will not create the instance.

Note-

1. In case of BeanFactory and ApplicationContext, if scope is prototype then it


will create the object per user request.
2. If constructor is private or public then spring will create the instances in
both cases(By using reflection only).

CORE JAVA- SPRING INTRODUCTION & IOC BY JEEVAN SIR 9

You might also like