<?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: Vidhya sunkara</title>
    <description>The latest articles on DEV Community by Vidhya sunkara (@vidhya_sunkara).</description>
    <link>https://dev.to/vidhya_sunkara</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4257585%2F2280fc7d-10ad-466e-84a5-85dec7bf4f57.png</url>
      <title>DEV Community: Vidhya sunkara</title>
      <link>https://dev.to/vidhya_sunkara</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vidhya_sunkara"/>
    <language>en</language>
    <item>
      <title>Understanding Spring Core: From IoC and Bean Lifecycle to Proxies and Transaction Management</title>
      <dc:creator>Vidhya sunkara</dc:creator>
      <pubDate>Sat, 21 Nov 2026 15:51:38 +0000</pubDate>
      <link>https://dev.to/vidhya_sunkara/understanding-spring-core-from-ioc-and-bean-lifecycle-to-proxies-and-transaction-management-37m9</link>
      <guid>https://dev.to/vidhya_sunkara/understanding-spring-core-from-ioc-and-bean-lifecycle-to-proxies-and-transaction-management-37m9</guid>
      <description>&lt;p&gt;Spring is one of the most widely used Java frameworks, but understanding Spring Boot becomes much easier when we first understand what's happening underneath the hood.&lt;/p&gt;

&lt;p&gt;Today, I revisited some important Spring Core concepts, starting with configuration and dependency injection, and gradually moving towards bean lifecycle, proxies, AOP, and transaction management.&lt;/p&gt;

&lt;p&gt;Here's what I learned.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Spring Configuration: Annotation-Based vs Java-Based
&lt;/h2&gt;

&lt;p&gt;Spring provides different ways to configure and manage application beans.&lt;/p&gt;

&lt;h3&gt;
  
  
  Annotation-Based Configuration
&lt;/h3&gt;

&lt;p&gt;In this approach, we use annotations to tell Spring which classes it should manage.&lt;/p&gt;

&lt;p&gt;Some commonly used annotations are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;@Component&lt;/code&gt; – Marks a class as a Spring-managed component.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@Service&lt;/code&gt; – Indicates a service-layer component.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@Repository&lt;/code&gt; – Indicates a persistence-layer component.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@Controller&lt;/code&gt; – Defines a Spring MVC controller.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@Autowired&lt;/code&gt; – Requests dependency injection.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@Qualifier&lt;/code&gt; – Specifies which bean should be injected when multiple candidates exist.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@ComponentScan&lt;/code&gt; – Tells Spring where to search for components.&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 java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PaymentService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Payment processing"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Java-Based Configuration
&lt;/h3&gt;

&lt;p&gt;Java-based configuration uses &lt;code&gt;@Configuration&lt;/code&gt; and &lt;code&gt;@Bean&lt;/code&gt; to explicitly define beans.&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="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppConfig&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Bean&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;PaymentService&lt;/span&gt; &lt;span class="nf"&gt;paymentService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PaymentService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach is particularly useful when working with third-party classes that we cannot modify to add Spring annotations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key takeaway:&lt;/strong&gt; Annotation-based configuration allows Spring to discover components, while Java-based configuration gives us explicit control over bean creation.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. IoC Container and Dependency Injection
&lt;/h2&gt;

&lt;p&gt;One of the fundamental principles of Spring is Inversion of Control (IoC).&lt;/p&gt;

&lt;p&gt;Traditionally, a class might create its own dependencies using the &lt;code&gt;new&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;Spring changes this approach.&lt;/p&gt;

&lt;p&gt;Instead of objects managing their own dependencies, the Spring IoC container creates and manages objects and supplies their dependencies.&lt;/p&gt;

&lt;p&gt;These Spring-managed objects are called &lt;strong&gt;beans&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The two important container interfaces are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;BeanFactory&lt;/code&gt; – Provides fundamental IoC and dependency injection functionality.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ApplicationContext&lt;/code&gt; – Extends &lt;code&gt;BeanFactory&lt;/code&gt; and provides additional capabilities such as event publishing, internationalization, and resource loading.&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 java"&gt;&lt;code&gt;&lt;span class="nc"&gt;ApplicationContext&lt;/span&gt; &lt;span class="n"&gt;context&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;AnnotationConfigApplicationContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AppConfig&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;PaymentService&lt;/span&gt; &lt;span class="n"&gt;paymentService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getBean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PaymentService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The container manages the bean rather than requiring us to instantiate it manually.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Eager and Lazy Initialization
&lt;/h2&gt;

&lt;p&gt;Spring singleton beans are eagerly initialized by default.&lt;/p&gt;

&lt;p&gt;This means Spring generally creates singleton beans during application context initialization.&lt;/p&gt;

&lt;p&gt;However, we can defer bean creation using &lt;code&gt;@Lazy&lt;/code&gt;.&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;@Lazy&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PaymentService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;PaymentService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"PaymentService initialized"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The bean is created when it is first requested.&lt;/p&gt;

&lt;p&gt;An interesting scenario occurs when an eager singleton depends on a lazy bean.&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="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;PaymentService&lt;/span&gt; &lt;span class="n"&gt;paymentService&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though &lt;code&gt;PaymentService&lt;/code&gt; is marked &lt;code&gt;@Lazy&lt;/code&gt;, Spring needs to resolve it while creating &lt;code&gt;OrderService&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Therefore, the dependency can still be instantiated during application startup.&lt;/p&gt;

&lt;p&gt;We can also use &lt;code&gt;@Lazy&lt;/code&gt; at an injection point to inject a lazy-resolution proxy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key takeaway:&lt;/strong&gt; &lt;code&gt;@Lazy&lt;/code&gt; delays instantiation, not bean registration.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Understanding Spring Bean Scopes
&lt;/h2&gt;

&lt;p&gt;Bean scope determines how Spring creates and shares bean instances.&lt;/p&gt;

&lt;p&gt;The important scopes are:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Scope&lt;/th&gt;
&lt;th&gt;Behaviour&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Singleton&lt;/td&gt;
&lt;td&gt;One instance per Spring IoC container&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Prototype&lt;/td&gt;
&lt;td&gt;A new instance for each bean request&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Request&lt;/td&gt;
&lt;td&gt;One instance per HTTP request&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Session&lt;/td&gt;
&lt;td&gt;One instance per HTTP session&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Application&lt;/td&gt;
&lt;td&gt;One instance per ServletContext&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

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

&lt;h3&gt;
  
  
  An interesting scenario: Prototype inside Singleton
&lt;/h3&gt;

&lt;p&gt;Consider:&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="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"prototype"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PaymentService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;PaymentService&lt;/span&gt; &lt;span class="n"&gt;paymentService&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;PaymentService&lt;/span&gt; &lt;span class="nf"&gt;getPaymentService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;paymentService&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though &lt;code&gt;PaymentService&lt;/code&gt; has prototype scope, injecting it directly into a singleton &lt;code&gt;OrderService&lt;/code&gt; does not create a fresh instance every time the getter is called.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Because Spring creates the singleton &lt;code&gt;OrderService&lt;/code&gt; once and injects the prototype dependency during its creation.&lt;/p&gt;

&lt;p&gt;The same reference is then returned by the getter.&lt;/p&gt;

&lt;p&gt;To obtain a fresh prototype instance whenever needed, we can use &lt;code&gt;ObjectProvider&amp;lt;PaymentService&amp;gt;&lt;/code&gt; and call &lt;code&gt;getObject()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This was an important distinction between bean scope and normal Java object references.&lt;/p&gt;

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

&lt;p&gt;Spring manages beans throughout their lifecycle.&lt;/p&gt;

&lt;p&gt;The major stages are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Instantiation – Spring creates the bean.&lt;/li&gt;
&lt;li&gt;Dependency Injection – Spring injects its dependencies.&lt;/li&gt;
&lt;li&gt;Aware callbacks – Applicable awareness interfaces are invoked.&lt;/li&gt;
&lt;li&gt;Before Initialization – BeanPostProcessor executes.&lt;/li&gt;
&lt;li&gt;Initialization – Methods such as &lt;code&gt;@PostConstruct&lt;/code&gt; execute.&lt;/li&gt;
&lt;li&gt;After Initialization – BeanPostProcessor executes again.&lt;/li&gt;
&lt;li&gt;Bean Ready for Use – The bean is available to the application.&lt;/li&gt;
&lt;li&gt;Destruction – Spring invokes applicable destruction callbacks when the container shuts down.&lt;/li&gt;
&lt;/ol&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;@Component&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Constructor"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@PostConstruct&lt;/span&gt;
    &lt;span class="k"&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="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Initialized"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@PreDestroy&lt;/span&gt;
    &lt;span class="k"&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="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Destroyed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The constructor runs first, followed by dependency injection and initialization callbacks.&lt;/p&gt;

&lt;p&gt;When the application context closes, Spring invokes the destruction callback for managed singleton beans.&lt;/p&gt;

&lt;p&gt;An important detail: Spring does not automatically invoke destruction callbacks for prototype beans.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. BeanPostProcessor
&lt;/h2&gt;

&lt;p&gt;A BeanPostProcessor allows us to customize beans during their lifecycle.&lt;/p&gt;

&lt;p&gt;It provides two important 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="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;BeanPostProcessor&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kt"&gt;Object&lt;/span&gt; &lt;span class="nf"&gt;postProcessBeforeInitialization&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="kt"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;bean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;beanName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;Object&lt;/span&gt; &lt;span class="nf"&gt;postProcessAfterInitialization&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="kt"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;bean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;beanName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first method executes before initialization callbacks such as &lt;code&gt;@PostConstruct&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The second executes after initialization callbacks.&lt;/p&gt;

&lt;p&gt;The execution sequence is:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constructor → Dependency Injection → Before Initialization → @PostConstruct → After Initialization&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Why is this important?&lt;/p&gt;

&lt;p&gt;Spring uses infrastructure involving BeanPostProcessors to implement several advanced features, including the creation of proxies for eligible beans.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Understanding Proxies in Spring
&lt;/h2&gt;

&lt;p&gt;A proxy is an intermediary object that controls access to another object.&lt;/p&gt;

&lt;p&gt;Instead of directly invoking the actual service object, our application can invoke a proxy.&lt;/p&gt;

&lt;p&gt;The proxy can execute additional logic before or after forwarding the method call.&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 plaintext"&gt;&lt;code&gt;OrderService
     |
     v
PaymentService Proxy
     |
     |-- Logging
     |-- Security checks
     |-- Transaction handling
     |
     v
Actual PaymentService
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spring commonly uses two types of proxies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JDK Dynamic Proxy – Uses Java interfaces.&lt;/li&gt;
&lt;li&gt;CGLIB Proxy – Creates a subclass of the target class.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Proxies are an important foundation for understanding Spring AOP.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Spring AOP and Transaction Management
&lt;/h2&gt;

&lt;p&gt;Aspect-Oriented Programming (AOP) helps separate cross-cutting concerns from business logic.&lt;/p&gt;

&lt;p&gt;Examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Logging&lt;/li&gt;
&lt;li&gt;Security&lt;/li&gt;
&lt;li&gt;Transaction management&lt;/li&gt;
&lt;li&gt;Caching&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Consider a money transfer operation:&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="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AccountService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Transactional&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;transferMoney&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;debitAccount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nf"&gt;creditAccount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We don't need to manually write transaction-handling code inside every business method.&lt;/p&gt;

&lt;p&gt;Spring's transaction infrastructure can manage the transaction through a proxy.&lt;/p&gt;

&lt;p&gt;The flow is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A method call reaches the transaction proxy.&lt;/li&gt;
&lt;li&gt;Spring starts a transaction.&lt;/li&gt;
&lt;li&gt;The actual business method executes.&lt;/li&gt;
&lt;li&gt;If successful, Spring commits the transaction.&lt;/li&gt;
&lt;li&gt;If an applicable exception occurs, Spring rolls back the transaction.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The database executes the SQL operations and enforces transactional guarantees, while Spring manages transaction boundaries.&lt;/p&gt;

&lt;h3&gt;
  
  
  What happens when something fails?
&lt;/h3&gt;

&lt;p&gt;Suppose &lt;code&gt;debitAccount()&lt;/code&gt; succeeds but &lt;code&gt;creditAccount()&lt;/code&gt; throws a runtime exception.&lt;/p&gt;

&lt;p&gt;If both operations participate in the same transaction, Spring rolls back the transaction.&lt;/p&gt;

&lt;p&gt;This prevents the debit from being committed independently.&lt;/p&gt;

&lt;p&gt;By default, Spring rolls back on unchecked exceptions (&lt;code&gt;RuntimeException&lt;/code&gt;) and &lt;code&gt;Error&lt;/code&gt;, but not on checked exceptions. We can customize this using &lt;code&gt;rollbackFor&lt;/code&gt; and &lt;code&gt;noRollbackFor&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. What I Took Away From Today's Learning
&lt;/h2&gt;

&lt;p&gt;The most valuable part of today's revision was understanding how these concepts connect.&lt;/p&gt;

&lt;p&gt;They are not isolated Spring features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;IoC provides control over object creation.&lt;/li&gt;
&lt;li&gt;Dependency Injection connects application components.&lt;/li&gt;
&lt;li&gt;Bean scopes determine object instance management.&lt;/li&gt;
&lt;li&gt;Bean lifecycle defines how Spring initializes and destroys beans.&lt;/li&gt;
&lt;li&gt;BeanPostProcessor provides lifecycle extension points.&lt;/li&gt;
&lt;li&gt;Proxies intercept method calls.&lt;/li&gt;
&lt;li&gt;AOP uses interception to implement cross-cutting concerns.&lt;/li&gt;
&lt;li&gt;Transaction management uses these mechanisms to manage database transactions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding these fundamentals makes Spring Boot much less mysterious.&lt;/p&gt;

&lt;p&gt;Instead of simply memorizing annotations, I want to understand what Spring actually does behind the scenes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The goal is not just to write Spring Boot applications, but to understand how they work internally.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>springframework</category>
    </item>
  </channel>
</rss>
