What Is Spring Framework-2
What Is Spring Framework-2
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.
The Spring Core container contains core, beans, context and expression language (EL)
modules.
Core and Beans (These modules provide IOC and Dependency Injection
features.)
Ex.
Expression exp = parser.parseExpression("new String('hello world').toUpperCase
()");
String message = exp.getValue(String.class);
System.out.println(message);
These modules support aspect oriented programming implementation where you can
use Advices, Pointcuts etc. to decouple the code.
This group comprises of JDBC, ORM, OXM, JMS and Transaction modules. These
modules basically provide support to interact with the database.
Web
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
1) No Partial Injection P
ar
ti
al
In
je
ct
io
n
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:
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.
@Autowired
private Department department;
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.
@Autowired
public void setDepartment(Department department) {
this.department = department;
}
@Autowired on Constructors(equivalent to constructor)
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;
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")
Bike.java
@Component
@Qualifier("bikeBean")
VehicleService.java
@Component
@Autowired
@Qualifier("carBean")
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.
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
{
}
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.