Spring Flashcards
What is Spring framework?
Spring is development framework for Java programming. It is an
open source development framework for Enterprise Java.
The core features of Spring Framework can be used in developing a
Java Enterprise application.
It has many extensions and jars for developing web applications on
top of Java EE platform.
With Spring we can develop large-scale complex Java applications
very easily. It is also based on good design patterns like
Dependency Injection, Aspect oriented programming for developing
extensible feature rich software.
What are the benefits of Spring
framework in software development?
Lightweight Framework: Basic Spring framework is very small in
size. It is easy to use and does not add a lot of overhead on
software. It just has 2 MB in basic version.
Container: Spring framework provides the basic container that
creates and manages the life cycle of application objects like Plain
old Java objects (POJO). It also stores the configuration files of
application objects to be created.
Dependency Injection (DI): Spring provided loose coupling is
application by Dependency Injection. It uses Inversion of Control
technique by which objects specify their dependencies to Spring
container instead of creating new objects themselves.
Aspect Oriented Programming (AOP): Spring framework promotes
and provides support for Aspect oriented programming in Java.
This helps in separating application business logic from system
services that are common across all the business logic. E.g. Logging
can be a cross cutting concern in an Application.
Transaction Management: Spring provides a framework for
transaction management. So a developer does not have to implement
it from scratch. Spring Transaction Management is so powerful that
we can scale it from one local transaction to global transactions in a
cluster.
MVC Framework: For Web applications, Spring provides MVC
framework. This framework is based on MVC design pattern and
has better features compared to other web frameworks.
Exception Handling: Spring also gives support for a common API to
handle exceptions in various technologies like- Hibernate, JDBC
etc
What are the modules in Core
Container of Spring framework?
Core module
Bean module
Context module
Spring Expression Language module
What are the modules in Data
Access/Integration layer of Spring
framework?
JDBC module: An abstraction layer to remove tedious JDBC
coding.
ORM module Integration layers for Object Relational Mapping
OXM module: An abstraction layer to support Object XML
mapping.
Java Messaging Service (JMS) module: Module for producing and
consuming messages.
Transactions module: Transaction Management for POJO classes
What are the modules in Web
layer of Spring framework?
Web module: This provides basic web-oriented integration features.
Servlet module: Support for Servlet Listeners.
WebSocket module: Support for Web Socket style messaging.
Portlet module: MVC implementation for Portlet environmentWeb module: This provides basic web-oriented integration features.
Servlet module: Support for Servlet Listeners.
WebSocket module: Support for Web Socket style messaging.
Portlet module: MVC implementation for Portlet environment
What is the main use of Core
Container module in Spring
framework?
As the name suggests, Spring Core Container is the core of Spring
framework. It gives the basic functionality of the Spring. All the
parts of Spring Framework are built on top of Core Container.
Its main use is to provide Dependency Injection (DI) and Inversion
of control (IOC) features.
What kind of testing can be done
in Spring Test Module?
Spring Test Module provides support for Unit testing as well as
Integration testing of Spring components. It allows using JUnit or
TestNG testing frameworks. It also gives ability to mock objects to
use the test code
What is the use of BeanFactory in
Spring framework?
BeanFactory is the main class that helps in implementing Inversion
of Control pattern in Spring. It is based on the factory design
pattern. It separates the configuration and dependencies of an
application from the rest of application code.
Implementations of BeanFactory like XmlBeanFactory class are
used by applications built with Spring
Which is the most popular
implementation of BeanFactory in
Spring?
XMLBeanFactory is the most popular implementation of
BeanFactory in Spring.
What is XMLBeanFactory in Spring framework?
XMLBeanFactory is one of the most useful implementation of
BeanFactory in Spring. This factory loads its beans based on the
definitions mentioned in an XMLfile.
Spring container reads bean configuration metadata from an XML
file and creates a fully configured application with the help of
XMLBeanFactory class.
What are the uses of AOP module
in Spring framework?
AOP module is also known as Aspect Oriented Programming
module. Its uses are:
Development of aspects in a Spring based application
Provides interoperability between Spring and other AOP
frameworks
Supports metadata programming to Spring
What are the benefits of JDBC
abstraction layer module in Spring
framework?
Spring provides JDBC abstraction layer module. Main benefits of
this module are:
Helps in keeping the database code clean and simple.
Prevents problems that result from a failure to close database
resources.
Provides a layer of useful exceptions on top of the error messages
given by different database servers.
Based on Spring’s AOP module
Provides transaction management services for objects in a Spring
application
How does Spring support Object Relational Mapping (ORM) integration?
Spring supports Object Relational Mapping (ORM) by providing
ORM Module. This module helps in integrating with popular ORM
framework like Hibernate, JDO, and iBATIS SQLMaps etc.
Transaction Management module of Spring framework supports all
of these ORM frameworks as well as JDBC.
How does Web module work in Spring framework?
Spring provides support for developing web application by using
Web module. This module is built on application context module
that provides context for web-based applications.
This module also supports web-oriented integration features liketransparently handling multipart requests for uploading files,
programmatically binding request parameters to business objects
etc.
This module also supports integration with popular web
frameworks like Jakarta Struts, JSF, and Tapestry etc.
What are the main uses of Spring
MVC module?
Spring-webmvc module is also known as Web-servlet module. It is
based on Web Model View Controller pattern.
Main uses of this module are:
Integration of Spring with other MVC frameworks
Supports IoC to provide clean separation of controller logic from
business objects
Provides clean separation between domain model code and web
forms
Allows developers to declaratively bind request parameters to
business objects
What is the purpose of Spring
configuration file?
Spring application can be configured by an XML file. This file
contains information of classes and how these classes are
configured and introduced to each other.
Spring IoC container uses some kind of configuration metadata. This
configuration metadata represents how an application developer
tells the Spring container to instantiate, configure, and assemble the
objects in your application. This configuration metadata is stored in
Spring configuration file.
The other ways of specifying configuration metadata are Java based
configuration and Annotation based configuration.
What is the purpose of Spring IoC container?
The Spring IoC Container is responsible for:
Creating the objects
Configuring the objects
Managing dependency between objects (with dependency injection
(DI))
Wiring the objects together
Managing complete lifecycle of objects
What is the main benefit of
Inversion of Control (IOC) principle?
Inversion of Control (IOC) principle is the base of Spring
framework. It supports dependency injection in an application. With
Dependency Injection, a programmer has to write minimal code. It
also makes easier to test an application.
Most important benefit is that it leads to loose coupling within
objects. With loose coupling it is easier to change the application
with new requirements.
Does IOC containers support Eager Instantiation or Lazy loading of beans?
IOC Container in Spring supports both the approaches. Eager
instantiation as well as lazy loading of beans.
What are the benefits of ApplicationContext in Spring?
ApplicationContext in Spring provides following benefits:
Bean factory methods: These are used to access application
components
Load File Resources: It helps in loading file resources in a generic
fashion
Publish Events: It enables publishing events to registered listeners
Internationalization Support: Ability to resolve messages to support
internationalization
Parent Context: Ability to inherit from a parent context
How will you implement ApplicationContext in Spring framework?
ApplicationContext in Spring can be implemented in one of the
following three ways:
FileSystemXmlApplicationContext: If we want to load the
definitions of beans from an XML file then
FileSystemXmlApplicationContext is used. The full path of XML
bean configuration file is provided to the constructor.
ClassPathXmlApplicationContext: To loads the definitions of beans
from an XML file in the CLASSPATH, we use
ClassPathXmlApplicationContext. It is used for application context
embedded in jars.
WebXmlApplicationContext: To provide configuration for a web
application WebXmlApplicationContext is used. While the
application is running, it is read only. But it can be reloaded if
underlying application supports it.
What are the main components of
a typical Spring based application?
In a Spring based application, main components are:
Spring configuration XML file: This is used to configure Spring
application
API Interfaces: Definition of API interfaces for functions provided
by application
Implementation: Application code with implementation of APIs
Aspects: Spring Aspects implemented by application
Client: Application at client side that is used for accessing functions
Explain Dependency Injection
(DI) concept in Spring framework?
Dependency Injection is a software design pattern. It is used to
implement Inversion of Control (IOC) in Spring framework. As per
this pattern, we do not create objects in an application by calling
new. Rather, we describe how an object should be created. In this
way creation of an object is not tightly coupled with another object.
A container is responsible for creating and wiring the objects. The
container can call injecting code and wire the objects as per the
configuration at runtime.
What are the different roles in Dependency Injection (DI)?
There are four roles in Dependency Injection:
Service object(s) to be used
Client object that depends on the service
Interface that defines how client uses services
Injector responsible for constructing services and injecting them
into client
What are the types of Dependency Injection?
Spring framework provides two types of Dependency Injection mechanism: Constructor-based Dependency Injection: Spring container can invoke a class constructor with a number of arguments. This represents a dependency on other class. Setter-based Dependency Injection: Spring container can call setter method on a bean after creating it with a no-argument constructor or no-argument static factory method to instantiate another bean.
What are the disadvantages of Dependency Injection (DI)?
Dependency Injection (DI) pattern has following disadvantages:
Most of the time Dependency Injection forces developers to use an
injection framework like Spring. This causes dependency on a
framework.
With Dependency Injection, clients are dependent on the
configuration data. This becomes extra task for developers when the
application does not need so many custom configuration values.
Code is difficult to trace and read in Dependency Injection. DI
separates behavior from construction of objects.
Dependency injection increases complexity in the linkages between
classes. It may become harder to manage such complexity outside
the implementation of a class.
What are the disadvantages of Dependency Injection (DI)?
Dependency Injection (DI) pattern has following disadvantages:
Most of the time Dependency Injection forces developers to use an
injection framework like Spring. This causes dependency on a
framework.
With Dependency Injection, clients are dependent on the
configuration data. This becomes extra task for developers when the
application does not need so many custom configuration values.
Code is difficult to trace and read in Dependency Injection. DI
separates behavior from construction of objects.
Dependency injection increases complexity in the linkages between
classes. It may become harder to manage such complexity outside
the implementation of a class.
What is a Spring Bean?
A Spring Bean is a plain old Java object (POJO) that is created and
managed by a Spring container.
There can be more than one bean in a Spring application. But all
these Beans are instantiated and assembled by Spring container.
Developer provides configuration metadata to Spring container for
creating and managing the lifecycle of Spring Bean.
In general a Spring Bean is singleton. Evert bean has an attribute
named “singleton”. If its value is true then bean is a singleton. If its
value is false then bean is a prototype bean.
By default the value of this attribute is true. Therefore, by default all
the beans in spring framework are singleton in nature.
What does the definition of a
Spring Bean contain?
A Spring Bean definition contains configuration metadata for bean.
This configuration metadata is used by Spring container to:
Create the bean
Manage its lifecycle
Resolve its dependencies
What are the different ways to
provide configuration metadata to a
Spring Container?
XML based configuration: We can specify configuration data in an
XMLfile.
Annotation-based configuration: We can use Annotations to specify
configuration. This was introduced in Spring 2.5.
Java-based configuration: This is introduced from Spring 3.0. We
can embed annotations like @Bean, @Import, @Configuration in
Java code to specify configuration metadata.
What are the different scopes of a Bean supported by Spring?
Spring framework support seven types of scopes for a Bean. Out of these only five scopes are available for a web-aware
ApplicationContext application:
singleton: This is the default scope of a bean. Under this scope,there is a single object instance of bean per Spring IoC container.
prototype: Under this scope a single bean definition can have multiple object instances.
request: In this scope, a single bean definition remains tied to the
lifecycle of a single HTTP request. Each HTTP request will have its own instance of a bean for a single bean definition. It is only
valid in the context of a web-aware Spring Application Context.
session: Under this scope, a single bean definition is tied to the lifecycle of an HTTP Session. Each HTTP Session will have one instance of bean. It is also valid in the context of a web-aware
Spring ApplicationContext.
globalSession: This scope, ties a single bean definition to the lifecycle of a global HTTP Session. It is generally valid in a Portlet context. It is also valid in the context of a web-aware Spring Application Context.
application: This scope, limits a single bean definition to the lifecycle of a ServletContext. It is also valid in the context of a
web-aware Spring ApplicationContext.
websocket: In this scope, a single bean definition is tied to the lifecycle of a WebSocket. It is also valid in the context of a webaware Spring ApplicationCont
How will you define the scope
of a bean in Spring?
In configuration xml, we can specify the scope of bean in its
definition. This is used by container to decide the scope of bean in
Spring.
E.g.
This is an example of userService bean with prototype scope.