<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Manujaya Rathnayake on Medium]]></title>
        <description><![CDATA[Stories by Manujaya Rathnayake on Medium]]></description>
        <link>https://medium.com/@janithrathnayake01?source=rss-548f30477756------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*10Y5T1XK-Dx5YVrt</url>
            <title>Stories by Manujaya Rathnayake on Medium</title>
            <link>https://medium.com/@janithrathnayake01?source=rss-548f30477756------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 09:22:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@janithrathnayake01/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[What is spring IOC and Di]]></title>
            <link>https://medium.com/@janithrathnayake01/what-is-spring-ioc-and-di-254b0e926c30?source=rss-548f30477756------2</link>
            <guid isPermaLink="false">https://medium.com/p/254b0e926c30</guid>
            <dc:creator><![CDATA[Manujaya Rathnayake]]></dc:creator>
            <pubDate>Tue, 01 Jul 2025 15:08:20 GMT</pubDate>
            <atom:updated>2025-07-01T15:25:05.147Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yvN3GqrtMCq32ynIiqUPxg.jpeg" /></figure><p>✦<strong>Spring IOC</strong>: It is called <strong>Inversion Of Control</strong>. it mean design principle where the control of <strong>object creation</strong> and <strong>management.<br></strong>so it is inverted from the application code to the <strong>spring container.</strong></p><p>Typically, programmers are responsible for creating, maintaining, and destroying objects manually. They don’t focus on basic business logic.</p><p>therefore that reason to achieve this spring provide <strong>IOC containers</strong><br>Simply put (<strong><em>you do not need to create object manually, spring will do it</em></strong>)</p><figure><img alt="IOC container" src="https://cdn-images-1.medium.com/max/271/1*HtyNAhpDfo3_1psLQ4-sHQ.png" /></figure><blockquote>❓<strong><em>How exactly you will make it work?<br></em></strong>To achieve this we need to use design pattern. it is <strong>Dependency Injection<br></strong>It is the <strong>actual implementation</strong> if IOC spring.<br>Simply put, Instead of a class creating its own dependencies, Spring provides those dependencies from the outside.</blockquote><p>🖥️E.g.:-<br>Think you have a laptop . laptop depend on CPU. You cannot run laptop without CPU</p><pre>class Cpu{<br>  public void process(){<br>  System.out.println(&quot;spu is processing&quot;);<br> }<br>}</pre><pre>class CPU {<br>    public void process() {<br>        System.out.println(&quot;CPU is processing...&quot;);<br>    }<br>}<br><br>class Laptop {<br>    private CPU cpu;<br><br>    // Dependency Injection (constructor injection)<br>    public Laptop(CPU cpu) {<br>        this.cpu = cpu;<br>    }<br><br>    public void start() {<br>        cpu.process();<br>        System.out.println(&quot;Laptop is starting&quot;);<br>    }<br>}</pre><p>💡Here’s what happening:<br> ▪️<strong>Laptop </strong>depend on <strong>CPU<br> </strong>▪️Instead of creating a <strong>CPU</strong> object inside the <strong>Laptop</strong> class, we inject it<br> from the outside using a constructor</p><p>✅ That is the concept of <strong>Dependency Injection.</strong></p><p>There are 3 types of Dependency Injection<br>▪️<strong>Constructor injection</strong><br>▪️<strong>Setter injection</strong><br>▪️<strong>Field Injection</strong></p><p>▪️<strong>Constructor injection<br> </strong>When an object is constructed, dependencies are injected from the outside rather than being created within the class itself.</p><pre>@Component<br>class CPU {<br>    public void process() {<br>        System.out.println(&quot;CPU is processing&quot;);<br>    }<br>}<br><br>@Component<br>class Laptop {<br><br>    private final CPU cpu;<br><br>    @Autowired <br>    public Laptop(CPU cpu) {<br>        this.cpu = cpu;<br>    }<br><br>    public void start() {<br>        cpu.process();<br>        System.out.println(&quot;Laptop is starting...&quot;);<br>    }<br>}</pre><p>▪️<strong>Setter injection</strong><br>Spring injects dependencies through setters <strong>methods</strong> of a class, after the object is constructed.</p><pre>@Component<br>class CPU {<br>    public void process() {<br>        System.out.println(&quot;CPU is processing&quot;);<br>    }<br>}<br><br>@Component<br>class Laptop {<br><br>    private CPU cpu;<br><br>    // Setter method<br>    @Autowired<br>    public void setCpu(CPU cpu) {<br>        this.cpu = cpu;<br>    }<br><br>    public void start() {<br>        cpu.process();<br>        System.out.println(&quot;Laptop is starting&quot;);<br>    }<br>}</pre><h3>🔍 How it works</h3><p>▫️Spring creates the <strong>Laptop</strong> object first<br>▫️Then it finds the <strong>CPU</strong> bean<br>▫️It calls the <strong>setCpu()</strong> method to <strong>inject the CPU dependency</strong></p><p><strong>▪️Field Injection<br></strong> Spring <strong>directly injects the dependency into a field (variable)</strong> using the<br><strong> @ AutoWired</strong> annotation. This injection type not recommended for real project.<br>because,<br> — Doesn’t work well with final fields<br> — Break encapsulation</p><pre>@Component<br>class Laptop {<br><br>    @Autowired<br>    private CPU cpu;<br><br>    public void start() {<br>        cpu.process();<br>        System.out.println(&quot;Laptop is starting...&quot;);<br>    }<br>}</pre><p>🔄Main class to run App</p><pre>@SpringBootApplication<br>public class App {<br>    public static void main(String[] args) {<br>        //intialize spring ApplicationContext (ioc container)<br>        ApplicationContext context = SpringApplication.run(App.class, args);<br>        Laptop laptop = context.getBean(Laptop.class);<br>        laptop.start();<br>    }<br>}</pre><p><strong>SpringApplication.run — </strong>Activate your spring framework</p><p>⭕️In this spring you can imagine <strong>@ symbol </strong>with<strong> special name. </strong>it is<strong> “Annotation”<br>▪️Annotation </strong>special kind of <strong>metadata</strong> that you can add to your Java code<br> (classes, methods, variables) using <strong>@ symbol.<br>▪️</strong>Annotations tell the <strong>compiler</strong> or <strong>framework</strong> (like Spring) to perform<br> <strong>some behavior</strong> at <strong>compile time</strong>, <strong>runtime</strong>, or even <strong>during build</strong>.</p><p>❓Why use annotation<br> 🔺Make code more readable, declarative, and maintainable<br> 🔺They reduce boilerplate code</p><p>❇️Common annotation in spring<br><strong>@Component</strong> ➔ mark a class as a spring managed bean { getBean() }<br><strong>@AutoWired</strong> ➔ automatically inject a bean (object) into a class<br><strong>@Bean</strong> ➔ defines a method that returns a bean to be managed by Spring<br><strong>@Configuratio</strong>n ➔ declares a class with manual Spring bean definitions.<br><strong>@Service</strong> ➔ specialized <em>@ Componet</em> for service classes<br><strong>@Repository</strong> ➔specialized <em>@Component</em> for classes involving database access<br><strong>@Qualifier</strong> ➔ Used with <strong>@ AutoWired</strong> to choose a specific bean</p><p>🔗Please share this article with other programmers if you found it useful!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=254b0e926c30" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>