0% found this document useful (0 votes)
7 views11 pages

What Is Spring Framework-2

The Spring Framework is a lightweight, loosely coupled framework for developing enterprise applications in Java, emphasizing dependency injection and inversion of control for loose coupling. It includes various modules such as Spring Core Container, AOP, Data Access, and Web, and supports testing with JUnit and TestNG. Key features include autowiring, different bean scopes, and the ability to create web applications and RESTful services.

Uploaded by

Karthik Fazer
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)
7 views11 pages

What Is Spring Framework-2

The Spring Framework is a lightweight, loosely coupled framework for developing enterprise applications in Java, emphasizing dependency injection and inversion of control for loose coupling. It includes various modules such as Spring Core Container, AOP, Data Access, and Web, and supports testing with JUnit and TestNG. Key features include autowiring, different bean scopes, and the ability to create web applications and RESTful services.

Uploaded by

Karthik Fazer
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/ 11

What is Spring Framework?

It is a lightweight, loosely coupled and integrated framework for developing enterprise


applications in java.

Advantages or Why we should go Spring?


Predefined Templates (Ex. Spring JDBC, JMS,..)
Loose coupling( by using Dependency Injection or Inversion of Control to write
components that are independent of each other)

Spring MVC framework can be used to create web applications as well as restful web
services capable of returning XML as well as JSON response
Light weight (You no need to use full Spring and can use part of spring. Ex. you can
use Spring JDBC without Spring MVC)

Modules ?

Test
This layer provides support of testing with JUnit and TestNG.

Spring Core Container

The Spring Core container contains core, beans, context and expression language (EL)
modules.

Core and Beans (These modules provide IOC and Dependency Injection
features.)

Context (This module supports internationalization (I18N), EJB, JMS, Basic


Remoting)

SpEL (SpEL is an exression language supporting the features of querying and


manipulating an object at runtime)

Ex.
Expression exp = parser.parseExpression("new String('hello world').toUpperCase
()");
String message = exp.getValue(String.class);
System.out.println(message);

AOP, Aspects and Instrumentation

These modules support aspect oriented programming implementation where you can
use Advices, Pointcuts etc. to decouple the code.

The aspects module provides support to integration with AspectJ.

The instrumentation module provides support to class instrumentation and classloader


implementations.

Data Access / Integration

This group comprises of JDBC, ORM, OXM, JMS and Transaction modules. These
modules basically provide support to interact with the database.

Web

These modules provide support to create web application.

What is IOC and DI?


IOC (Inversion of Control) and DI (Dependency Injection) is a design pattern to provide
loose coupling. It removes the dependency from the program.
Let's write a code without following IOC and DI.
public class Employee{
Address address;
Employee(){
address=new Address();//creating instance
}
}
Now, there is dependency between Employee and Address because Employee is
forced to use the same address instance.
Let's write the IOC or DI code.
public class Employee{
Address address;
Employee(Address address){ //Constructor Injection
this.address=address;//not creating instance
}
//or
public void setAddress(Address address){ //Setter Injection
this.address=address;//not creating instance
}
}
Now, there is no dependency between Employee and Address because Employee is
not forced to use the same address instance. It can use any address instance.
IOC Container
The Spring IoC container is at the core of the Spring Framework. The container will
create the objects, wire them together, configure them, and manage their complete life
cycle from creation till destruction. The Spring container uses dependency injection (DI)
to manage the components that make up an application.

Spring provides following two types of containers.

1. BeanFactory container
2. ApplicationContext container
3. ApplicationContext is the preferred way of using spring because of the
functionality provided by it and the interviewer wanted to check whether you are
familiar with it or not.

ApplicationContext. BeanFactory

Here we can have more than In this only one config file or .xml file
one config files possible
Application contexts can Don't support.
publish events to beans that
are registered as listeners
Support internationalization It’s not
(I18N) messages
Support application life-cycle Doesn’t support.
events, and validation.
Supports many Doesn’t support.
enterprise services such as
JNDI access, EJB integration,
remoting

No. Constructor Injection Set


ter
Inj
ect
ion

1) No Partial Injection P
ar
ti
al
In
je
ct
io
n

2) Doesn't override the setter property O


v
er
ri
d
e
s
th
e
c
o
n
st
ru
ct
or
pr
o
p
er
ty
if
b
ot
h
ar
e
d
ef
in
e
d.

3) Creates new instance if any modification occurs D


o
e
s
n'
t
cr
e
at
e
n
e
w
in
st
a
n
c
e
if
y
o
u
c
h
a
n
g
e
th
e
pr
o
p
er
ty
v
al
u
e

4) Better for too many properties B


et
te
r
fo
r
fe
w
pr
o
p
er
ti
e
s.

difference between constructor injection and setter injection?

Autowiring :

Autowiring enables the programmer to inject the bean automatically. We don't need to
write explicit injection logic.
The autowiring modes are given below:

No. Mode Description

1) no this is the default mode for XML based, it means autowiring is


not enabled.

2) byName injects the bean based on the property name. It uses setter
method.

3) byType this is the default mode for Annotation based, injects the bean
based on the property type. It uses setter method.

4) constructo It injects the bean using constructor


r

In Spring, you can use @Autowired annotation to auto-wire bean on


the setter method, constructor, or a field

@Autowired on Properties(equivalent to ByType)


In the below example, when the annotation is directly used on properties, Spring looks
for and injects Department when Employee is created. This is how it eliminates the
need for getters and setters.

@Autowired
private Department department;

@Autowired on Setters (equivalent to ByType)

In the below example, when the annotation is used on the setter method, the setter
method is called with the instance of Department when Employee is created.

private Department department;

public Department getDepartment() {


return department;
}

@Autowired
public void setDepartment(Department department) {
this.department = department;
}
@Autowired on Constructors(equivalent to constructor)

In the below example, the annotation is used on a constructor, an instance


of Department is injected as an argument to the constructor when Employee is created.

private Department department;


@Autowired
public EmployeeBean(DepartmentBean deptBean) {
System.out.println("*** Autowiring by using @Autowire annotation on constructor
***");
this.deptBean = deptBean;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}

Dependency checking

By default, the @Autowired will perform the dependency checking to make sure the
property has been wired properly. When Spring can’t find a matching bean to wire, it will
throw an exception. To fix it, you can disable this checking feature by setting the
“required” attribute of @Autowired to false.

package com.mkyong.common;

import org.springframework.beans.factory.annotation.Autowired;

public class Customer


{
@Autowired(required=false)
private Person person;
private int type;
private String action;
//getter and setter methods
}
Copy
In the above example, if the Spring can’t find a matching bean, it will leave the person
property unset.
@Qualifier
The @Qualifier annotation is used to resolve the autowiring conflict, when there are
multiple beans of same type.

Vehicle is a Interface and it has Start() and stop() method declarations.


CAR and BIKE both class implements Vehicle interface and has different
implementations of start() and stop() methods.
public class VehicleService {
@Autowired private Vehicle vehicle;
public void service() {
vehicle.start(); vehicle.stop();
}
}

As you can see in the above exception, there are two candidates (Car and Bike)
for Vehicle annotated with @Autowire in the VehicleService bean. The Spring IoC
container unable to determine which bean should be injected and throws an
exception NoUniqueBeanDefinitionException.
To fix the above problem, we will use the @Qualifier annotation with class annotated
with @Component and field annotated with @Autowired.
Modify the Bike, Car and VehicleService beans as follow.
@Component

@Qualifier("carBean")

public class Car implements Vehicle {}

Bike.java
@Component

@Qualifier("bikeBean")

public class Bike implements Vehicle{}

VehicleService.java
@Component

public class VehicleService {

@Autowired

@Qualifier("carBean")

private Vehicle vehicle; }

working fine now.

What are the different bean scopes in spring?

There are 5 bean scopes in spring framework.

No. Scope Description

1) singleton The bean instance will be only once and same instance will be
returned by the IOC container. It is the default scope.

2) prototype The bean instance will be created each time when requested.

3) request The bean instance will be created per HTTP request.

4) session The bean instance will be created per HTTP session.

5) globalsessio The bean instance will be created per HTTP global session. It can
n be used in portlet context only.

@Service
@Scope(value = "prototype")
public class CustomerService
{
}

In which scenario, you will use singleton and prototype scope?

Singleton scope should be used with stateless session bean and if thread safety not
required and prototype scope with stateful session bean and if thread safety
required.

You might also like