Spring 5.0 Question and Answers
Spring 5.0 Question and Answers
Spring 5.0 Question and Answers
1) What is spring framework? Why Java programmer should use Spring framework
Very common Spring interview question, Spring is a framework which helps Java
programmer in development.
Spring provides dependency Injection and IOC container, Spring MVC flow and several
useful API for Java programmer.
6) What is Spring MVC ? Can you explain How one request is processed ?
Spring security is a project under spring framework umbrella, which provides support for
security requirements of enterprise Java projects. Spring Security formerly known as aegis
security provides out of box support for creating login screen, remember me cookie support,
securing URL, authentication provider to authenticate the user from the database, LDAP and
in memory, concurrent active session management support and much more. In order to use
Spring security in a Spring MVC based project, you need to include spring-security.jar and
configure it in application-Context-security.XML file, you can name it whatever you want, but
make sure to supply this to ContextLoaderListener, which is responsible for creating Spring
context and initializing dispatcher servlet.
10) How do you control concurrent Session on Java web application using Spring
Security?
You can use Spring Security to control a number of active session in Java web application.
Spring security framework provides this feature out of the box and when enabled , a user can
only have one active session at a time.
There are 2 types of dependency injection supported by Spring, constructor based injection,
and setter-based injection. Both types have their own advantages and disadvantages, you
should use Constructor injection when object's dependencies are not optional and they must
be initialized with their dependencies. Also use constructor injection if the order of initialization
or dependency matters because in Setter based injection you cannot impose any order. Use
setter injection when dependencies are optional.
15) Can we use more than one configuration file for our Spring project?
16) Explain Spring MVC flow with a simple example e.g. starting from Container receives a
request and forward to your Java application ?
19) What is the advantage of Spring MVC framework over Struts 1.0 or Struts 2.0 ? is it worth
to convert an existing Struts application to Spring MVC ?
Some Spring MVC questions are tricky e.g. Struts and Spring integration and can be only
answered by experienced Java program with 2 to 4-year experience in Spring MVC
framework.
21) If a user checked in CheckBox and got a validation error in other fields and then he
unchecked the CheckBox, what would be selection status in command object in Spring
MVC ? How do you fix this issue?
Since during HTTP post, if the checkbox is unchecked than HTTP does include a request
parameter for checkbox, which means updated selection won't be picked up. you can use
hidden form field, starting with _ to fix this in Spring MVC. quite a tricky question to answer if
you are not aware of HTTP POST behavior and Spring MVC.
22) What are different implementations of View interface you have used in Spring
MVC?
There are some methods in Spring tag library, can't remember now.
Following is the list of few of the great benefits of using Spring Framework –
There are loads of different design patterns used, but there are a few obvious ones:
It’s very beneficial and easy to use Spring security in web applications, through the use
of annotations such as @EnableWebSecurity.
● Instantiate − First the spring container finds the bean's definition from the XML file and
● Populate properties − Using the dependency injection, spring populates all of the
● Set Bean Name − If the bean implements BeanNameAware interface, spring passes the
● Set Bean factory − If Bean implements BeanFactoryAware interface, spring passes the
● Pre Initialization − Also called postprocess of bean. If there are any bean
postProcesserBeforeInitialization() method.
● Initialize beans − If the bean implements IntializingBean,its afterPropertySet() method
is called. If the bean has init method declaration, the specified initialization method is called.
● Post Initialization − If there are any BeanPostProcessors associated with the bean,
● Destroy − If the bean implements DisposableBean , it will call the destroy() method .
https://www.javacodegeeks.com/2014/05/spring-interview-questions-and-
answers.html
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.
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.
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
don’t 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 <bean/> 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 setup which is
called when the bean is loaded in to the container. The second method is the
teardown method 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).
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.
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).
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.
66. WebApplicationContext
The WebApplicationContext is an extension of the plain ApplicationContext that
has some extra features necessary for web applications. It differs from a normal
ApplicationContext in that it is capable of resolving themes, and that it knows
which servlet it is associated with.
the transaction with the help of programming. That gives you extreme flexibility, but it is
difficult to maintain.
● Declarative transaction management − This means you separate transaction
management from the business code. You only use annotations or XML based configuration