Spring framework - Interview Questions and Answers

Q1. What is Spring ? 

Ans. Spring is a light weight Dependency Injection and AOP container and framework.

Q2. What is dependency injection or IOC ?


Ans. It's a mechanism of passing object creation to an external component wherein dependent objects are identified and created using configuration.  

Q3. Difference between ApplicationContext module and BeanFactory ?

a. BeanFactory helps in Bean Instantiation whereas ApplicationContext provides additional capabilities like Message Internationalization and event handling.

b. BeanFactory loads the beans lazily i.e it doesn't load all beans when factory is loaded whereas ApplicationContext loads the beans eagerly.

c. Access to low level resources is not convenient in BeanFactory whereas its easier in Application Context.

Q4.  How can we manage Error Messages in the web application ?

Ans. Within message.properties file.

Q5.  Difference between Java beans and Spring Beans ?

Ans. Java Beans managed by Spring IoC are called Spring Beans.

Q6.  What are different modules of spring ?

Ans. There are seven core modules in spring

Spring MVC
The Core container
O/R mapping
DAO
Application context
Aspect Oriented Programming or AOP
Web module

Q7.  Explain Flow of Spring MVC ?

Ans. The DispatcherServlet configured in web.xml file receives the request.

The DispatcherServlet finds the appropriate Controller with the help of HandlerMapping and then invokes associated Controller.

Then the Controller executes the logic business logic and then returns ModeAndView object to the DispatcherServlet.

The DispatcherServlet determines the view from the ModelAndView object.

Then the DispatcherServlet passes the model object to the View.

The View is rendered and the Dispatcher Servlet sends the output to the Servlet container. Finally Servlet Container sends the result back to the user.

Q8.  What is Spring configuration file?

Ans. 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.

Q9.  Q: What is default scope of bean in Spring framework?

Ans. The default scope of bean is Sing leton for Spring framework.

Q10.  What bean scopes does Spring support? Explain them.

Ans. The Spring Framework supports following five scopes -

Singleton
prototype 
request
session 
global-session

Q11.  What is bean auto wiring?

Ans. The Spring container is able to autowire relationships between collaborating beans. This means that it is possible to automatically let Spring resolve collaborators (other beans) for your bean by inspecting the contents of the BeanFactory without using <constructor-arg> and <property> elements.

Q12. What are different auto wiring types ?

Ans. byName , by Type and constructor.

Q13. What are the disadvantages of auto wiring ?

Ans. 

Hard to identify the wiring if the configuration size grows big. 

Won't do autowire if the wiring has been specified explicitly. 

If there is a conflict between multiple bean configuration, Spring won't resolve the conflict and reject all for autowiring.