Every interview which covers Spring, these questions are asked most of the times. This is the List of basic Spring interview questions with answers provided along with them. Make sure you go through them if you are planning to appear on any Java interview covering Spring framework.
Question 1: What is Spring and what are the benefits of using 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. There are many features of Spring:
Inversion of control (IOC): Loose coupling is achieved in spring using the technique Inversion of Control. The objects give their dependencies instead of creating or looking for dependent objects.
Lightweight: Spring is lightweight when it comes to size and transparency.
Aspect oriented (AOP): Spring supports Aspect oriented programming and enables cohesive development by separating 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 and provides a great alternative to web frameworks such as Struts.
Transaction Management: Spring provides a consistent transaction management interface that can scale down to a local transaction (using a single database, for example) and scale up to global transactions (using JTA, for example).
Exception Handling: Spring provides a convenient API to translate technology-specific exceptions (thrown by JDBC, Hibernate, or JDO, for example) into consistent, unchecked exceptions.
Question 2: What are the spring modules?
There are mainly seven modules of Spring:
1) The Core Container
2) Spring Context
3) Spring AOP
4) Spring DAO
5) Spring ORM
6) Spring Web Module
7) Spring MVC Framework
Question 3:What is Dependency Injection (DI)?
Dependency Injection has two rules:
1) High level modules should not depend upon low level modules; both should depend on abstractions. Ex: Business layer should not depend on DAO
2) Abstraction should not depend upon details. Details should depend upon abstractions. Ex: Find common behaviors in code
In addition to these rules, DI has the concept of assembler or factory – that instantiates the objects required by an application and injects them into their dependency objects.
Question 4:What is inversion of Control (IOC) and what is the difference between DI and IOC ?
IOC is much larger than DI. Spring IOC container enforces dependency injection and employs a number of design patterns to achieve this. IOC is a process by which application defines the dependency which are then satisfied in runtime by Spring framework. It also defines life cycle events.
Question 5:What are the different types of dependency injection (DI)?
There are three types of DI though in Spring just two.
1) Constructor Injection – Constructor arguments are injected during instance initialization.
2) Setter Injection – dependencies are set in the objects through setter methods defined in Spring config file.
3) Interface Injection – NOT in Spring
Question 6: Which DI is suggested to use – constructor or setter based DI?
DI can be selected based on the need as the cases provided below.
Cases for Constructor DI:
1) Allows to enforce mandatory dependencies
2) Promote Immutability – assign dependencies to final fields
3) Concise – creation and injection in 1 line of code
Cases for setter based DI:
1) Allow optional dependencies and defaults
2) Descriptive names can be provided
3) Inherited automatically
Setters can be made mandatory by decorating the property with the [SetterProperty] attribute.
Question 7: What are the implementation of ApplicationContext?
There are three implementations of ApplicationContext in Spring.
1) ClassPathXmlApplicationContext: This context loads a context definition from an XMl file located in classpath, treating that as a classpath resource.
2) FileSystemXmlApplicationContext: loads the context definition from an XML file in the application.
Absolute Path : C:\etc\app-config.xml
3) XmlWebApplicationContext: loads the xml file contained within a web application.
Question 8: What are the different Spring scope?
The Spring Framework supports following five scopes, three of which are available only if you use a web-aware ApplicationContext.
singleton: Each bean instance has default ‘Singleton’ scope.This scopes the bean definition to a single instance per Spring IoC container.
prototype: A new instance is created each time the bean is referenced.This scopes a single bean definition to have any number of object instances.
request: A new instance once per user request (HTTP request) gets created. Only valid in the context of a web-aware Spring ApplicationContext.
session: A new instance once per user session (HTTP session) gets created. Only valid in the context of a web-aware Spring ApplicationContext.
global-session: This scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.
Question 9: What is default scope of bean in Spring framework?
The default scope of bean is Singleton in Spring framework. This scopes the bean definition to a single instance per Spring IoC container.
Question 10: Are Singleton beans thread safe in Spring Framework?
No, they are not. Singleton beans are not thread-safe in Spring framework.