Spring Interview Questions Flashcards

1
Q

What is Spring?

A

Spring is an open source development framework for Enterprise Java. The core features of the Spring Framework can be used in developing any Java application, but there are extensions for building web applications on top of the Java EE platform. Spring framework targets to make Java EE development easier to use and promote good programming practice by enabling a POJO-based programming model.

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

What are benefits of Spring Framework?

A

Lightweight: Spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 2MB.
Inversion of control (IOC): Loose coupling is achieved in Spring, with the Inversion of Control technique. The objects give their dependencies instead of creating or looking for dependent objects.
Aspect oriented (AOP): Spring supports Aspect oriented programming and separates application business logic from system services.
Container: Spring contains and manages the life cycle and configuration of application objects.
MVC Framework: Spring’s web framework is a well-designed web MVC framework, which provides a great alternative to web frameworks.
Transaction Management: Spring provides a consistent transaction management interface that can scale down to a local transaction and scale up to global transactions (JTA).
Exception Handling: Spring provides a convenient API to translate technology-specific exceptions (thrown by JDBC, Hibernate, or JDO) into consistent, unchecked exceptions.

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

Which are the Spring framework modules?

A

There are around 20 modules which are generalized into Core Container, Data Access/Integration, Web, AOP (Aspect Oriented Programming), Instrumentation and Test. The basic modules of the Spring framework are :

Spring Core Container

This layer is basically the core of Spring Framework. It contains the following modules:

Core module
Bean module
Context module
Expression Language module

Data Access/Integration

This layer provides support to interact with the database. It contains the following modules:

    JDBC module
    Object-Relational Mapping (ORM) module
    Java Messaging Service (JMS) module
    Object XML Mappers (OXM) module
    Transaction Management module

Web

This layer provides support to create web application. It contains the following modules:

Web module
Web-MVC module
Web-Socket module
Web-Portlet module

Aspect Oriented Programming (AOP)

In this layer you can use Advices, Pointcuts etc., to decouple the code.
Instrumentation – This layer provides support to class instrumentation and classloader implementations.

Test

This layer provides support to testing with JUnit and TestNG.

Messaging

This module provides support for STOMP. It also supports an annotation programming model that is used for routing and processing STOMP messages from WebSocket clients.

Aspects

This module provides support to integration with AspectJ.

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

Explain the Core Container (Application context) module

A

This is the basic Spring module, which provides the fundamental functionality of the Spring framework. BeanFactory is the heart of any spring-based application. Spring framework was built on the top of this module, which makes the Spring container.

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

BeanFactory implementation example

A

A BeanFactory is an implementation of the factory pattern that applies Inversion of Control to separate the application’s configuration and dependencies from the actual application code.

The most commonly used BeanFactory implementation is the XmlBeanFactory class.

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

XMLBeanFactory

A

The most useful one is org.springframework.beans.factory.xml.XmlBeanFactory, which loads its beans based on the definitions contained in an XML file. This container reads the configuration metadata from an XML file and uses it to create a fully configured system or application.

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

Explain the AOP module

A

The AOP module is used for developing aspects for our Spring-enabled application. Much of the support has been provided by the AOP Alliance in order to ensure the interoperability between Spring and other AOP frameworks. This module also introduces metadata programming to Spring.

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

Explain the JDBC abstraction and DAO module

A

With the JDBC abstraction and DAO module we can be sure that we keep up the database code clean and simple, and prevent problems that result from a failure to close database resources. It provides a layer of meaningful exceptions on top of the error messages given by several database servers. It also makes use of Spring’s AOP module to provide transaction management services for objects in a Spring application.

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

Explain the object/relational mapping integration module

A

Spring also supports for using of an object/relational mapping (ORM) tool over straight JDBC by providing the ORM module. Spring provides support to tie into several popular ORM frameworks, including Hibernate, JDO, and iBATIS SQL Maps. Spring’s transaction management supports each of these ORM frameworks as well as JDBC

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

Explain the web module

A

The Spring web module is built on the application context module, providing a context that is appropriate for web-based applications. This module also contains support for several web-oriented tasks such as transparently handling multipart requests for file uploads and programmatic binding of request parameters to your business objects. It also contains integration support with Jakarta Struts.

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

Explain the Spring MVC module

A

MVC framework is provided by Spring for building web applications. Spring can easily be integrated with other MVC frameworks, but Spring’s MVC framework is a better choice, since it uses IoC to provide for a clean separation of controller logic from business objects. With Spring MVC you can declaratively bind request parameters to your business objects.

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

Spring configuration file

A

Spring configuration file is an XML file. This file contains the classes information and describes how these classes are configured and introduced to each other.

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

How can we have multiple Spring configuration files?

A

web.xml contextConfigLocation: you can load them all into your Web application context via the ContextConfigLocation element. You’re already going to have your primary applicationContext here, assuming you’re writing a web application. All you need to do is put some white space between the declaration of the next context.
applicationContext.xml import resource: you can add your primary applicationContext.xml to the web.xml and then use import statements in that primary context.

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

What are the common implementations of the ApplicationContext?

A

The FileSystemXmlApplicationContext container loads the definitions of the beans from an XML file. The full path of the XML bean configuration file must be provided to the constructor.
The ClassPathXmlApplicationContext container also loads the definitions of the beans from an XML file. Here, you need to set CLASSPATH properly because this container will look bean configuration XML file in CLASSPATH.
The WebXmlApplicationContext: container loads the XML file with definitions of all beans from within a web application.

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

What is the difference between Bean Factory and ApplicationContext?

A

Application contexts provide a means for resolving text messages, a generic way to load file resources (such as images), they can publish events to beans that are registered as listeners. In addition, operations on the container or beans in the container, which have to be handled in a programmatic fashion with a bean factory, can be handled declaratively in an application context. The application context implements MessageSource, an interface used to obtain localized messages, with the actual implementation being pluggable.

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

What are some of the best practices for Spring Framework?

A

Some of the best practices for Spring Framework are:

    Define singleton beans with names same as their class or interface names
    Place Spring bean configuration files under a folder instead of root folder
    Give common prefixes or suffixes to Spring bean configuration files
    Avoid using import elements within Spring XML configuration files as much as possible
    Stay away from auto wiring in XML based bean configurations
    Always externalize bean property values with property placeholders
    Select default version-less XSD when importing namespace definitions
    Always place classpath prefix in resource paths
    Create a setter method even though you use field level auto wiring
    Create a separate service layer even though service methods barely delegate their responsibilities to corresponding DAO methods
17
Q

What are the various ways of using Spring Framework?

A

You can use Spring Framework:

for writing web applications
for exposing RESTful services
to secure your web applications
for communicating with databases
for handling long running jobs
to handle external resources or systems you have to work with
for testing purposes
for standalone java projects
to convert your application into an executable
to integrate Social Media into your applications
18
Q

How can we use Spring to create Restful Web Service returning JSON response?

A

Any Spring @RestController in a Spring Boot application should render JSON response by default as long as Jackson2 is on the classpath.

19
Q

Spring vs Spring MVC vs Spring Boot?

A

Spring: the most important feature of Spring is Dependency Injection or Inversion of Control.
Spring MVC: is a complete HTTP oriented MVC framework managed by the Spring Framework and based in Servlets. It would be equivalent to JSF in the JavaEE stack.
Spring Boot: is a utility for setting up applications quickly, offering an out of the box configuration in order to build Spring powered applications.

20
Q

What does a Spring application look like?

A

Interface: An interface that defines the functions.
Bean class: It contains properties, its setter and getter methods, functions etc.
Spring AOP: Provides the functionality of cross-cutting concerns.
The configuration XML file: Contains the information of classes and how to configure them.
The Client program: uses the function.