01: Q01 – Q07 Spring core interview Q&As

Spring interview questions are very common for the Java programmer jobs.

Q1. What do you understand by the terms Dependency Inversion Principle (DIP), Dependency Injection (DI) and Inversion of Control (IoC) container?
A1. The differences are very subtle and can be hard to understand. Hence, explained via code samples.



1) Dependency Inversion Principle (DIP): is one of the 5 OO design principles abbreviated as “SOLID”, and “D” stands for DIP meaning that we should always only rely on interfaces and not on their implementations. The idea of DIP is that higher layers of your application should not directly depend on lower layers. DIP is the principle that guides us towards DI pattern. You will see in the example below that the higher layer module “MyServiceImpl” depends on the lower layer module interface “Processor” and NOT on the implementations “XmlProcessor” & “JsonProcessor”. This is commented on the code shown in Q3 for “MyServiceImpl” as “// code to interface” for the understanding.

DIP – Dependency Inversion Principle

2) Dependency Injection (DI): is a design pattern where instead of having your objects create a dependency or asking a factory object to make one for you, you pass the needed dependencies into the constructor (i.e. Constructor Injection) or via setter methods (i.e. Setter Injection) from outside the class. This is achieved by defining the dependencies as interfaces, and then injecting in a concrete class implementing that interface via a constructor (i.e. constructor injection) or a setter method (i.e. setter injection) by wiring up via an IoC container like Spring. You can wire up the dependencies from outside using an XML config file as shown below:

or using annotations (i.e. preferred) as demonstrated with code in Q3. Dependency Injection is a design pattern that allows us to write loosely coupled code for better maintainability.

Q. Wondering, what loose coupling is? how to write loosely coupled code?
A. Top 6 tips to go about writing loosely coupled Java applications.

3) Inversion of Control (IoC): is a software design principle where the framework controls the program flow. Spring framework, Guice, etc are IoC containers that implement the IoC principle. An IoC container like Spring is responsibly for loosely wiring up the dependencies. When Spring application runs, it looks at the either XML config file or the annotations to wire up the dependencies. For example, in the example shown below Spring creates instances of XmlProcessor & JsonProcessor and inject them via a constructor into “MyServiceImpl“.

Q2. What are you “Inverting” in IoC?
A2. Flow of control is “inverted” by dependency injection because you are effectively delegating dependencies to some external system (e.g. IoC container like Spring or Service Locator).

Still not too clear? Try Spring DIP, DI & IoC in detail with diagrams & a step by step video tutorial: Spring DIP, DI, and IoC.

Q3. What are the different implementation patterns of IoC principle?
A3. The two implementation patterns of the IoC design principles are

  1. Dependency Injection (DI) pattern: A class is given it’s dependencies from outside like Spring IoC or JEE 7+ container. It neither knows, nor cares where the dependencies are coming from.
  2. Service Locator (SL) pattern: has the same goal as DI where a service locator class is responsible for creating its dependencies. E.g. JNDI or other Map based registries to map dependencies. In a map based registry you can lookup services based on keys. Data sources, JMS connection factories and destinations (e.g. Queue or Topic) can be looked up via JNDI.

Here is a DI example with Spring IoC container. We inject XML & JSON processors into “MyServiceImpl”.

Interface Processor

Spring Configuration AppConfig

Processor implementations “JsonProcessor” and “XmlProcessor”

Service class MyServiceImpl

Standalone App to execute


Service Locator (SL) type IoC example:

Not a common IoC pattern. Very rarely used. Same code as above can be modified to use a Service Locator.

Service Locator “ProcessorServiceLocatorFactory”

Modified “MyServiceImpl” to use the Service Locator

The core of the Spring Framework is its Inversion of Control (Ioc) container. The Spring IoC container manages Java objects from their instantiation to destruction via its BeanFactory. Java components that are instantiated by the IoC container are called beans, and the IoC container manages a bean’s scope (e.g. prototype vs singleton), lifecycle events (e.g. initialization, method callbacks & shutdown), and any AOP (Aspect Oriented Programming) features if configured.

The key focus of both types of IoC is to loosely couple dependencies among components like MyApp, MyServiceImpl, and Procesor as per the above examples.

Q4. What are the different types of dependency injections?
A4. There are 4 types of dependency injection. Spring supports 3 types. 1, 2 & 4 shown below.

1) Constructor Injection (e.g. Spring): Dependencies are provided as constructor parameters.

2) Setter Injection (e.g. Spring): Dependencies are assigned through setter methods.

3) Interface Injection (e.g. Avalon): Injection is done through an interface.

4) Field injection: Using annotations on fields and parameters.

Spring supports 1) Constructor Injection, 2) Setter Injection & 4) Field injection with annotations.

Q5. Which ones are the most commonly used DIs?
A5. 1) Constructor Injection, 2) Setter Injection & 4) Field injection with annotations.

Q6. When will you favor DI type “Constructor Injection” over “Setter Injection”?
A6. Using constructor injection allows you to hide immutable fields from users of your class. Immutable classes don’t declare setter methods. This also enforces that you have the valid objects at the construction time. It also prompts you to rethink about your design when you have too many constructor parameters.

Q7. When will you favor DI type “Setter Injection” over “Constructor Injection”?
A7. In some scenarios, the constructors may get a lot of parameters, which force you to create a lot of overloaded constructors for every way the object might be created. In these scenarios setter injection can be favored over constructor injection, but having too many constructor parameters may be an indication of a bad design.

Q8 to Q13: 13 Spring basics Q8 – Q13 interview questions & answers.

More FAQ Spring Interview Questions & Answers:

1. 9 Spring Bean Scopes Interview Q&As

2. 17 Spring FAQ interview Questions & Answers

3. 30+ FAQ Hibernate interview questions & answers

300+ Java & Big Data Interview FAQs

16+ Java Key Areas Interview Q&As

800+ Java Interview Q&As

300+ Java & Big Data Tutorials