<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: mohanathas holins</title>
    <description>The latest articles on DEV Community by mohanathas holins (@holibhai).</description>
    <link>https://dev.to/holibhai</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3849309%2F3c9e5b2f-e100-4b43-926d-585a9580e12d.jpg</url>
      <title>DEV Community: mohanathas holins</title>
      <link>https://dev.to/holibhai</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/holibhai"/>
    <language>en</language>
    <item>
      <title>Deep Dive into the Spring IoC Container: Unmasking BeanFactory and ApplicationContext</title>
      <dc:creator>mohanathas holins</dc:creator>
      <pubDate>Tue, 09 Jun 2026 11:17:29 +0000</pubDate>
      <link>https://dev.to/holibhai/deep-dive-into-the-spring-ioc-container-unmasking-beanfactory-and-applicationcontext-5gd4</link>
      <guid>https://dev.to/holibhai/deep-dive-into-the-spring-ioc-container-unmasking-beanfactory-and-applicationcontext-5gd4</guid>
      <description>&lt;h1&gt;
  
  
  Why Every Spring Boot Developer Must Understand the IoC Container
&lt;/h1&gt;

&lt;p&gt;In the modern era of software development, abstracting away complexity has never been easier.&lt;/p&gt;

&lt;p&gt;With AI assistants ready to generate code blocks at a moment's notice, anyone can quickly build a working CRUD application using Spring Boot. You annotate a class with &lt;code&gt;@RestController&lt;/code&gt;, add a constructor, connect a service, and the API works.&lt;/p&gt;

&lt;p&gt;That first successful response from Postman gives an instant dopamine hit.&lt;/p&gt;

&lt;p&gt;But there is a hidden trap in this &lt;strong&gt;"code-first, fundamentals-later"&lt;/strong&gt; approach.&lt;/p&gt;

&lt;p&gt;When your application grows, or when an unexpected production bug appears, blindly patching code with trial and error is not enough. To truly master Spring Boot, you need to step away from just writing code and understand the engine behind it:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Spring IoC Container.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this article, we will break down the core architecture that powers every Spring Boot application.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. The Problem with Manual Object Management
&lt;/h2&gt;

&lt;p&gt;Before understanding Spring, let’s start with plain Java.&lt;/p&gt;

&lt;p&gt;In standard Object-Oriented Programming, you are responsible for creating and managing objects manually.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;studentId&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Student&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;studentId&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;studentId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;studentId&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getStudentId&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;studentId&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setStudentId&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;studentId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;studentId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;studentId&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let’s create a &lt;code&gt;Student&lt;/code&gt; object manually:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="nc"&gt;Student&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"John Doe"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is normal Java object creation.&lt;/p&gt;

&lt;p&gt;But here is the issue:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Student&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"John Doe"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;Main&lt;/code&gt; class is now directly responsible for creating the &lt;code&gt;Student&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;That means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your code controls object creation.&lt;/li&gt;
&lt;li&gt;Your code controls object configuration.&lt;/li&gt;
&lt;li&gt;Your code controls dependency creation.&lt;/li&gt;
&lt;li&gt;Your code controls the object lifecycle.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This may look simple in a small application. But in a real enterprise application, objects depend on many other objects.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;UserRepository&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Database&lt;/span&gt; &lt;span class="nc"&gt;Connection&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you manually create and connect everything using &lt;code&gt;new&lt;/code&gt;, your code becomes tightly coupled and difficult to maintain.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. What Is Inversion of Control?
&lt;/h2&gt;

&lt;p&gt;Spring solves this problem using &lt;strong&gt;Inversion of Control&lt;/strong&gt;, commonly known as &lt;strong&gt;IoC&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In simple words:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Instead of you creating objects manually, Spring creates and manages them for you.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This means the control is inverted.&lt;/p&gt;

&lt;p&gt;In plain Java:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You are creating the object.&lt;/p&gt;

&lt;p&gt;In Spring Boot:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spring creates the object and manages it inside its container.&lt;/p&gt;

&lt;p&gt;That is the basic idea of IoC.&lt;/p&gt;

&lt;p&gt;The core principle is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Spring Boot takes responsibility for creating, configuring, connecting, and managing objects throughout the application.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This makes your application:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;More loosely coupled&lt;/li&gt;
&lt;li&gt;Easier to test&lt;/li&gt;
&lt;li&gt;Easier to maintain&lt;/li&gt;
&lt;li&gt;Cleaner in structure&lt;/li&gt;
&lt;li&gt;Better for large-scale development&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  3. What Is a Spring Bean?
&lt;/h2&gt;

&lt;p&gt;In Java, we usually say &lt;strong&gt;object&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In Spring, we say &lt;strong&gt;bean&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But what is the difference?&lt;/p&gt;

&lt;h3&gt;
  
  
  Java Object
&lt;/h3&gt;

&lt;p&gt;A Java object is created manually by you using the &lt;code&gt;new&lt;/code&gt; keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Student&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spring does not know about this object.&lt;/p&gt;

&lt;p&gt;It is just a normal Java object.&lt;/p&gt;

&lt;h3&gt;
  
  
  Spring Bean
&lt;/h3&gt;

&lt;p&gt;A Spring Bean is an object created, configured, and managed by the Spring IoC Container.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;UserService&lt;/code&gt; becomes a Spring Bean because Spring detects it during component scanning and registers it inside the application context.&lt;/p&gt;

&lt;p&gt;So the simple difference is:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Created By&lt;/th&gt;
&lt;th&gt;Managed By Spring?&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Java Object&lt;/td&gt;
&lt;td&gt;Developer&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Spring Bean&lt;/td&gt;
&lt;td&gt;Spring Container&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A bean is not just an object. It is an object whose lifecycle is fully managed by Spring.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. What Is Dependency Injection?
&lt;/h2&gt;

&lt;p&gt;If &lt;strong&gt;Inversion of Control&lt;/strong&gt; is the concept, then &lt;strong&gt;Dependency Injection&lt;/strong&gt; is the technique used to implement it.&lt;/p&gt;

&lt;p&gt;Dependency Injection means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Instead of a class creating its dependencies, Spring provides those dependencies from outside.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s say &lt;code&gt;UserController&lt;/code&gt; needs &lt;code&gt;UserService&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Without Spring:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;UserController&lt;/code&gt; is tightly coupled with &lt;code&gt;UserService&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With Spring:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RestController&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;UserController&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;userService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;UserController&lt;/code&gt; does not create &lt;code&gt;UserService&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Spring injects it automatically.&lt;/p&gt;

&lt;p&gt;This is Dependency Injection.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Types of Dependency Injection in Spring Boot
&lt;/h2&gt;

&lt;p&gt;Spring provides three main ways to inject dependencies.&lt;/p&gt;




&lt;h3&gt;
  
  
  5.1 Constructor Injection
&lt;/h3&gt;

&lt;p&gt;This is the recommended and industry-standard approach.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RestController&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;UserController&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;userService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why constructor injection is preferred:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It supports &lt;code&gt;final&lt;/code&gt; fields.&lt;/li&gt;
&lt;li&gt;It makes dependencies clear.&lt;/li&gt;
&lt;li&gt;It prevents incomplete object creation.&lt;/li&gt;
&lt;li&gt;It improves testability.&lt;/li&gt;
&lt;li&gt;It avoids hidden dependencies.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the best option for production-level Spring Boot applications.&lt;/p&gt;




&lt;h3&gt;
  
  
  5.2 Setter Injection
&lt;/h3&gt;

&lt;p&gt;In setter injection, dependencies are injected through setter methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RestController&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setUserService&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;userService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Setter injection is useful when the dependency is optional or changeable.&lt;/p&gt;

&lt;p&gt;However, it makes the object mutable because the dependency can be changed after object creation.&lt;/p&gt;




&lt;h3&gt;
  
  
  5.3 Field Injection
&lt;/h3&gt;

&lt;p&gt;Field injection directly injects the dependency into the field.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RestController&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This looks simple, but it is not recommended for production code.&lt;/p&gt;

&lt;p&gt;Problems with field injection:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dependencies are hidden.&lt;/li&gt;
&lt;li&gt;Unit testing becomes harder.&lt;/li&gt;
&lt;li&gt;It tightly couples your class to the Spring framework.&lt;/li&gt;
&lt;li&gt;You cannot use &lt;code&gt;final&lt;/code&gt; fields.&lt;/li&gt;
&lt;li&gt;The object can be created in an incomplete state.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, as a best practice, use constructor injection.&lt;/p&gt;




&lt;h2&gt;
  
  
  6. BeanFactory vs ApplicationContext
&lt;/h2&gt;

&lt;p&gt;The Spring IoC container is mainly represented by two interfaces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BeanFactory
    ↑
ApplicationContext
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ApplicationContext&lt;/code&gt; extends &lt;code&gt;BeanFactory&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Both are containers, but they are not the same.&lt;/p&gt;




&lt;h3&gt;
  
  
  BeanFactory
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;BeanFactory&lt;/code&gt; is the basic IoC container.&lt;/p&gt;

&lt;p&gt;It provides the core functionality for creating and managing beans.&lt;/p&gt;

&lt;p&gt;Important characteristics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lightweight&lt;/li&gt;
&lt;li&gt;Lazy initialization&lt;/li&gt;
&lt;li&gt;Creates beans only when requested&lt;/li&gt;
&lt;li&gt;Basic dependency injection support&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ApplicationContext
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;ApplicationContext&lt;/code&gt; is the advanced container used in modern Spring Boot applications.&lt;/p&gt;

&lt;p&gt;It provides everything &lt;code&gt;BeanFactory&lt;/code&gt; provides, plus additional enterprise features.&lt;/p&gt;

&lt;p&gt;Important characteristics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Eagerly creates singleton beans during startup&lt;/li&gt;
&lt;li&gt;Supports internationalization&lt;/li&gt;
&lt;li&gt;Supports event publishing&lt;/li&gt;
&lt;li&gt;Supports AOP integration&lt;/li&gt;
&lt;li&gt;Supports application environment and profiles&lt;/li&gt;
&lt;li&gt;Used by Spring Boot applications by default&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  BeanFactory vs ApplicationContext
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;BeanFactory&lt;/th&gt;
&lt;th&gt;ApplicationContext&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Bean creation&lt;/td&gt;
&lt;td&gt;Lazy&lt;/td&gt;
&lt;td&gt;Mostly eager for singleton beans&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Memory usage&lt;/td&gt;
&lt;td&gt;Lightweight&lt;/td&gt;
&lt;td&gt;Heavier&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dependency injection&lt;/td&gt;
&lt;td&gt;Supported&lt;/td&gt;
&lt;td&gt;Supported&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;AOP support&lt;/td&gt;
&lt;td&gt;Manual setup needed&lt;/td&gt;
&lt;td&gt;Built-in support&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Event publishing&lt;/td&gt;
&lt;td&gt;Not supported&lt;/td&gt;
&lt;td&gt;Supported&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Internationalization&lt;/td&gt;
&lt;td&gt;Not supported&lt;/td&gt;
&lt;td&gt;Supported&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Common Spring Boot usage&lt;/td&gt;
&lt;td&gt;Rare&lt;/td&gt;
&lt;td&gt;Default&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In modern Spring Boot applications, we usually work with &lt;code&gt;ApplicationContext&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  7. What Happens When a Spring Boot Application Starts?
&lt;/h2&gt;

&lt;p&gt;Every Spring Boot application starts from the &lt;code&gt;main&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@SpringBootApplication&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MentifyApplication&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;SpringApplication&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MentifyApplication&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This line is very important:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;SpringApplication&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MentifyApplication&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is where the Spring Boot application starts.&lt;/p&gt;

&lt;p&gt;Behind this single line, many things happen.&lt;/p&gt;




&lt;h2&gt;
  
  
  8. Spring Boot Startup Flow
&lt;/h2&gt;

&lt;p&gt;Here is the high-level startup flow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JVM Starts
    ↓
main() method runs
    ↓
SpringApplication.run()
    ↓
Environment is prepared
    ↓
ApplicationContext is created
    ↓
Component scanning happens
    ↓
Bean definitions are registered
    ↓
Beans are created
    ↓
Dependencies are injected
    ↓
Auto-configuration is applied
    ↓
Embedded server starts
    ↓
Application is ready
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s understand this step by step.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 1: JVM Starts
&lt;/h3&gt;

&lt;p&gt;The JVM starts and calls the &lt;code&gt;main()&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;SpringApplication&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MentifyApplication&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Step 2: SpringApplication.run() Executes
&lt;/h3&gt;

&lt;p&gt;Spring Boot begins the application bootstrap process.&lt;/p&gt;

&lt;p&gt;This is where a normal Java application starts becoming a Spring-powered application.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 3: Environment Is Prepared
&lt;/h3&gt;

&lt;p&gt;Spring loads configuration details such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;application.properties&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;application.yml&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Active profiles&lt;/li&gt;
&lt;li&gt;Environment variables&lt;/li&gt;
&lt;li&gt;System properties&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight properties"&gt;&lt;code&gt;&lt;span class="py"&gt;server.port&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;8080&lt;/span&gt;
&lt;span class="py"&gt;spring.profiles.active&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Step 4: ApplicationContext Is Created
&lt;/h3&gt;

&lt;p&gt;Spring creates the correct type of &lt;code&gt;ApplicationContext&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For a web application, Spring Boot commonly uses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AnnotationConfigServletWebServerApplicationContext
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This context manages your beans and also starts the embedded web server.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 5: Component Scanning Happens
&lt;/h3&gt;

&lt;p&gt;Spring scans your project to find classes annotated with stereotypes like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="nd"&gt;@Repository&lt;/span&gt;
&lt;span class="nd"&gt;@Controller&lt;/span&gt;
&lt;span class="nd"&gt;@RestController&lt;/span&gt;
&lt;span class="nd"&gt;@Configuration&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spring detects this class and registers it as a bean.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 6: Bean Creation and Dependency Injection
&lt;/h3&gt;

&lt;p&gt;Spring creates bean objects and injects dependencies.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RestController&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;UserController&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;userService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spring creates &lt;code&gt;UserService&lt;/code&gt; first, then injects it into &lt;code&gt;UserController&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 7: Auto-Configuration Is Applied
&lt;/h3&gt;

&lt;p&gt;Spring Boot checks your classpath and automatically configures required components.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;If Spring Boot finds web dependencies, it configures:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Embedded Tomcat&lt;/li&gt;
&lt;li&gt;DispatcherServlet&lt;/li&gt;
&lt;li&gt;JSON conversion&lt;/li&gt;
&lt;li&gt;Web MVC setup&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If Spring Boot finds database dependencies, it configures:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;DataSource&lt;/li&gt;
&lt;li&gt;EntityManagerFactory&lt;/li&gt;
&lt;li&gt;TransactionManager&lt;/li&gt;
&lt;li&gt;JPA repositories&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is why Spring Boot feels magical.&lt;/p&gt;

&lt;p&gt;But it is not magic.&lt;/p&gt;

&lt;p&gt;It is conditional auto-configuration.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 8: Embedded Server Starts
&lt;/h3&gt;

&lt;p&gt;Spring Boot starts the embedded server, such as Tomcat.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Tomcat started on port 8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now your application can receive HTTP requests.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 9: Application Is Ready
&lt;/h3&gt;

&lt;p&gt;Finally, Spring publishes an application ready event.&lt;/p&gt;

&lt;p&gt;At this point, your application is fully started and ready to handle requests.&lt;/p&gt;




&lt;h2&gt;
  
  
  9. Spring Bean Lifecycle
&lt;/h2&gt;

&lt;p&gt;A Spring Bean is not simply created and forgotten.&lt;/p&gt;

&lt;p&gt;It goes through a lifecycle.&lt;/p&gt;

&lt;p&gt;The main stages are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bean Definition
    ↓
Instantiation
    ↓
Dependency Injection
    ↓
Initialization
    ↓
Ready to Use
    ↓
Destruction
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  9.1 Bean Definition
&lt;/h3&gt;

&lt;p&gt;Spring first identifies metadata about the bean.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spring understands that &lt;code&gt;UserService&lt;/code&gt; should be managed as a bean.&lt;/p&gt;




&lt;h3&gt;
  
  
  9.2 Instantiation
&lt;/h3&gt;

&lt;p&gt;Spring creates the object in memory.&lt;/p&gt;

&lt;p&gt;This is similar to calling:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But instead of you doing it manually, Spring does it.&lt;/p&gt;




&lt;h3&gt;
  
  
  9.3 Dependency Injection
&lt;/h3&gt;

&lt;p&gt;Spring injects required dependencies into the bean.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;UserRepository&lt;/span&gt; &lt;span class="n"&gt;userRepository&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;UserService&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;UserRepository&lt;/span&gt; &lt;span class="n"&gt;userRepository&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;userRepository&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;userRepository&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  9.4 Initialization
&lt;/h3&gt;

&lt;p&gt;After dependencies are injected, Spring allows custom initialization logic.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@PostConstruct&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"UserService initialized"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method runs after the bean is created and dependencies are injected.&lt;/p&gt;




&lt;h3&gt;
  
  
  9.5 Ready to Use
&lt;/h3&gt;

&lt;p&gt;Now the bean is fully ready and can be used inside the application.&lt;/p&gt;




&lt;h3&gt;
  
  
  9.6 Destruction
&lt;/h3&gt;

&lt;p&gt;When the application shuts down, Spring destroys the bean.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@PreDestroy&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;destroy&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"UserService destroyed"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is useful for cleanup operations such as closing resources.&lt;/p&gt;




&lt;h2&gt;
  
  
  10. Bean Scopes in Spring
&lt;/h2&gt;

&lt;p&gt;Bean scope defines how long a bean lives and how many instances Spring creates.&lt;/p&gt;

&lt;p&gt;Spring supports several scopes.&lt;/p&gt;




&lt;h3&gt;
  
  
  10.1 Singleton Scope
&lt;/h3&gt;

&lt;p&gt;Singleton is the default scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spring creates only one object of this bean for the entire application.&lt;/p&gt;

&lt;p&gt;Every class that needs this bean gets the same instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UserController ---&amp;gt; same UserService object
AdminController ---&amp;gt; same UserService object
OrderController ---&amp;gt; same UserService object
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the most common scope in Spring Boot.&lt;/p&gt;




&lt;h3&gt;
  
  
  10.2 Prototype Scope
&lt;/h3&gt;

&lt;p&gt;Prototype scope creates a new object every time the bean is requested.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="nd"&gt;@Scope&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"prototype"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ReportGenerator&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each request to the container creates a new instance.&lt;/p&gt;




&lt;h3&gt;
  
  
  10.3 Request Scope
&lt;/h3&gt;

&lt;p&gt;Request scope is used in web applications.&lt;/p&gt;

&lt;p&gt;A new bean instance is created for each HTTP request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="nd"&gt;@Scope&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"request"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RequestData&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  10.4 Session Scope
&lt;/h3&gt;

&lt;p&gt;Session scope creates one bean per user session.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="nd"&gt;@Scope&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"session"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserSession&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  10.5 Application Scope
&lt;/h3&gt;

&lt;p&gt;Application scope creates one bean for the entire servlet context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="nd"&gt;@Scope&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"application"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppConfig&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  11. Component Scanning in Spring Boot
&lt;/h2&gt;

&lt;p&gt;The annotation &lt;code&gt;@SpringBootApplication&lt;/code&gt; is a combination of three important annotations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@SpringBootApplication&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Internally, it includes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Configuration&lt;/span&gt;
&lt;span class="nd"&gt;@EnableAutoConfiguration&lt;/span&gt;
&lt;span class="nd"&gt;@ComponentScan&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s understand them.&lt;/p&gt;




&lt;h3&gt;
  
  
  @Configuration
&lt;/h3&gt;

&lt;p&gt;This tells Spring that the class can define bean configurations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Configuration&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppConfig&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  @EnableAutoConfiguration
&lt;/h3&gt;

&lt;p&gt;This enables Spring Boot’s auto-configuration mechanism.&lt;/p&gt;

&lt;p&gt;Spring Boot checks the dependencies in your project and automatically configures matching features.&lt;/p&gt;




&lt;h3&gt;
  
  
  @ComponentScan
&lt;/h3&gt;

&lt;p&gt;This tells Spring to scan the current package and its sub-packages for components.&lt;/p&gt;

&lt;p&gt;Example project structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;com.mentify
 ├── MentifyApplication.java
 ├── user
 │   ├── controller
 │   │   └── UserController.java
 │   └── service
 │       └── UserService.java
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;MentifyApplication.java&lt;/code&gt; is inside the &lt;code&gt;com.mentify&lt;/code&gt; package, Spring scans:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;com.mentify
com.mentify.user
com.mentify.user.controller
com.mentify.user.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So it will detect:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RestController&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And also:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Service&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  12. Bean Name and Bean Type
&lt;/h2&gt;

&lt;p&gt;When Spring detects a component, it registers it with two important details:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bean Name
Bean Type
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RestController&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spring registers it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bean Name: userController
Bean Type: com.mentify.user.controller.UserController
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, Spring uses the class name with the first letter converted to lowercase.&lt;/p&gt;

&lt;p&gt;So:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;UserController&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;userController&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is how Spring identifies and manages beans internally.&lt;/p&gt;




&lt;h2&gt;
  
  
  13. Why This Matters for Real Projects
&lt;/h2&gt;

&lt;p&gt;When you are building a small CRUD application, you can survive without understanding these concepts deeply.&lt;/p&gt;

&lt;p&gt;But when you build production-level systems, you will face problems like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Circular dependencies&lt;/li&gt;
&lt;li&gt;Bean creation errors&lt;/li&gt;
&lt;li&gt;Missing bean exceptions&lt;/li&gt;
&lt;li&gt;Incorrect package scanning&lt;/li&gt;
&lt;li&gt;Lazy vs eager initialization issues&lt;/li&gt;
&lt;li&gt;Transaction proxy problems&lt;/li&gt;
&lt;li&gt;Testing difficulties&lt;/li&gt;
&lt;li&gt;Configuration conflicts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you understand the IoC container, these problems become easier to debug.&lt;/p&gt;

&lt;p&gt;Instead of guessing, you can reason about what Spring is doing internally.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Spring Boot is not magic.&lt;/p&gt;

&lt;p&gt;It is a powerful framework built on clear principles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inversion of Control&lt;/li&gt;
&lt;li&gt;Dependency Injection&lt;/li&gt;
&lt;li&gt;Bean management&lt;/li&gt;
&lt;li&gt;ApplicationContext&lt;/li&gt;
&lt;li&gt;Component scanning&lt;/li&gt;
&lt;li&gt;Auto-configuration&lt;/li&gt;
&lt;li&gt;Bean lifecycle&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At the beginner level, it is normal to focus only on writing APIs and getting output quickly.&lt;/p&gt;

&lt;p&gt;But to grow as a strong backend developer, you need to understand what happens behind the scenes.&lt;/p&gt;

&lt;p&gt;Once you understand the Spring IoC Container, you stop writing code blindly.&lt;/p&gt;

&lt;p&gt;You start writing code with confidence.&lt;/p&gt;

&lt;p&gt;You understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Who creates the object&lt;/li&gt;
&lt;li&gt;Where the object lives&lt;/li&gt;
&lt;li&gt;How dependencies are injected&lt;/li&gt;
&lt;li&gt;When beans are initialized&lt;/li&gt;
&lt;li&gt;How Spring Boot starts&lt;/li&gt;
&lt;li&gt;Why certain errors happen&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is the point where you move from just using Spring Boot to truly understanding Spring Boot.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thought
&lt;/h2&gt;

&lt;p&gt;When I first started learning Spring Boot, I focused only on writing code and getting APIs to work.&lt;/p&gt;

&lt;p&gt;But later, I realized that understanding the fundamentals is what makes debugging, scaling, and writing production-level applications much easier.&lt;/p&gt;

&lt;p&gt;So before going deeper into advanced Spring Boot topics, take time to understand the IoC Container.&lt;/p&gt;

&lt;p&gt;It is the heart of Spring.&lt;/p&gt;

&lt;p&gt;What was your biggest learning moment when you moved from plain Java objects to Spring-managed beans?&lt;/p&gt;

</description>
      <category>softwaredevelopment</category>
      <category>springboot</category>
      <category>spring</category>
      <category>ioc</category>
    </item>
  </channel>
</rss>
