Engineering With Java

Engineering With Java

How Can We Inject Beans In Spring? — Spring Interview Question

Answering Popular Spring Interview Questions

Suraj Mishra's avatar
Suraj Mishra
Apr 26, 2025
∙ Paid


Consider becoming a free or paid subscriber and support the work :)

Still not convinced? Check out the details of the past work


Overview

In spring, beans can be injected using various methods, including constructor injection, setter injection, and field injection. Here’s a brief overview of each method:

𝟏. 𝐂𝐨𝐧𝐬𝐭𝐫𝐮𝐜𝐭𝐨𝐫 𝐈𝐧𝐣𝐞𝐜𝐭𝐢𝐨𝐧:
Constructor injection is done by providing dependencies through a constructor. This is the preferred way of dependency injection as it makes the dependencies explicit and the object immutable.

𝟐. 𝐒𝐞𝐭𝐭𝐞𝐫 𝐈𝐧𝐣𝐞𝐜𝐭𝐢𝐨𝐧:
Setter injection uses setter methods to inject dependencies. This method provides flexibility but makes the object mutable.

User's avatar

Continue reading this post for free, courtesy of Suraj Mishra.

Or purchase a paid subscription.
© 2026 Suraj Mishra · Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture