01: ♥♦ 13 Spring interview questions & answers Q1 – Q7

Spring interview questions are very popular with the job interviewers along with the Hibernate interview questions & answers. You may also like Video: Spring DIP, DI, and IoC.

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.

DIP, DI & IoC

DIP, DI & IoC

1) Dependency Inversion Principle (DIP): is one of the 6 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 below as “// code to interface” for the understanding.

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 or via setters 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). Dependency Injection is a design pattern that allows us to write loosely coupled code for better maintainability,

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.

Still not too clear?

Try Spring DIP, DI & IoC in detail with diagrams.

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 or Service Locator) .

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: A class is still responsible for creating its dependencies. It just uses the service locator to do it.

Here is a DI example with Spring IoC container…

Interface Processor

Spring Configuration AppConfig

Processor implementations “JsonProcessor” and “XmlProcessor”

Service class MyServiceImpl

Standalone App to execute

Output:

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, MyService, 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

Print Friendly
The following two tabs change content below.
Arulkumaran Kumaraswamipillai
Mechanical Engineering to Java freelancer since 2003. Published Java/JEE books via Amazon.com in 2005, and sold 35K+ copies. Books are outdated and replaced with this online Java training.
Arulkumaran Kumaraswamipillai

Mechanical Engineering to Java freelancer since 2003. Published Java/JEE books via Amazon.com in 2005, and sold 35K+ copies. Books are outdated and replaced with this online Java training.

Posted in Spring, Spring Job Interview Essentials
Tags:
3 comments on “01: ♥♦ 13 Spring interview questions & answers Q1 – Q7
  1. indrjeetkumar says:

    Very Good explanation of the concept and cover the topic in very easy way

    Keep the good job and helping the people through Video.

  2. Nitin says:

    Excellent article.

  3. Thaya says:

    Great, Concepts are clearly explained.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

1100+ paid subscribers. Reviews | Free Contents. Monthly 260K+ views & 40k+ visitors. 9 tips to earn more.

Java Developer Training – 800+ Q&As ♥Free|♦FAQ (Mouse Hover for Tooltip)

open all | close all

200+ Java Developer Job Interview FAQs

open all | close all

16 Java Programmer Key Areas to be a top-notch

open all | close all

80+ Java Tutorials – Step by step

open all | close all

100+ Java Developer Coding Exercises

open all | close all

How good are your …..Java job hunting & career fast-tracking skills?

open all | close all