69 Spring Interview Questions and Answers - The ULTIMATE List (PDF Download)
69 Spring Interview Questions and Answers - The ULTIMATE List (PDF Download)
Spring overview
1. What is Spring?
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.
Context module
Expression Language module
JDBC module
ORM module
OXM module
Java Messaging Service(JMS) module
Transaction module
Web module
Web-Servlet module
Web-Struts module
Web-Portlet module
6. XMLBeanFactory
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.
Spring AOP
The Spring configuration XML file.
Client program that uses the function
Dependency Injection
18. What is Dependency Injection in Spring?
Dependency Injection, an aspect of Inversion of Control (IoC), is a general concept, and it can be
expressed in many different ways.This concept says that you do not create your objects but describe
how they should be created. You dont directly connect your components and services together in code
but describe which services are needed by which components in a configuration file. A container (the
IOC container) is then responsible for hooking it all up.
Spring Beans
21. What are Spring beans?
The Spring Beans are Java Objects that form the backbone of a Spring application. They are
instantiated, assembled, and managed by the Spring IoC container. These beans are created with the
configuration metadata that is supplied to the container, for example, in the form of XML definitions.
Beans defined in spring framework are singleton beans. There is an attribute in bean tag named
"singleton" if specified true then bean becomes singleton and if set to false then the bean becomes
a prototype bean. By default it is set to true. So, all the beans in spring framework are by default
singleton beans.
28. Which are the important beans lifecycle methods? Can you override them?
There are two important bean lifecycle methods. The first one is setupwhich is called when the bean
is loaded in to the container. The second method is the teardownmethod which is called when the
bean is unloaded from the container.
The bean tag has two important attributes ( init-method and destroy-method) with which you can
define your own custom initialization and destroy methods. There are also the correspondive
annotations(@PostConstruct and @PreDestroy).
autodetect: Spring first tries to wire using autowire by constructor, if it does not work, Spring
tries to autowire by byType.
35. Can you inject null and empty string values in Spring?
Yes, you can.
Spring Annotations
36. What is Spring Java-Based Configuration? Give some annotation example.
Java based configuration option enables you to write most of your Spring configuration without XML but
with the help of few Java-based annotations.
An example is the @Configuration annotation, that indicates that the class can be used by the
Spring IoC container as a source of bean definitions. Another example is the@Bean annotated method
that will return an object that should be registered as a bean in the Spring application context.
43. JdbcTemplate
JdbcTemplate class provides many convenience methods for doing things such as converting
database data into primitives or objects, executing prepared and callable statements, and providing
custom database error handling.
49. What are the benefits of the Spring Frameworks transaction management?
It provides a consistent programming model across different transaction APIs such as JTA,
JDBC, Hibernate, JPA, and JDO.
It provides a simpler API for programmatic transaction management than a number of complex
transaction APIs such as JTA.
It supports declarative transaction management.
It integrates very well with Springs various data access abstractions.
52. Aspect
The core construct of AOP is the aspect, which encapsulates behaviors affecting multiple classes into
reusable modules. It ia a module which has a set of APIs providing cross-cutting requirements. For
example, a logging module would be called AOP aspect for logging. An application can have any
number of aspects depending on the requirement. In Spring AOP, aspects are implemented using
regular classes annotated with the @Aspect annotation (@AspectJ style).
53. What is the difference between concern and cross-cutting concern in Spring
AOP
The Concern is behavior we want to have in a module of an application. A Concern may be defined as
a functionality we want to implement.
The cross-cutting concern is a concern which is applicable throughout the application and it affects the
entire application. For example, logging, security and data transfer are the concerns which are needed
in almost every module of an application, hence they are cross-cutting concerns.
55. Advice
The advice is the actual action that will be taken either before or after the method execution. This is
actual piece of code that is invoked during the program execution by the Spring AOP framework.
Spring aspects can work with five kinds of advice:
before: Run advice before the a method execution.
after: Run advice after the a method execution regardless of its outcome.
after-returning: Run advice after the a method execution only if method completes
successfully.
after-throwing: Run advice after the a method execution only if method exits by throwing an
exception.
around: Run advice before and after the advised method is invoked.
56. Pointcut
The pointcut is a set of one or more joinpoints where an advice should be executed. You can specify
pointcuts using expressions or patterns.
61. What is Weaving? What are the different points where weaving can be applied?
Weaving is the process of linking aspects with other application types or objects to create an advised
object.
65. DispatcherServlet
The Spring Web MVC framework is designed around a DispatcherServletthat handles all the
HTTP requests and responses.
66. WebApplicationContext
The WebApplicationContextis an extension of the plain ApplicationContextthat has some
extra features necessary for web applications. It differs from a normal ApplicationContextin that it
is capable of resolving themes, and that it knows which servlet it is associated with.
You are welcome to contribute with your comments and we will include them in the article!