Spring Flashcards

1
Q

What is Spring framework?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the benefits of Spring

framework in software development?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the modules in Core

Container of Spring framework?

A

Core module
Bean module
Context module
Spring Expression Language module

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the modules in Data
Access/Integration layer of Spring
framework?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the modules in Web

layer of Spring framework?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the main use of Core
Container module in Spring
framework?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What kind of testing can be done

in Spring Test Module?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the use of BeanFactory in

Spring framework?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which is the most popular
implementation of BeanFactory in
Spring?

A

XMLBeanFactory is the most popular implementation of

BeanFactory in Spring.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is XMLBeanFactory in Spring framework?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the uses of AOP module

in Spring framework?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the benefits of JDBC
abstraction layer module in Spring
framework?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
How does Spring support Object
Relational Mapping (ORM)
integration?
A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
How does Web module work in
Spring framework?
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the main uses of Spring

MVC module?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the purpose of Spring

configuration file?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the purpose of Spring IoC container?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the main benefit of

Inversion of Control (IOC) principle?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Does IOC containers support Eager Instantiation or Lazy loading of beans?

A

IOC Container in Spring supports both the approaches. Eager

instantiation as well as lazy loading of beans.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What are the benefits of ApplicationContext in Spring?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How will you implement ApplicationContext in Spring framework?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What are the main components of

a typical Spring based application?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Explain Dependency Injection

(DI) concept in Spring framework?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What are the different roles in Dependency Injection (DI)?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What are the types of Dependency Injection?

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q
What are the disadvantages of
Dependency Injection (DI)?
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q
What are the disadvantages of
Dependency Injection (DI)?
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What is a Spring Bean?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What does the definition of a

Spring Bean contain?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What are the different ways to
provide configuration metadata to a
Spring Container?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What are the different scopes of a Bean supported by Spring?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

How will you define the scope

of a bean in Spring?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

Is it safe to assume that a
Singleton bean is thread safe in Spring
Framework?

A

No, Spring framework does not guarantee anything related to multithreaded behavior of a singleton bean. Developer is responsible for
dealing with concurrency issues and maintaining thread safety of a
singleton bean.

34
Q

What are the design-patterns

used in Spring framework?

A

Singleton – By default beans defined in spring config files are
singleton. These are based on Singleton pattern.
Template – This pattern is used in many classes likeJdbcTemplate, RestTemplate, JmsTemplate, JpaTemplate etc.
Dependency Injection – This pattern is the core behind the design of
BeanFactory and ApplicationContext.
Proxy – Aspect Oriented Programming (AOP) heavily uses proxy
design pattern.
Front Controller – DispatcherServlet in Spring is based on Front
Controller pattern to ensure that incoming requests are dispatched to
other controllers.
Factory pattern – To create an instance of an object, BeanFactory is
used. This is based on Factory pattern.
View Helper – Spring has multiple options to separating core code
from presentation in views. Like- Custom JSP tags, Velocity macros
etc

35
Q

What is the lifecycle of a Bean in Spring framework?

A
35
Q

What is the lifecycle of a Bean in Spring framework?

A

A Bean in Spring framework goes through following phases in its
lifecycle.

Initialization and creation: Spring container gets the definition of Bean from XML file and instantiates the Bean. It populates all the
properties of Bean as mentioned in the bean definition.

Setting the Behavior of Bean: In case a Bean implements
BeanNameAware interface, Spring uses setBeanName() method to
pass the bean’s id. In case a Bean implements BeanFactoryAware
interface, Spring uses setBeanFactory() to pass the BeanFactory to
bean.
Post Processing: Spring container uses
postProcesserBeforeInitialization() method to call
BeanPostProcessors associated with the bean. Spring calls
afterPropertySet() method to call the specific initialization methods.
In case there are any BeanPostProcessors of a bean, the
postProcessAfterInitialization() method is called.
Destruction: During the destruction of a bean, if bean implements
DisposableBean, Spring calls destroy() method.

36
Q

What are the two main groups of methods in a Bean’s lifecycle?

A

A Bean in Spring has two main groups of lifecycle methods.
Initialization Callbacks: Once all the necessary properties of a Bean
are set by the container, Initialization Callback methods are used for
performing initialization work. A developer can implement method
afterPropertiesSet() for this work.
Destruction Callbacks: When the Container of a Bean is destroyed,
it calls the methods in DisposableBean to do any cleanup work.
There is a method called destroy() that can be used for this purpose
to make Destruction Callbacks.
Recent recommendation from Spring is to not use these methods,
since it can strongly couple your code to Spring code.

37
Q

Can we override main lifecycle methods of a Bean in Spring?

A

Yes, Spring framework allows developers to override the lifecycle methods of a Bean. This is used for writing any custom behavior for
Bean.

38
Q

What are Inner beans in Spring?

A

A bean that is used as a property of another bean is known as Inner
bean. It can be defined as a element in or
tags.
It is not mandatory for an Inner bean to have id or a name. These are
always anonymous.
Inner bean does not need a scope. By default it is of prototype
scope.

39
Q

How can we inject a Java Collection in Spring framework?

A

Spring promotes Dependency Injection (DI) in code. It givessupport for injecting not only objects but also collection of objects.We can inject collections like- list, set, map etc. in Spring.Following tags can be used for this purpose:
: This type is used for injecting a list of values. In a
duplicates are allowed.
: This type is used for injecting a set of values. As per set
property, duplicates are not allowed.
: This type is used for injecting name-value pairs in form of
map. Name and value can be of any type that is allowed for a map.
: This type is used to inject a collection of String based
name-value. It is like a properties file.

40
Q

What is Bean wiring in Spring?

A

A Spring container is responsible for injecting dependencies
between beans. This process of connecting beans is called wiring.
Developer mentions in configuration file, the dependencies between
beans. And Spring container reads these dependencies and wires
the beans on creation.

41
Q

What is Autowiring in Spring?

A

Autowiring is a feature of Spring in which container can automatically wire/connect the beans by reading the configuration
file.

Developer has to just define “autowire” attribute in a bean. Spring resolves the dependencies automatically by looking at this
attribute of beans that are autowired.

42
Q

What are the different modes of

Autowiring supported by Spring?

A

There are five modes of Autowiring supported by Spring
framework:
no: This is default setting for Autowiring. In this case, we use “ref”
mode to mention the explicit bean that is being referred for wiring.
E.g. In this example Employee bean refers Manager bean.

byName: In this case, Spring container tries to match beans by name
during Autowiring. If the name of a bean is same as the name of
bean referred in autowire byname, then it automatically wires it.
E.g. In following example, Manager bean is wired to Employee
bean by Name.

byType: In this case, Spring container check the properties of beans
referred with attribute byType. Then it matches the type of bean and
wires. If it finds more than one such bean of that type, it throws a
fatal exception.

43
Q

What are the cases in which
Autowiring may not work in Spring
framework?

A

Autowiring is a great feature in Spring. It can be used in most of the
cases. But there are certain scenarios in which Autowiring may not work.

Explicit wiring: Since Autowiring is done by Spring, developer does not have full control on specifying the exact class to be used. It
is preferable to use Explicit wiring in case of full control over wiring.

Primitive Data types: Autowiring does not allow wiring of properties that are based on primitive data types like- int, float etc.

44
Q

Is it allowed to inject null or

empty String values in Spring?

A

Yes, Spring allows injecting null or empty String values.

44
Q

Is it allowed to inject null or

empty String values in Spring?

A

Yes, Spring allows injecting null or empty String values.

45
Q

What is a Java-based

Configuration in Spring?

A

Spring allows for Java-based configuration in which a developer
can specify configuration by using Java-based annotations. This
feature was introduced in Spring 3.0.
You can use annotations like- @Configuration, @Bean, @Import
and @DependsOn in Java classes for specifying the configuration

46
Q

What is the purpose of @Configuration annotation?

A
This annotation is used in a class to indicate that this is class is the primary source of bean definitions. This class can also contain
inter-bean dependencies that are annotated by @Bean annotation.
47
Q

What is the difference between
Full @Configuration and ‘lite’
@Beans mode?

A
Spring allows for using @Bean annotation on methods that are declared in classes not annotated with @Configuration. This is
known as “lite” mode. In this mode, bean methods can be declared in a @Component or a plain java class without any annotation.
In the “lite” mode, @Bean methods cannot declare inter-bean dependencies.

It is recommended that one @Bean method should not invoke another @Bean method in ‘lite’ mode.
Spring recommends that @Bean methods declared within @Configuration classes should be used for full configuration. This
kind of full mode can prevent many bugs.

48
Q

In Spring framework, what is Annotation-based container configuration?

A

From Spring 2.5 version it is possible to provide configuration by
using annotation.
To turn this configuration on, we need to mention in spring XMLfile.

Now developer can use annotations like @Required, @Autowired, @Qualifier etc. in a class file to specify the configuration for beans. Spring container can use this information from annotation for creating and wiring the beans.

49
Q

How will you switch on Annotation based wiring in Spring

A

To use Annotation based wiring, we need to turn on Annotation
based configuration in Spring.
By default, Annotation based configuration is switched off in
Spring. To turn it is we can specify
element in Spring config file.
Once it is turned on, we can use @Autowired annotation or
@Required annotation in a Java class for wiring in Spring.

50
Q

What is @Autowired annotation?

A

We can use @Autowired annotation to auto wire a bean on a setter
method, constructor or a field. @Autowired auto wiring is done by
matching the data type.
Before using @Autowired annotation we have to register
AutowiredAnnotationBeanPostProcessor. This can be done by
including in bean configuration file.

51
Q

What is @Required annotation?

A

We use @Required annotation to a property to check whether the
property has been set or not.
Spring container throws BeanInitializationException if the
@Required annotated property is not set.
When we use @Required annotation, we have to register
RequiredAnnotationBeanPostProcessor in Spring config file.

52
Q

What is @Qualifier annotation in Spring?

A

We use @Qualifier annotation to mark a bean as ready for auto wiring. This annotation is used along with @Autowired annotation
to specify the exact bean for auto wiring by Spring container.

53
Q

How Spring framework makes

JDBC coding easier for developers?

A

Spring provides a mature JDBC framework to provide support for
JDBC coding. Spring JDBC handled resource management as well
as error handling in a generic way. This reduces the work of
software developers.
They just have to write queries and related statements to fetch the
data or to store the data in database.

54
Q

What is the purpose of JdbcTemplate?

A

Spring framework provides JdbcTemplate class that contains many
convenient methods for regular tasks like- converting data into
primitives or objects, executing prepared or callable statements etc.
This class makes it very easy to work with database in our
Application and it also provides good support for custom error
handling in database access code.

55
Q

What are the benefits of using

Spring DAO?

A

Some of the benefits of using Spring DAO are:
It makes it easier to work on different data access methods likeJDBC, Hibernate etc.
It provides a consistent and common way to deal with different data
access methods.
Spring DAO makes it easier to switch between different data
persistence frameworks.
No need for catching framework specific exceptions.

56
Q

What are the different ways to use Hibernate in Spring?

A

Spring provides two ways to use Hibernate:
We can extend HibernateDAOSupport and apply an AOP
interceptor node to use Hibernate.
We can also use HibernateTemplate and Callback to access
Hibernate. This is based on Inversion of Control.

57
Q

What types of Object Relational
Mapping (ORM) are supported by
Spring?

A

Spring supports following Object Relational Mapping (ORM) frameworks:

Hibernate
Java Persistence API (JPA)
TopLink
Java Data Objects (JDO)
Apache Object Relational Bridge (ORB)
58
Q

How will you integrate Spring
and Hibernate by using
HibernateDaoSupport?

A

We can use following steps for integrating Spring and Hibernate:
Add dependencies for Spring and Hibernate in pom.xml
Implement DAO from HibernateDaoSupport
Use Hibernate functions via getHibernateTemplate() method

59
Q

What are the different types of
the Transaction Management
supported by Spring framework?

A

Spring framework provides support for two types of Transaction Management:

Programmatic: In this method, we have to manage Transaction by programming explicitly. It provides flexibility to a developer, but it
is not easier to maintain.

Declarative: In this approach, we can separate Transaction Management from the Application Business code. We can use annotations or XML based configuration to manage the transactions in declarative approach.

60
Q

What are the benefits provided by

Spring Framework’s Transaction Management?

A

Consistent: By using Spring Transaction management, we can use
consistent programming model across different transaction APIs
like- JPA, JDBC, JTA, Hibernate, JPA, JDO etc.

Simplicity: Spring TM provides simple API for managing the transaction programmatically.

Declarative: Spring also supports annotation or xml based declarative transaction management.

Integration: Spring Transaction management is easier to integrate
with other data access abstractions of Spring.

61
Q

What is an Aspect in Spring?

A

An Aspect is the core construct of AOP. It encapsulates the behavior
that affects multiple classes in a reusable module.
An Aspect can have a group of APIs that provide cross-cutting
features.
E.g. A logging module can be an Aspect in an Application.
An application can have multiple of Aspects based on the different
requirements.
An Aspect can be implemented by using annotation @Aspect on a
class.

62
Q

What is a Joinpoint in Spring

AOP?

A

In Spring AOP, Joinpoint refers to a candidate point in application where we can plug in an Aspect.
Joinpoint can be a method or an exception or a field getting modified.
This is the place where the code of an Aspect is inserted to add new
behavior in the existing execution flow

63
Q

What is an Advice in Spring AOP?

A

An Advice in Spring AOP, is an object containing the actual action that an Aspect introduces.

An Advice is the code of cross cutting concern that gets executed. There are multiple types of Advice in Spring AOP.

64
Q

What are the different types of

Advice in Spring AOP?

A

Spring AOP provides five kinds of Advice:
1. Before Advice: This type of advice runs just before a
method executes. We can use @Before annotation for this.
2. After (finally) Advice: This type of advice runs just after a
method executes. Even if the method fails, this advice will
run. We can use @After annotation here.
3. After Returning Advice: This type of advice runs after a
method executes successfully. @AfterReturning annotation
can be used here.
4. After Throwing Advice: This type of advice runs after a
method executes and throws an exception. The annotation
to be used is @AfterThrowing.
5. Around Advice: This type of advice runs before and after
the method is invoked. We use @Around annotation for
this.

65
Q

What is a Pointcut in Spring

AOP?

A
A Pointcut in Spring AOP refers to the group of one or more
Joinpoints where an advice can be applied.
We can apply Advice to any Joinpoint. But we want to limit the
places where a specific type of Advice should be applied. To
achieve this we use Pointcut.
We can use class names, method names or regular expressions to
specify the Pointcuts for an Advice.
66
Q

What is an Introduction in Spring AOP?

A

In Spring AOP we can declare additional methods or fields on behalf of a type. To do this we use an Introduction. It is also known
as inter-type declaration.

E.g. We can use an Introduction for making a bean implement IsModified interface.

67
Q

What is a Target object in Spring AOP?

A

A Target object is the object that gets Advice from one or more
Aspects.
This is also known as advised object.
In most cases it is a proxy object.

68
Q

What is a Proxy in Spring AOP?

A

In Spring AOP, a Proxy is an object created by the AOP framework to implement Aspect contracts. It is generally a JDK dynamic proxy
or CGLIB proxy.

69
Q

What are the different types of

AutoProxy creators in Spring?

A

Spring AOP provides following standard types of Autoproxy
creators:
1. BeanNameAutoProxyCreator: This is a
BeanPostProcessor that creates AOP proxies for beans
automatically by matching names.
2. DefaultAdvisorAutoProxyCreator: This creator is more
powerful that other Proxy Creators. This also applies
eligible advisors automatically to bean in the current
context.
3. AbstractAdvisorAutoProxyCreator: This is the parent
class of DefaultAdvisorAutoProxyCreator. We can create
our own auto-proxy creators by extending this class.

70
Q

What is Weaving in Spring AOP?

A

In Aspect oriented programming, linking Aspects with the other
application types creates an Advised object. This process is known
as Weaving.
Without Weaving, we just have definition of Aspects. Weaving
makes use realize full potential of the AOP.
Weaving can be done at compile time, load time or at run time

71
Q

In Spring AOP, Weaving is done

at compile time or run time?

A

Spring container performs Weaving at run time.

72
Q

What is Annotation-based aspect implementation in Spring AOP?

A

This is a declarative style AOP implementation. In this case, we use
annotations like @Aspect, @Pointcut, @Joinpoint etc. to annotate
code with different types of AOP elements.
This can be used Java 5 onwards, when the support for Annotations
was introduced.

73
Q

How does Spring MVC framework work?

A

Spring provides its own Model View Controller (MVC) framework
for developing web applications.
Spring MVC framework is based on Inversion of Control (IOC)
principle. It separates the business objects from controller.
It is designed around the DispatcherServlet that is responsible for
dispatching requests to relevant handlers.
Spring MVC framework also supports annotation based binding of
request parameters.

74
Q

What is DispatcherServlet?

A

In Spring MVC, DispatcherServlet is the core servlet that is responsible for handling all the requests and dispatching these to
handlers.

Dispatcher servlet knows the mapping between the method to be called and the browser request. It calls the specific method and combines the results with the matching JSP to create an html document, and then sends it back to browser.

In case of RMI invocation, it sends back response to the client application.

75
Q

Can we have more than one DispatcherServlet in Spring MVC?

A

Yes, a Spring MVC web application can have more than one
DispatcherServlets.
Each DispatcherServlet has to operate in its own namespace. It has
to load its own ApplicationContext with mappings, handlers, etc.
Only the root application context will be shared among these
Servlets

76
Q

What is WebApplicationContext in Spring MVC?

A

WebApplicationContext is the child of plain ApplicationContext. It
is used in web applications. It provides features to deal with webrelated components like- controllers, view resolvers etc.
A Web Application can have multiple WebApplicationContext to
handle requests.
Each DispatcherServlet is associated with one
WebApplicationContext.

77
Q

What is Controller in Spring MVC framework?

A

Controller is an interface in Spring MVC. It receives
HttpServletRequest and HttpServletResponse in web app just like
an HttpServlet, but it is able to participate in an MVC flow.
Controllers are similar to a Struts Action in a Struts based Web
application.
Spring recommends that the implementation of Controller interface
should be a reusable, thread-safe class, capable of handling
multiple HTTP requests throughout the lifecycle of an application.
It is preferable to implement Controller by using a JavaBean.
Controller interprets user input and transforms it into a model. The
model is represented to the user by a view.
Spring implements a controller in a very generic way. This enables
us to create a wide variety of controllers.
What is @Controller annotation in Spring MVC?
We use @ Controller annotation to indicate that a class is a
Controller in Spring MVC.
The dispatcher in Spring scans for @Controller annotated classes
for mapped methods and detects @RequestMapping.

78
Q

What is @RequestMapping annotation in Spring?

A

In Spring MVC, we use @RequestMapping annotation to map a web
request to either a class or a handler method.
In @RequestMapping we can specify the path of URL as well as
HTTP method like- GET, PUT, POST etc.
@RequestMapping also supports specifying HTTP Headers as
attributes.
We can also map different media types produced by a controller in
@RequestMapping. We use HTTP Header Accepts for this purpose.
E.g. @RequestMapping(
value = “/test/mapping”,
method = GET,
headers = “Accept=application/json”)

79
Q

How will you decide which scopePrototype or Singleton to use for a
bean in Spring?

A

In general, we use prototype scope for all stateful beans and singleton scope for stateless beans.

Since a stateless bean does not maintain any state, we can use the same object instance again and again. Singleton scope bean serves
the same purpose.

In a stateful bean, there is a need to maintain the state in each request, it is necessary to use a new instance of object with each call. A Prototype scope bean ensures that we get a new instance each time we request for the object.