<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Badal Jha</title><description>Personal site and blog of Badal Jha.</description><link>https://badaljha.com/</link><item><title>Who actually runs your virtual threads?</title><link>https://badaljha.com/blog/who-actually-runs-your-virtual-threads/</link><guid isPermaLink="true">https://badaljha.com/blog/who-actually-runs-your-virtual-threads/</guid><description>Carriers, cores, maxPoolSize, parking, unmounting and the compensation trick the JDK uses when something blocks.</description><pubDate>Sun, 04 Oct 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Carriers, cores, maxPoolSize, parking, unmounting and the compensation trick the JDK uses when something blocks.&lt;/p&gt;
&lt;p&gt;When I first read “you can create a million virtual threads”, my next thought was: run on what? A laptop with 8 cores still has 8 cores. The answer is that virtual threads don’t replace platform threads. They sit on top of a small pool of them, and most of the confusing behaviour (stalls, “why is my app slow with only 3 threads busy?”, odd thread names in dumps) comes from not knowing how that pool is managed. So let’s go through it properly.&lt;/p&gt;
&lt;h2 id=&quot;virtual-threads-need-carriers&quot;&gt;Virtual threads need carriers&lt;/h2&gt;
&lt;p&gt;A virtual thread is a normal &lt;code&gt;java.lang.Thread&lt;/code&gt; object, but it has no OS thread of its own. When it has work to do, the scheduler picks a platform thread and runs it there. That platform thread is called the &lt;strong&gt;carrier&lt;/strong&gt;. The same virtual thread may use a different carrier next time; there is no affinity.&lt;/p&gt;
&lt;p&gt;The default scheduler is a &lt;code&gt;ForkJoinPool&lt;/code&gt;, which is why carriers show up in dumps as &lt;code&gt;ForkJoinPool-1-worker-N&lt;/code&gt;. Running a virtual thread is called &lt;em&gt;mounting&lt;/em&gt; it on a carrier. Taking it off is &lt;em&gt;unmounting&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/uploads/covers/64d716cfd3ee0a27b4484b9a/11634a07-12b6-459a-811c-70e5c74c4040.png&quot; alt/&gt;&lt;/p&gt;
&lt;p&gt;Many virtual threads, few carriers. Each carrier is a real OS thread.&lt;/p&gt;
&lt;h2 id=&quot;cores-parallelism-and-maxpoolsize&quot;&gt;Cores, parallelism and maxPoolSize&lt;/h2&gt;
&lt;p&gt;Three settings control the carrier pool, and people mix them up constantly:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Property&lt;/th&gt;
&lt;th&gt;What it controls&lt;/th&gt;
&lt;th&gt;Default&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;jdk.virtualThreadScheduler.parallelism&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;How many carriers the pool aims to keep. This is how many virtual threads can run at the same instant.&lt;/td&gt;
&lt;td&gt;Number of CPU cores&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;jdk.virtualThreadScheduler.maxPoolSize&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Hard ceiling on carriers, including temporary ones added by compensation.&lt;/td&gt;
&lt;td&gt;256&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;jdk.virtualThreadScheduler.minRunnable&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Minimum number of carriers allowed to be not blocked. It is passed straight to the pool’s constructor.&lt;/td&gt;
&lt;td&gt;I haven’t verified this, so check your JDK&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The JDK reads the first two as ForkJoinPool constructor arguments. In the source, if you set &lt;code&gt;maxPoolSize&lt;/code&gt; lower than parallelism, parallelism gets clamped down to it.&lt;/p&gt;
&lt;h3 id=&quot;parallel-versus-concurrent&quot;&gt;Parallel versus concurrent&lt;/h3&gt;
&lt;p&gt;This is the part worth burning into memory. With 8 cores you can have 100,000 virtual threads &lt;em&gt;alive&lt;/em&gt;, but at most 8 are &lt;em&gt;executing&lt;/em&gt; at once. The rest are parked, waiting for I/O, a lock or a timer, and cost almost nothing. That’s why virtual threads shine for I/O-bound work and do nothing for CPU-bound work: if every thread is crunching numbers, 8 carriers are 8 carriers.&lt;/p&gt;
&lt;p&gt;The scheduler also doesn’t preempt. A virtual thread that spins in &lt;code&gt;while(true)&lt;/code&gt; never yields. Rock the JVM demonstrates this nicely: set &lt;code&gt;maxPoolSize=1&lt;/code&gt; and a busy-looping virtual thread starves everyone else; set it to 2 and a second carrier picks up the other thread.&lt;/p&gt;
&lt;h2 id=&quot;park-and-unmount&quot;&gt;Park and unmount&lt;/h2&gt;
&lt;p&gt;Here is the normal life of a virtual thread that hits a blocking call, such as a socket read, &lt;code&gt;Thread.sleep&lt;/code&gt;, &lt;code&gt;BlockingQueue.take()&lt;/code&gt; or a &lt;code&gt;java.util.concurrent&lt;/code&gt; lock:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;It calls the blocking method. The JDK sees it’s on a virtual thread and &lt;strong&gt;parks&lt;/strong&gt; it instead of blocking the OS thread.&lt;/li&gt;
&lt;li&gt;The thread’s stack frames are copied from the carrier’s stack into a heap object. That’s the &lt;strong&gt;unmount&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The carrier is now free and immediately picks another runnable virtual thread.&lt;/li&gt;
&lt;li&gt;When the I/O completes or the timer fires, something submits the virtual thread back to the scheduler.&lt;/li&gt;
&lt;li&gt;Some carrier (maybe a different one) &lt;strong&gt;mounts&lt;/strong&gt; it again, copies the stack back, and execution continues right after the blocking call.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/uploads/covers/64d716cfd3ee0a27b4484b9a/21cc91a6-03ec-49d0-9b8d-bb5a69e22ede.png&quot; alt/&gt;&lt;/p&gt;
&lt;p&gt;Per the OpenJDK notes, this friendly path covers things like &lt;code&gt;Thread.sleep&lt;/code&gt;, &lt;code&gt;Thread.join&lt;/code&gt;, everything blocking in &lt;code&gt;java.util.concurrent&lt;/code&gt;, and socket accept/read/write.&lt;/p&gt;
&lt;h2 id=&quot;pinned-when-parking-cant-unmount&quot;&gt;Pinned: when parking can’t unmount&lt;/h2&gt;
&lt;p&gt;Sometimes a virtual thread can’t leave its carrier. JEP 444 names two cases: it is inside a &lt;code&gt;synchronized&lt;/code&gt; block or method, or it is running a native method or foreign function. If it blocks while pinned, the carrier and the OS thread underneath are blocked too, exactly like the old days. In stack traces you may notice a frame called &lt;code&gt;parkOnCarrierThread&lt;/code&gt;, which is the virtual thread parking the carrier itself.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Version note.&lt;/strong&gt; JEP 491 (JDK 24) removed most of the &lt;code&gt;synchronized&lt;/code&gt; pinning and the &lt;code&gt;Object.wait&lt;/code&gt; limitation. On JDK 21 to 23 it’s a real concern; on 24+ it’s mostly about native frames. Check which JDK you run before you rewrite code.&lt;/p&gt;
&lt;h2 id=&quot;compensation&quot;&gt;Compensation&lt;/h2&gt;
&lt;p&gt;Not every non-unmountable block is a dead end. Some operations can’t release the carrier but the JDK knows they will block, so it makes up for the loss. This is &lt;strong&gt;compensation&lt;/strong&gt;: the scheduler temporarily adds an extra carrier so the pool still has enough threads making progress.&lt;/p&gt;
&lt;p&gt;Inside the JDK the mechanism is the internal class &lt;code&gt;jdk.internal.misc.Blocker&lt;/code&gt;. Code like &lt;code&gt;FileInputStream.read&lt;/code&gt; wraps the blocking call with &lt;code&gt;Blocker.begin()&lt;/code&gt; and &lt;code&gt;Blocker.end(...)&lt;/code&gt; in a try/finally. If it’s a virtual thread on a ForkJoinPool carrier, &lt;code&gt;begin()&lt;/code&gt; goes through &lt;code&gt;ForkJoinPool.ManagedBlocker&lt;/code&gt;, which allows an extra worker to be spun up. You can’t call &lt;code&gt;Blocker&lt;/code&gt; from your code (it’s an internal package), but the public cousin is &lt;code&gt;ForkJoinPool.managedBlock&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;what-compensates-and-what-doesnt&quot;&gt;What compensates and what doesn’t&lt;/h3&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Blocking operation&lt;/th&gt;
&lt;th&gt;Unmounts?&lt;/th&gt;
&lt;th&gt;Compensates?&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Socket I/O, sleep, j.u.c. locks and queues&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Not needed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;File I/O, &lt;code&gt;InetAddress&lt;/code&gt; lookups, &lt;code&gt;Selector&lt;/code&gt; selection, &lt;code&gt;Object.wait&lt;/code&gt; (JDK 21 to 23)&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes, via ManagedBlocker&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Anything blocking while pinned (monitor or native frame)&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/uploads/covers/64d716cfd3ee0a27b4484b9a/4423b984-6256-431f-a2ee-90195edc7934.svg&quot; alt/&gt;&lt;/p&gt;
&lt;p&gt;Two things to remember. First, compensation is bounded by &lt;code&gt;maxPoolSize&lt;/code&gt;, so lots of simultaneous file I/O can still exhaust it. Second, the “pinned means no compensation” rule is how people get real deadlocks: there’s an OpenJDK bug (JDK-8334304) where more pinned virtual threads than available carriers just hang forever, and a plain ForkJoinPool would have compensated on the same code.&lt;/p&gt;
&lt;h2 id=&quot;taking-a-thread-snapshot&quot;&gt;Taking a thread snapshot&lt;/h2&gt;
&lt;p&gt;When something looks stuck, a thread dump is the first thing you reach for, and here the usual tools mislead you. Plain &lt;code&gt;jstack&lt;/code&gt; or &lt;code&gt;jcmd &amp;lt;pid&amp;gt; Thread.print&lt;/code&gt; doesn’t list virtual threads, and the carriers often look idle. The JDK’s own bug report on the deadlock above makes a point of how hard this is to see through &lt;code&gt;Thread.getAllStackTraces()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The command that does include virtual threads is:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;jcmd &amp;lt;pid&amp;gt; Thread.dump_to_file -format=json /tmp/threads.json&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Carrier threads appear with the &lt;code&gt;ForkJoinPool-1-worker-N&lt;/code&gt; naming. For hunting pinning specifically:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;-Djdk.tracePinnedThreads=full&lt;/code&gt; (or &lt;code&gt;short&lt;/code&gt;) prints a stack trace when a virtual thread blocks while pinned. It works on JDK 21 to 23; I haven’t checked whether it was kept in later releases.&lt;/li&gt;
&lt;li&gt;Flight Recorder emits &lt;code&gt;jdk.VirtualThreadPinned&lt;/code&gt; events. Someone in a Spring Boot issue saw 59 of them from three messages sent over a socket inside &lt;code&gt;synchronized&lt;/code&gt;, which is a good example of how quickly it adds up.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When you read a dump, ask three questions: how many carriers exist, are all of them blocked, and what are the virtual threads waiting on? If every carrier sits in a native frame or monitor wait, you’ve found your pinning problem.&lt;/p&gt;
&lt;h2 id=&quot;practical-takeaways&quot;&gt;Practical takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Parallelism defaults to your core count, so don’t expect virtual threads to speed up CPU work.&lt;/li&gt;
&lt;li&gt;Don’t pool virtual threads. Create one per task; the carriers are the pool.&lt;/li&gt;
&lt;li&gt;Keep blocking I/O out of &lt;code&gt;synchronized&lt;/code&gt; on JDK 21 to 23. Use &lt;code&gt;ReentrantLock&lt;/code&gt; instead, or upgrade to 24+.&lt;/li&gt;
&lt;li&gt;Raise &lt;code&gt;maxPoolSize&lt;/code&gt; only after a dump shows carriers are actually being exhausted.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;Thread.dump_to_file&lt;/code&gt; and JFR pinning events before guessing.&lt;/li&gt;
&lt;/ul&gt;</content:encoded></item><item><title>Mastering Concurrency: ThreadPool in Java Explained</title><link>https://badaljha.com/blog/mastering-concurrency-threadpool-in-java-explained/</link><guid isPermaLink="true">https://badaljha.com/blog/mastering-concurrency-threadpool-in-java-explained/</guid><description>Today we are going to discuss about Threads and ThreadPool. Lets understand the need of multithread</description><pubDate>Sun, 09 Mar 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Today we are going to discuss about Threads and ThreadPool. Lets understand the need of multithread&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Need for Multithreading in Prime Number Counting&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When counting prime numbers in a specified range, a &lt;strong&gt;single-threaded approach&lt;/strong&gt; checks each number sequentially, leading to slow performance for large ranges. This happens because only one CPU core is utilized at a time, making the computation inefficient.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Multithreading&lt;/strong&gt; improves performance by:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Parallel Execution:&lt;/strong&gt; Splitting the range into smaller parts, allowing multiple threads to check numbers simultaneously.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Efficient CPU Utilization:&lt;/strong&gt; Utilizing multiple CPU cores instead of a single one.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Faster Processing:&lt;/strong&gt; Reducing overall execution time by distributing the workload.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For example, if we need to count prime numbers in &lt;code&gt;1 to 1,000,000&lt;/code&gt;, using multiple threads can divide the range (e.g., &lt;code&gt;1-250k&lt;/code&gt;, &lt;code&gt;250k-500k&lt;/code&gt;, etc.), significantly speeding up computation compared to a single-threaded approach.&lt;/p&gt;
&lt;p&gt;Will will se how multithreading is helping in above case but before that lets understand some core concepts of multithreading&lt;/p&gt;
&lt;p&gt;&lt;em&gt;So What is a Thread?? and how its different from a process?&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;So A &lt;strong&gt;process&lt;/strong&gt; is an independent program execution unit with its own memory space while a &lt;strong&gt;thread&lt;/strong&gt; is a lightweight unit of execution within a process that shares the same memory.&lt;/p&gt;
&lt;p&gt;So you can think a process as you open &lt;strong&gt;Microsoft Word&lt;/strong&gt;, the system creates a &lt;strong&gt;separate process&lt;/strong&gt; for it. This process has its own memory space and resources, independent of other applications running on your computer.&lt;/p&gt;
&lt;p&gt;multiple &lt;strong&gt;threads&lt;/strong&gt; run simultaneously to handle different tasks efficiently. Examples include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Auto-save Thread:&lt;/strong&gt; Saves the document in the background while you continue typing.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Spell Check Thread:&lt;/strong&gt; Continuously checks for spelling and grammar errors.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rendering Thread:&lt;/strong&gt; Handles UI updates, such as scrolling or text formatting changes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;User Input Thread:&lt;/strong&gt; Processes keystrokes and mouse clicks.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id=&quot;how-to-create-thread-in-java&quot;&gt;How to create Thread in Java&lt;/h1&gt;
&lt;p&gt;In Java, there are two primary ways to create a thread:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Extending the&lt;/strong&gt; &lt;code&gt;Thread&lt;/code&gt; class&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Implementing the&lt;/strong&gt; &lt;code&gt;Runnable&lt;/code&gt; interface (which &lt;code&gt;Thread&lt;/code&gt; internally implements)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Since &lt;code&gt;Thread&lt;/code&gt; itself implements &lt;code&gt;Runnable&lt;/code&gt;, extending &lt;code&gt;Thread&lt;/code&gt; is essentially another way of using &lt;code&gt;Runnable&lt;/code&gt;. However, using &lt;code&gt;Runnable&lt;/code&gt; is preferred as it allows better flexibility (e.g., enabling multiple inheritance)&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;package&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; multithreading&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; ThreadInJava&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; static&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; main&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;[]&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; args&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;        // there are two ways to create threadss in java one is by extending Thread class another is to implement runnable class&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;        // Thread creation using Thread class&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;        Thread&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; t1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; MyThread&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;        t1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;start&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;        System&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;this is  thread : &lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;Thread&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;currentThread&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;getName&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;        //Thread using Runnable interface&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;        Runnable&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; myRunnable&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; MyRunnable&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;        Thread&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; t2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Thread&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;myRunnable&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;        t2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;start&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;       System&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;this is  thread : &lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;Thread&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;currentThread&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;getName&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; MyRunnable&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; implements&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; Runnable&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Override&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; run&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;        System&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;THis is Thread:  &lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;Thread&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;currentThread&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;getName&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;// although underthehood this Thread class will eventually implementing Runnable interface only&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; MyThread&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; Thread&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Override&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; run&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;        System&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;this is another thread : &lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;Thread&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;currentThread&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;getName&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1 id=&quot;thread-pool&quot;&gt;Thread Pool&lt;/h1&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/res/hashnode/image/upload/v1741505818696/30d958aa-aa9f-402f-b8b2-9378953efbd0.png&quot; alt/&gt;&lt;/p&gt;
&lt;p&gt;A &lt;strong&gt;ThreadPool&lt;/strong&gt; is a collection of pre-created worker threads that efficiently handle multiple tasks by reusing available threads. It helps improve performance by managing resources effectively, avoiding the overhead of creating and destroying threads repeatedly.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How many threads we can create in java?&lt;/strong&gt; So number of threads we can create depends on various factor like&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;CPU cores&lt;/li&gt;
&lt;li&gt;JV memory&lt;/li&gt;
&lt;li&gt;Task nature IO intensive or CPU intensive&lt;/li&gt;
&lt;li&gt;Os limits&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;While Java theoretically allows for tens of thousands of threads, creating too many threads may lead to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Resource exhaustion&lt;/strong&gt;: Running out of system memory, causing the JVM to fail.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Context switching overhead&lt;/strong&gt;: Too many threads may increase the context-switching overhead and degrade performance.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Why Use a ThreadPool?&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Reduces thread creation overhead.&lt;/li&gt;
&lt;li&gt;Manages resource utilization efficiently.&lt;/li&gt;
&lt;li&gt;Prevents system crashes due to excessive thread creation.&lt;/li&gt;
&lt;li&gt;Optimizes performance for concurrent tasks.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;executor-and-executorservice&quot;&gt;Executor and ExecutorService&lt;/h2&gt;
&lt;p&gt;Executor interface which provide only one method to invoke the task i.e execute method&lt;/p&gt;
&lt;p&gt;&lt;code&gt;void execute(Runnable command);&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;ExecutorService interface extends Executor interface and provide more methods to control the progress of the tasks and manage the termination of the service.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;ExecutorService executorService = Executors.newFixedThreadPool(5);&lt;/code&gt;&lt;br/&gt;
&lt;code&gt;Future&amp;lt;String&amp;gt; future = executorService.submit(() -&amp;gt; &amp;quot;Hello Badal&amp;quot;); // some operations&lt;/code&gt;&lt;br/&gt;
&lt;code&gt;String result = future.get();&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;ExecutorService provide submit method which can both take callable and runnable. Runnable’s single method does not return any value and neither throw any exception&lt;/p&gt;
&lt;p&gt;Callable allow us to throw an exception and return an value&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;FunctionalInterface&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; interface&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Runnable&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;    /**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;     * Runs this operation.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;     */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    void&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; run&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;FunctionalInterface&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; interface&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Callable&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;V&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;    /**&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;     * Computes a result, or throws an exception if unable to do so.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;     *&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;     * &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-light-font-style:italic;--shiki-dark:#DCA15A;--shiki-dark-font-style:italic&quot;&gt;@return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt; computed result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;     * &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-light-font-style:italic;--shiki-dark:#DCA15A;--shiki-dark-font-style:italic&quot;&gt;@throws&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-light-font-style:italic;--shiki-dark:#E9C48F;--shiki-dark-font-style:italic&quot;&gt; Exception&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt; if unable to compute a result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;     */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    V&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; call&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; throws&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; Exception&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;2-threadpoolexecutor&quot;&gt;2. ThreadPoolExecutor&lt;/h2&gt;
&lt;p&gt;ThreadPoolExecutor is an extensible threadpool implementation which manages a pool of worker threads efficiently.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/res/hashnode/image/upload/v1741507413981/1de0c385-dd1d-4322-8b32-1ce02f68c5fd.png&quot; alt/&gt;&lt;/p&gt;
&lt;h3 id=&quot;why-to-use-threadpool-instead-of-creating-thread-everytime&quot;&gt;Why to Use ThreadPool instead of creating Thread everytime&lt;/h3&gt;
&lt;p&gt;1. &lt;strong&gt;Efficient Thread Management&lt;/strong&gt; - Reuses threads instead of creating new ones for every task.&lt;br/&gt;
2. &lt;strong&gt;Performance Optimization&lt;/strong&gt; - Reduces thread creation overhead, improving performance.&lt;br/&gt;
3. &lt;strong&gt;Customizable Behavior&lt;/strong&gt; - Allows fine-tuning of core threads, max threads, queue size, and rejection policies.&lt;/p&gt;
&lt;p&gt;Whith ThreadPoolExecutor we can have more control over the thread creation and its management we can pass multiple parameters like corePoolSize, MaxPoolSize, keepAliveTime, workQueue&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;ThreadPoolExecutor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; corePoolSize&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;                              int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; maximumPoolSize&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;                              long&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; keepAliveTime&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;                              TimeUnit&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; unit&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;                              BlockingQueue&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;Runnable&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; workQueue&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Lets understand all the parameters one by one&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;corePoolSize&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This is the number of threads that will be present when we initiate a threadpool. lets say corePoolSize=5 this means 5 thread will be created and added to threadpool initially&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;maximumPoolSize&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;It is the maximum number of thread that a threadpool can grow to.&lt;em&gt;if all core threads are busy and the internal queue is full, the pool is allowed to grow up to maximumPoolSize&lt;/em&gt;**.**&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;keepAliveTIme&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Time a thread(excess thread apart from corePoolSize) remains idle before terminating&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;unit&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;unit of keepAliveTime, Hour, Minute etc&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;workQueue&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A &lt;strong&gt;Task Queue&lt;/strong&gt; (also called a &lt;strong&gt;Work Queue&lt;/strong&gt;) in &lt;code&gt;ThreadPoolExecutor&lt;/code&gt; is used to hold tasks &lt;strong&gt;waiting&lt;/strong&gt; to be executed when all core threads are busy.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;what-happens-when-a-task-is-submitted-tothreadpoolexecutor&quot;&gt;What happens when a task is submitted toThreadPoolExecutor&lt;/h2&gt;
&lt;p&gt;When a new task is submitted to &lt;code&gt;ThreadPoolExecutor&lt;/code&gt;:&lt;br/&gt;
1. If there are &lt;strong&gt;idle core threads (corePoolSize)&lt;/strong&gt;, the task is assigned to an available thread immediately.&lt;br/&gt;
2. If all &lt;strong&gt;core threads are busy&lt;/strong&gt;, the task is added to the &lt;strong&gt;task queue&lt;/strong&gt; (&lt;code&gt;BlockingQueue&lt;/code&gt;).&lt;br/&gt;
3. If the queue is &lt;strong&gt;full&lt;/strong&gt; and the number of threads is &lt;strong&gt;less than&lt;/strong&gt; &lt;code&gt;maximumPoolSize&lt;/code&gt;, a &lt;strong&gt;new thread&lt;/strong&gt; is created.&lt;br/&gt;
4. If both the queue is &lt;strong&gt;full&lt;/strong&gt; and &lt;code&gt;maximumPoolSize&lt;/code&gt; is reached, the task is &lt;strong&gt;rejected&lt;/strong&gt; based on a &lt;strong&gt;Rejection Policy&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Apart from above parameters we can also pass two more parameter &lt;strong&gt;threadFactory&lt;/strong&gt; and &lt;strong&gt;RejectedExecutionHandler&lt;/strong&gt; while creating threadpool&lt;/p&gt;
&lt;p&gt;By default, Java’s &lt;code&gt;Executors&lt;/code&gt; create new threads using &lt;code&gt;new Thread()&lt;/code&gt;, but &lt;code&gt;ThreadFactory&lt;/code&gt; allows:&lt;br/&gt;
1. &lt;strong&gt;Custom thread naming&lt;/strong&gt;&lt;br/&gt;
2. &lt;strong&gt;Setting thread priorities&lt;/strong&gt;&lt;br/&gt;
3. &lt;strong&gt;Creating daemon threads&lt;/strong&gt; (threads that do not prevent JVM shutdown)&lt;br/&gt;
4. &lt;strong&gt;Applying thread-specific configurations&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;//constuctor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;ThreadPoolExecutor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; corePoolSize&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;                              int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; maximumPoolSize&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;                              long&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; keepAliveTime&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;                              TimeUnit&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; unit&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;                              BlockingQueue&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;Runnable&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; workQueue&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;                              ThreadFactory&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; threadFactory&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;                              RejectedExecutionHandler&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; handler&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; CustomThreadFactory&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; implements&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; ThreadFactory &lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; final&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; AtomicInteger&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; threadCount &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; AtomicInteger&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; final&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; namePrefix&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; CustomThreadFactory&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; namePrefix&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;        this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;namePrefix &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; namePrefix&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Override&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; Thread&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; newThread&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Runnable&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; r&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;        Thread&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; thread &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Thread&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; namePrefix &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;-Thread-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; threadCount&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;getAndIncrement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; thread&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;//create new threadpool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;ExecutorService&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; executor &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; ThreadPoolExecutor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt;    2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; 60&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; TimeUnit&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;SECONDS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; LinkedBlockingQueue&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;lt;&amp;gt;(),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; CustomThreadFactory&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;WorkerPool&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;RejectedExecutionHandler&lt;/strong&gt; → When using &lt;code&gt;ThreadPoolExecutor&lt;/code&gt;, if a task &lt;strong&gt;cannot be accepted&lt;/strong&gt; (e.g., the queue is full, and all threads are busy), the task is &lt;strong&gt;rejected&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;RejectedExecutionHandler&lt;/code&gt; interface allows you to &lt;strong&gt;define a custom rejection policy&lt;/strong&gt; to decide what happens when a task is rejected.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Policy&lt;/th&gt;
&lt;th&gt;Behavior&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;AbortPolicy (Default)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Throws &lt;code&gt;RejectedExecutionException&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;DiscardPolicy&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Silently &lt;strong&gt;discards&lt;/strong&gt; the rejected task.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;DiscardOldestPolicy&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Removes the &lt;strong&gt;oldest task&lt;/strong&gt; in the queue and retries adding the new task.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;CallerRunsPolicy&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Runs the task in the &lt;strong&gt;calling thread&lt;/strong&gt; instead of the pool.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;code&gt;ExecutorService executor = new ThreadPoolExecutor(&lt;/code&gt;&lt;br/&gt;
&lt;code&gt;2, 4, 10, TimeUnit.SECONDS,&lt;/code&gt;&lt;br/&gt;
&lt;code&gt;new ArrayBlockingQueue&amp;lt;&amp;gt;(2),&lt;/code&gt;&lt;br/&gt;
&lt;code&gt;new ThreadPoolExecutor.AbortPolicy());&lt;/code&gt;&lt;/p&gt;
&lt;h1 id=&quot;executors-in-java&quot;&gt;Executors in java&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;Executors&lt;/code&gt; utility class is part of the &lt;code&gt;java.util.concurrent&lt;/code&gt; package and provides factory methods for creating different types of executor services. This class helps to simplify the process of creating and configuring thread pools, making it easier to work with concurrent tasks without having to manage thread creation and execution manually&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;Executors&lt;/code&gt; class contains static methods that return pre-configured implementations of &lt;code&gt;ExecutorService&lt;/code&gt;, &lt;code&gt;ScheduledExecutorService&lt;/code&gt;, and &lt;code&gt;Executor&lt;/code&gt; interfaces. These are some of the most commonly used executor implementations in Java for managing and executing tasks in a multi-threaded environment.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;newFixedThreadPool(int nThread)&lt;/strong&gt;&lt;br/&gt;
&lt;em&gt;As per official documentaion: Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.&lt;/em&gt;&lt;br/&gt;
in short Creates a thread pool with a fixed number of threads. If all threads are busy, new tasks will be queued until a thread is available. This is useful when you have a known, fixed number of tasks to process concurrently.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; static&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; ExecutorService&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; newFixedThreadPool&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; nThreads&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; ThreadPoolExecutor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;nThreads&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; nThreads&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt;                                  0L&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; TimeUnit&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;MILLISECONDS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;                                  new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; LinkedBlockingQueue&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Runnable&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;newCachedThreadPool()&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;As per official documentaion: Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources. Note that pools with similar properties but different details (for example, timeout parameters) may be created using ThreadPoolExecutor constructors.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Returns: the newly created thread pool&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;It uses synchronous queue which means there is direct handoff between producer and consumer as the size of this synchronousQueue will be 1 always&lt;/p&gt;
&lt;p&gt;In short Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when available.&lt;/p&gt;
&lt;p&gt;This type of pool can grow as needed but has no upper limit (except for system constraints like memory). Tasks that finish early can help process subsequent tasks.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; static&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; ExecutorService&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; newCachedThreadPool&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;     return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; ThreadPoolExecutor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Integer&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;MAX_VALUE&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt;                                   60L&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; TimeUnit&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;SECONDS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;                                   new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; SynchronousQueue&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Runnable&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;newSingleThreadExecutor&lt;/em&gt;&lt;/strong&gt;()&lt;/p&gt;
&lt;p&gt;&lt;em&gt;As per official documentaion: Creates an Executor that uses a single worker thread operating off an unbounded queue. (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.) Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time. Unlike the otherwise equivalent newFixedThreadPool(1) the returned executor is guaranteed not to be reconfigurable to use additional threads.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Returns: the newly created single-threaded Executor&lt;/em&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; static&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; ExecutorService&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; newSingleThreadExecutor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;     return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; FinalizableDelegatedExecutorService&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;         (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; ThreadPoolExecutor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt;                                 0L&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; TimeUnit&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;MILLISECONDS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;                                 new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; LinkedBlockingQueue&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Runnable&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;gt;()));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h3 id=&quot;scheduledthreadpoolexecutor&quot;&gt;&lt;strong&gt;&lt;em&gt;ScheduledThreadPoolExecutor&lt;/em&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;ScheduledThreadPoolExecutor&lt;/code&gt; class extends the &lt;code&gt;ThreadPoolExecutor&lt;/code&gt; class and implements the &lt;code&gt;ScheduledExecutorService&lt;/code&gt; interface, providing several additional methods that are specifically designed for scheduling tasks with delays or periodic execution. These methods allow for greater control over when and how tasks are executed.&lt;/p&gt;
&lt;h3 id=&quot;key-methods-of-scheduledthreadpoolexecutor&quot;&gt;Key Methods of ScheduledThreadPoolExecutor:&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;schedule(Runnable command, long delay, TimeUnit unit)&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;This method allows us to run a task &lt;strong&gt;once&lt;/strong&gt; after a specified &lt;strong&gt;initial delay&lt;/strong&gt;. The task is scheduled to execute after the delay has passed, and only a single execution occurs.&lt;/li&gt;
&lt;li&gt;Use this When you need a one-time delayed execution of a task.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;        javaCopyScheduledExecutorService scheduler &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; Executors&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;newScheduledThreadPool&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;        scheduler&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;schedule&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; System&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;Task executed after delay&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; TimeUnit&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;SECONDS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;This method allows us to &lt;strong&gt;repeatedly execute&lt;/strong&gt; a task with a &lt;strong&gt;fixed rate&lt;/strong&gt;. The task is first delayed by the initialDelay, and then executed repeatedly at fixed intervals defined by the period.&lt;/li&gt;
&lt;li&gt;The period is the time between the &lt;strong&gt;start&lt;/strong&gt; of each execution, meaning tasks will start at regular intervals regardless of how long each task takes to execute.&lt;/li&gt;
&lt;li&gt;Use this When you need periodic execution of tasks, such as in scenarios like polling or heartbeats.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;        scheduler&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;scheduleAtFixedRate&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; System&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;Task executed at fixed rate&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; TimeUnit&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;SECONDS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;This method is simmilar to scheduleAtFixedRate, but with a small difference: it schedules tasks at a &lt;strong&gt;fixed delay&lt;/strong&gt; between the &lt;strong&gt;end of one execution and the start of the next&lt;/strong&gt;. The execution rate may vary depending on how long each task takes to run.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;delay&lt;/code&gt; is the time between the completion of one task and the start of the next. This allows for some flexibility in the execution schedule.&lt;/li&gt;
&lt;li&gt;When you need repeated task executions, but want the interval between executions to be measured from the completion of the previous task, ensuring a consistent gap between tasks rather than a fixed start time.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;        scheduler&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;scheduleWithFixedDelay&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; System&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;Task executed with fixed delay&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; 10&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We will learn More about Concurrency, Locking, ForkJoinPool etc in next article.&lt;/p&gt;
&lt;p&gt;I hope you understood the inversion of control and dependency injection. follow me for more engaging software engineering content!&lt;/p&gt;
&lt;p&gt;Happy Coding🚀🚀&lt;/p&gt;</content:encoded><category>java</category><category>multithreading</category><category>software-development</category><category>system-design</category></item><item><title>Kubernetes Essentials: Unlocking the Power of Container Management</title><link>https://badaljha.com/blog/kubernetes-essentials/</link><guid isPermaLink="true">https://badaljha.com/blog/kubernetes-essentials/</guid><description>So what the hell is Kubernetes? We have learned about Docker and seen how it containerizes our applications, allowing them to run in isolated environments…</description><pubDate>Mon, 19 Aug 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;So what the hell is Kubernetes? We have learned about Docker and seen how it containerizes our applications, allowing them to run in isolated environments called containers. So if we already have docker and other containerization tools then what is the need of Kubernetes.&lt;/p&gt;
&lt;p&gt;So, we will dive deep into the world of Kubernetes. Since it is vast, we will cover it step by step.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Introduction to Kubernetes, What is the need of it if we have docker already?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Architecture of Kubernetes&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Working with Kubernetes Objects&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Networking and persitent storage&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;security in kubernetes&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In this article we are going to see the first two part and rest will cover in next articles.&lt;/p&gt;
&lt;h1 id=&quot;1-introduction-to-kubernetes&quot;&gt;1. Introduction to Kubernetes&lt;/h1&gt;
&lt;p&gt;So let’s explore the challenges with containerization. While Docker is a popular tool, it’s important to recognize that there are other containerization solutions like containerd. We will focus on Docker to illustrate these concepts, but the principles apply broadly across various containerization technologies.&lt;/p&gt;
&lt;p&gt;problems with docker:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Single Host:&lt;/strong&gt; Docker on a single host can only support as many containers as the host’s resources allow. As your application grows, this setup becomes a bottleneck, making it difficult to scale effectively.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Auto Healing:&lt;/strong&gt; If a container fails or crashes, it won’t automatically recover on its own. DevOps engineers must manually monitor and restart failed containers, which can be time-consuming and prone to human error, especially in large-scale environments.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Auto Scaling:&lt;/strong&gt; There is no auto scaling in case of container if the traffic increases and load increases we have to manually spin more container it will not automatically scale&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Automated rollouts, scaling, and rollbacks&lt;/strong&gt;: Kubernetes manages your application by automatically creating the desired number of replicas, distributing them across appropriate hardware, and rescheduling containers if a node fails. It allows for on-demand scaling of replicas in response to changing conditions, such as CPU usage, ensuring your application remains resilient and responsive.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These are the problems that kubernetes solve lets understand what is kubernetes and how it solved above problems.&lt;/p&gt;
&lt;p&gt;So according to kubernetes official documentation Kubernetes also known as as K8s, is an open source system for automating deployment, scaling, and management of containerized applications. and best part is it does all of this without you having to supervise or get involved. Obviously, you have to set things up in the first place, but once you’ve done that, you sit back and let Kubernetes work its magic.&lt;/p&gt;
&lt;p&gt;You must be thinking why it is called k8s? because there are 8 letter between k and s in kubernetes simple there is nothing important in it.&lt;/p&gt;
&lt;h1 id=&quot;2-architecture-of-kubernetes&quot;&gt;2. Architecture of Kubernetes&lt;/h1&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/res/hashnode/image/upload/v1724043546828/c99bdf19-d8ae-4177-a6a7-b4fcb6c8cbf0.png&quot; alt/&gt;&lt;/p&gt;
&lt;p&gt;This is the basic kubernetes architecture. we will go through each and every component and learn about its working.&lt;/p&gt;
&lt;h2 id=&quot;master-node&quot;&gt;Master Node&lt;/h2&gt;
&lt;p&gt;Master Node is also called as control plane is responsible for managing whole clusture. It monitors the health check of all nodes in the cluster. It manages the information about all the nodes in a cluster, including which containers are assigned to which worker nodes. It continuously monitors both containers and nodes, and if a worker node fails, Kubernetes automatically shifts the workload from the failed node to another healthy node.&lt;/p&gt;
&lt;p&gt;The Kubernetes master is responsible for key tasks such as scheduling, provisioning, and configuring the cluster, as well as exposing APIs to clients. The master node uses various control plane components to handle these responsibilities efficiently.&lt;/p&gt;
&lt;p&gt;Kubernetes also simplifies many complex tasks for developers, such as service discovery, scaling, load balancing, self-healing, and leader election. This means developers no longer need to build these services into their applications, as Kubernetes handles them automatically.&lt;/p&gt;
&lt;p&gt;Four basic component of Master Nodes are&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Api server&lt;/li&gt;
&lt;li&gt;Scheduler&lt;/li&gt;
&lt;li&gt;Controller&lt;/li&gt;
&lt;li&gt;Etcd&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;1api-server&quot;&gt;1.Api server&lt;/h3&gt;
&lt;p&gt;It acts as the main communication hub for all operations within the cluster. It exposes the Kubernetes API, which other components and external tools can use to interact with the cluster. All other component such as Scheduler, Controller will Communicate to api server before taking any action.&lt;/p&gt;
&lt;p&gt;The API server receives and processes RESTful API requests like &lt;code&gt;kubectl&lt;/code&gt; commands from users, administrators, and other components within the cluster.The API server serves as the gateway for interacting with the cluster’s state. It reads and writes data to the etcd database, which stores the entire cluster’s state, including information about nodes, pods, services, and configurations.The API server manages authentication and authorization, ensuring that only legitimate users or services with the right permissions can interact with the cluster.&lt;/p&gt;
&lt;h3 id=&quot;2-scheduler&quot;&gt;2. Scheduler&lt;/h3&gt;</content:encoded><category>kubernetes</category><category>containers</category><category>software-development</category><category>backend</category></item><item><title>From Code to Container: A Guide to Efficiently Publish Spring Boot on Docker🚀🚀</title><link>https://badaljha.com/blog/from-code-to-container/</link><guid isPermaLink="true">https://badaljha.com/blog/from-code-to-container/</guid><description>In this article, we will explore the process of Dockerizing a Spring Boot application and delve into techniques for reducing the image size of our docker…</description><pubDate>Tue, 02 Jan 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In this article, we will explore the process of Dockerizing a Spring Boot application and delve into techniques for reducing the image size of our docker image. So before we move further lets understand some fundamentals of what is docker and what is docker image?&lt;/p&gt;
&lt;h2 id=&quot;docker&quot;&gt;Docker&lt;/h2&gt;
&lt;p&gt;Docker is a containerization platform that provides easy way to containerize your applications, which means, using Docker you can build container images, run the images to create containers and also push these containers to container regestries such as DockerHub, &lt;a href=&quot;http://Quay.io&quot;&gt;Quay.io&lt;/a&gt; and so on. Docker provides the ability to package and run an application in a loosely isolated environment called a container. The isolation and security lets you run many containers simultaneously on a given host. Containers are lightweight and contain everything needed to run the application, so you don’t need to rely on what’s installed on the host. You can share containers while you work, and be sure that everyone you share with gets the same container that works in the same way.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/res/hashnode/image/upload/v1704202399562/9726ac55-5c98-4796-9679-8a954c9d1bab.png&quot; alt/&gt;&lt;/p&gt;
&lt;h2 id=&quot;image&quot;&gt;Image&lt;/h2&gt;
&lt;p&gt;A docker image is just a package of software it consist standalone, and executable software unit that contains all the necessary components and dependencies required to run an application. It encapsulates the application code, runtime, system libraries, and settings in a lightweight, portable, and standardized format. Docker images serve as the building blocks for containers, which are instances of running applications.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/res/hashnode/image/upload/v1704202793979/c5a1013f-10e8-4bd3-8716-6ab14acf088c.png&quot; alt/&gt;&lt;/p&gt;
&lt;p&gt;These images can be accessed on different Docker registries, including Docker Hub. Their size is intentionally kept small, containing only the essential libraries and software required to execute the application. Other components such as system libraries, file systems, and networking stacks are accessed from the host operating system, allowing the images to remain lightweight. This design ensures that Docker images are portable, efficient, and easily distributable across various environments&lt;/p&gt;
&lt;p&gt;Now to get this image in our local system we need to install docker on our system after that we can pull the required image from dockerhub or any other docker registry. assume docker hub as github for images.&lt;/p&gt;
&lt;p&gt;after installing docker on system execute below command in cmd&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;docker pull imagename&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;tag&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;in this article we are mainly focused on creating the image. so lets create a image for out springboot application&lt;/p&gt;
&lt;h2 id=&quot;dockerize-springboot-application&quot;&gt;Dockerize SpringBoot application&lt;/h2&gt;
&lt;p&gt;i have created a simple springboot application springboot-docker&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/res/hashnode/image/upload/v1704203352498/d024c73b-f837-419a-bfb3-67ce02befe59.png&quot; alt/&gt;&lt;/p&gt;
&lt;p&gt;To Dockerize an application, we leverage a Dockerfile—a script that provides instructions for building a Docker image. Below is a brief overview of a Dockerfile and its role in the Dockerization process:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;select a base Image&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Start with a base image that provides the foundational operating system and environment for your application. Choose an image that aligns with your application’s requirements. this base image will be very lightway in nature&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;dockerfile&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; eclipse-temurin:17-jdk-alpine&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;create a directory&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;dockerfile&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; RUN&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; mkdir -p /app/source&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;select a work directory&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Specify the working directory inside the container where the application code and files will be copied.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;dockerfile&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; WORKDIR&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; /app/source&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Copy the required files&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;copy all the necessory file&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;dockerfile&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; COPY&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; . .&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Expose port&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If your application exposes specific ports, use the &lt;code&gt;EXPOSE&lt;/code&gt; instruction to inform Docker about the network ports that should be published.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;dockerfile&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; EXPOSE&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; 8080&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Run maven command to Build the project&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;dockerfile&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; RUN&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; ./mvnw cleane package&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Define the Command to Run the Application:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Specify the command that Docker should execute when the container starts. This typically involves running the main executable or script for your application&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;dockerfile&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; ENTRYPOINT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;&amp;quot;java&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;&amp;quot;-jar&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;&amp;quot;/app/source/target/springboot-docker-0.0.1-SNAPSHOT.jar&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/res/hashnode/image/upload/v1704204246020/a9adf5c4-187e-422a-8f68-9f378debcb1f.png&quot; alt/&gt;&lt;/p&gt;
&lt;p&gt;Now we have create a basic DockerFile to dockerize our application now we just have to build our project. to build our docker application run command:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;docker&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt; build&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt; -t&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt; dockerusername/app-name:tag&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt; .&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;for out application it will be&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;docker&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt; build&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt; -t&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt; badaljha/springboot-docker:latest&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt; .&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/res/hashnode/image/upload/v1704204448031/fcdd6bed-6355-43d0-ada0-b3ffddc2b8bc.png&quot; alt/&gt;&lt;/p&gt;
&lt;p&gt;Now to see the image we have create run command &lt;code&gt;docker images&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/res/hashnode/image/upload/v1704206902514/ae525908-b84e-42ce-adae-5da07360e4e5.png&quot; alt/&gt;&lt;/p&gt;
&lt;p&gt;Now we have created our image to run the image and create the container we can use run command&lt;/p&gt;
&lt;p&gt;&lt;code&gt;docker run -it -p 8080:8080 app-name&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;-it tag is to run application in attach mode and -p is for port mapping&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/res/hashnode/image/upload/v1704204829831/dbc4b922-9428-422c-a4fb-ab9a3531cbbf.png&quot; alt/&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/res/hashnode/image/upload/v1704205120875/e7c002ad-1bd8-4f60-8b4c-bf36bdd904c0.png&quot; alt/&gt;&lt;/p&gt;
&lt;p&gt;it is running now if we want we can push it to docker registry using docker push command&lt;/p&gt;
&lt;p&gt;As we can see when we run &lt;code&gt;docker images&lt;/code&gt; the size of our application is 412MB can we reduce the size of our image??&lt;/p&gt;
&lt;p&gt;So the answer is yes we can optimize our dockerFile and reduce the size of our image. we can use multi stage &lt;strong&gt;dockerfile&lt;/strong&gt;&lt;/p&gt;
&lt;h1 id=&quot;multistage-dockerfile&quot;&gt;Multistage Dockerfile&lt;/h1&gt;
&lt;p&gt;A multistage Dockerfile is a feature in Docker that allows you to use multiple “FROM” statements in a single Dockerfile, creating multiple build stages. Each stage represents a phase in the build process, and you can selectively copy artifacts from one stage to another. This feature is particularly useful for optimizing the final Docker image size and improving build efficiency.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/res/hashnode/image/upload/v1704205477049/8b48683f-6b5d-478e-8886-09e0d8497098.png&quot; alt/&gt;&lt;/p&gt;
&lt;p&gt;The need for multistage builds becomes evident in scenarios where building an application involves several dependencies and tools, but the runtime environment requires only a subset of them. Multistage builds help create a lightweight production image containing only the necessary artifacts, reducing the overall image size.&lt;/p&gt;
&lt;p&gt;To execute the application, there is no requirement for build tools or libraries used during the project’s construction. The essential components include the JAR file, the JDK for running the JAR, and a few necessary libraries. Adopting a multistage approach involves dividing the task into multiple stages. In the initial stage, we build our application, and in the final stage, we transport the JAR file along with the EntryPoint or CMD&lt;/p&gt;
&lt;p&gt;Ideally you want the image to contain only the items that are required by the container to achieve its purpose. so lets build our project in first stage and share the jar file to final stage.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/res/hashnode/image/upload/v1704206135987/9c73280c-1f73-44ea-a281-513122bed20b.png&quot; alt/&gt;&lt;/p&gt;
&lt;p&gt;After making changes to the Dockerfile, let’s rebuild our project and examine whether the image size has been reduced. Executing the command &lt;code&gt;&amp;#39;docker build -t badaljha/springboot-docker:latest&amp;#39;&lt;/code&gt; produced the following output:”&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/res/hashnode/image/upload/v1704206842007/598888f1-2b49-4b5e-8a83-c2b7853d0255.png&quot; alt/&gt;&lt;/p&gt;
&lt;p&gt;As observed, the size of our images has been successfully reduced to 335MB from the previous 412MB. Although the current Dockerfile is effective, there are additional optimizations that can further decrease the image size. In this article, the emphasis was on demonstrating the usage of a multistage Dockerfile, prioritizing clarity over achieving the most optimal size. Future improvements can involve crafting a more efficient Dockerfile to achieve even greater size reduction.&lt;/p&gt;
&lt;p&gt;Stay tuned for more informative articles, and see you in the next one. Follow me for more engaging software engineering content!&lt;/p&gt;
&lt;p&gt;Happy Coding🚀🚀&lt;/p&gt;</content:encoded><category>springboot</category><category>docker</category><category>containers</category><category>backend</category></item><item><title>Getting started with JDBC and Hibernate part-I</title><link>https://badaljha.com/blog/getting-started-with-jdbc-and-hibernate-part-i/</link><guid isPermaLink="true">https://badaljha.com/blog/getting-started-with-jdbc-and-hibernate-part-i/</guid><description>What do you know about JDBC and Hibernate? In this blog we are going to get fundamentals of both and how to use them to connect to a database.</description><pubDate>Fri, 29 Sep 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;What do you know about JDBC and Hibernate? In this blog we are going to get fundamentals of both and how to use them to connect to a database.&lt;/p&gt;
&lt;p&gt;Let’s assume you have created a Java project to do crud operation on Student i.e. you want to Create, Read, Update and Delete a Book from the database. So let’s create a Basic Java Project for the same.&lt;/p&gt;
&lt;h1 id=&quot;what-is-jdbc&quot;&gt;What is JDBC?&lt;/h1&gt;
&lt;p&gt;&lt;strong&gt;JDBC (Java Database Connectivity)&lt;/strong&gt; is a Java based API and a part of the Java Standard Library that provides a standardized way for Java applications to interact with relational databases. It allows Java applications to perform various database operations such as connecting to databases, executing SQL queries, and retrieving and updating data in a database.&lt;/p&gt;
&lt;p&gt;So let’s say we have our data to save and we also have a database setup but to save the data into the database we have to make the connection between Java and database and after making the connection we will be able to save data to the database. so to make that connection Java uses JDBC.&lt;/p&gt;
&lt;h2 id=&quot;jdbc-drivers&quot;&gt;JDBC Drivers&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;A JDBC driver is a platform-specific implementation of the Java Database Connectivity API which means Each DBMS has its own driver . It acts as a bridge between Java code and a particular database management system (DBMS) and you need the appropriate driver to connect to that specific DBMS.&lt;/li&gt;
&lt;li&gt;JDBC drivers are essential for establishing communication with a database. These drivers translate Java calls into database-specific calls that the DBMS can understand.&lt;/li&gt;
&lt;li&gt;After a driver is selected JDBC Driver Manager’s responsibility is to make a connection to the database using the driver.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There are four main types of JDBC drivers commonly referred to as Type 1, Type 2, Type 3, and Type 4. I am not going into this write now you can read about it from the documentation of jdbc.&lt;/p&gt;
&lt;h1 id=&quot;create-a-java-project&quot;&gt;Create a Java Project&lt;/h1&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Create a maven project&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/res/hashnode/image/upload/v1695976013154/36c95743-cc8a-4cac-a1bf-5cde079739b6.png&quot; alt/&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Project Structure&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; BookManagementApp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; ├── src&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; │   └── main&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; │       ├── java&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; │       │   └── com&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; │       │       └── badal&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; │       │           └── demo&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; │       │               └── &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;BookManagement&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; │       │                   ├── dao&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; │       │                   │   └── BookDao&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;java&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; │       │                   ├── entities&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; │       │                   │   └── Book&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;java&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; │       │                   ├── App&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;java&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; │       │                   └── &lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; │       └── resources&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; │           └── &lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; ├── target&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; │   └── &lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; ├── pom&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;xml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; └── &lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Create a Book class&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;  package&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; com&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;badal&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;demo&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;BookManagement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;entities&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;  public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Book&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;      private&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; id&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;      private&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; title&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;      private&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; author&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;      public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; getId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;          return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; id&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;      \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;      public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Book&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; id&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; title&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; author&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;          super&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;          this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;id &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; id&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;          this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;title &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; title&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;          this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;author &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; author&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;      \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;      public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; setId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; id&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;          this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;id &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; id&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;      \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;      public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; getTitle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;          return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; title&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;      \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;      public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; setTitle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; title&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;          this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;title &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; title&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;      \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;      public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; getAuthor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;          return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; author&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;      \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;      public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; setAuthor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; author&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;          this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;author &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; author&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;      \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;      @&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Override&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;      public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; toString&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;          return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;Book [id=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; id &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;, title=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; title &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;, author=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; author &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;      \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;  \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So now we have created the Book class let’s create an instance of this Book class and try to save to to database.&lt;strong&gt;3&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Creating an instance of Book&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;      package&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; com&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;badal&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;demo&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;BookManagement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;      import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; com&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;badal&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;demo&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;BookManagement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;entities&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Book&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;      public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; App&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;          public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; static&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; main&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;[]&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; args&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;              Book&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; book1 &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Book&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;The Great Gatsby&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;F. Scott&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;              System&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;book1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;          \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;      \&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We have created a Book instance Now how to save it to the database?&lt;/p&gt;
&lt;p&gt;So before saving it to the database, we need a database right? so let’s create a database first. I am using MySQL here you can use any database of you preferred choice&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Create a Database&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;after creating the Database create a table mybooks in the database.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; CREATE&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; DATABASE&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; books&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; CREATE&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; mybooks&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;     id &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; AUTO_INCREMENT &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;     title &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt;255&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;     author &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt;255&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;NOT NULL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; );&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we need to add a driver class to interact with the database.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;dependency&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;     &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;com&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;mysql&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;     &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;mysql&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;connector&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;j&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;     &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt;8.0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;32&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; &amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;dependency&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The MySQL Connector/J library provides the necessary classes and methods to establish a connection between your Java application and a MySQL database server. It allows you to specify the database server’s host, port, username, and password to connect to the database.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Connect and save the Book to the Database&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;There are typically 4 steps&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Load the driver&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In your Java application, you load the JDBC driver using the &lt;code&gt;Class.forName()&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;  Class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;forName&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;com.mysql.cj.jdbc.Driver&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Make the connection&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;You establish a connection to the database using the &lt;code&gt;DriverManager.getConnection()&lt;/code&gt; method. This method returns a &lt;code&gt;Connection&lt;/code&gt; object that represents the connection to the database.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;  Connection&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; connection &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; DriverManager&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;getConnection&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;jdbcUrl&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; username&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; password&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Execute the query&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;With the &lt;code&gt;Connection&lt;/code&gt; object, you create &lt;code&gt;Statement&lt;/code&gt; or &lt;code&gt;PreparedStatement&lt;/code&gt; objects to execute SQL queries against the database.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;  Statement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; statement &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; connection&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;createStatement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;  ResultSet&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; resultSet &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; statement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;executeQuery&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;SELECT * FROM mybooks&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Process The Results and Close the connection&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;             resultSet&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;close&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;              statement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;close&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;              connection&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;close&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;();&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now lets write code to save and get the Book from DataBase to interact with database we have created a class called BookDao in which we are all the logic for database interaction&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    package&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; com&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;badal&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;demo&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;BookManagement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;dao&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; java&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;sql&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Connection&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; java&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;sql&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;DriverManager&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; java&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;sql&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;PreparedStatement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; java&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;sql&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;SQLException&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; java&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;sql&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Statement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; com&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;badal&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;demo&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;BookManagement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;entities&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Book&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; BookDao&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;        String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; url &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;jdbc:mysql://localhost:3306/books&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;        String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; username &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;root&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;        String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; password &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;root&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;        // create jdbc connection&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;        public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; Connection&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; getConnection&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; throws&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; ClassNotFoundException&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; SQLException&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;            // Load the driver&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;            Class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;forName&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;com.mysql.cj.jdbc.Driver&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;            // get the connection&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;            Connection&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; connection &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; DriverManager&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;getConnection&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; username&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; password&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; connection&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;        public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; saveBook&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Book&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; book&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; throws&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; SQLException&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; ClassNotFoundException&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;            Connection&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; connection &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;getConnection&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;            // create the statement&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;            Statement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; st &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; connection&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;createStatement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;            // run the query&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;            String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; sql &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;INSERT INTO mybooks (id,title, author) VALUES (?, ?, ?)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;            PreparedStatement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; preparedStatement &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; connection&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;prepareStatement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;sql&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;            preparedStatement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;setInt&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; book&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;getId&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;            preparedStatement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;setString&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; book&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;getTitle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;            preparedStatement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;setString&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; book&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;getAuthor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;            System&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;preparedStatement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;executeUpdate&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; getBook&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; id&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; throws&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; ClassNotFoundException&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; SQLException&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;            Connection&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; connection &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;getConnection&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;            // create the statement&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;            Statement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; st &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; connection&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;createStatement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;            String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; query &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;select title from mybooks where id=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; id&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;            ResultSet&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; rs &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; st&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;executeQuery&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;query&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;            rs&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;next&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; rs&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;getString&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now lets test our code&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    package&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; com&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;badal&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;demo&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;BookManagement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; java&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;sql&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;SQLException&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; com&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;badal&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;demo&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;BookManagement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;dao&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;BookDao&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; com&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;badal&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;demo&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;BookManagement&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;entities&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Book&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; App&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;        public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; static&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; main&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;[]&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; args&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; throws&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; ClassNotFoundException&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; SQLException&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;            Book&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; book1 &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Book&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;The Great Gatsby&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;F. Scott&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;            System&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;book1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;            BookDao&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; dao &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; BookDao&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;            //save to db&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;            // dao.saveBook(book1);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;             //get from db&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;            System&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;the title of book with id=1 is &lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; dao&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;getBook&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the Next article we will continue learning about hibernate and why it is in demand now days also learn about why we need hibernate if there is jdbc available to connect to the database.&lt;/p&gt;
&lt;p&gt;Stay tuned for more informative articles, and see you in the next one. Follow me for more engaging software engineering content!&lt;/p&gt;
&lt;p&gt;Happy Coding🚀🚀&lt;/p&gt;</content:encoded><category>jdbc</category><category>springboot</category><category>databases</category><category>mysql</category></item><item><title>CORS Policy and Handling CORS in Nodejs and Spring Boot</title><link>https://badaljha.com/blog/cors-cross-origin-resource-sharing-policy/</link><guid isPermaLink="true">https://badaljha.com/blog/cors-cross-origin-resource-sharing-policy/</guid><description>CORS stands for Cross-Origin Resource Sharing. It is a security feature to prevent cross-origin requests to different domains. It prevents webpages from…</description><pubDate>Sun, 03 Sep 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;CORS stands for Cross-Origin Resource Sharing. It is a security feature to prevent cross-origin requests to different domains. It prevents webpages from making a request to a other domain than the one that served the web page. When building an application that needs to communicate to different APIs on different domains it gives cors error. There is an SOP i.e. same origin policy which is the foundation of Cors policy according to SOP a web page can only make requests to the same origin. What is Origin Here?&lt;/p&gt;
&lt;p&gt;An origin is defined by the combination of three components:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Scheme (or Protocol)&lt;/strong&gt;: This can be &lt;code&gt;http&lt;/code&gt;, &lt;code&gt;https&lt;/code&gt;, &lt;code&gt;ftp&lt;/code&gt;, etc.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Host&lt;/strong&gt;: This is the domain (e.g., &lt;a href=&quot;http://example.com&quot;&gt;&lt;code&gt;Badal.com&lt;/code&gt;&lt;/a&gt;) or IP address.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Port&lt;/strong&gt;: This is the port number, typically omitted for HTTP (80) and HTTPS (443).&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;what-is-cors&quot;&gt;What is CORS?&lt;/h2&gt;
&lt;p&gt;Cors is a mechanism to prevent unauthorized requests to a domain. CORS is crucial because it ensures that your data and resources are safe. If there is no CORS policy then it will be easier to make malicious requests to any domain from another domain. By enforcing CORS policies, you can control who gets access to your valuable information and resources, making the web a safer place for everyone.CORS is crucial for web security because it prevents unauthorized access to sensitive data and resources. Without CORS, malicious websites could potentially make requests to trusted domains on behalf of the user. To maintain a Secure environment, it’s essential to enforce CORS policies to ensure that only trusted domains can access your resources.&lt;/p&gt;
&lt;h2 id=&quot;cors-errors&quot;&gt;CORS Errors&lt;/h2&gt;
&lt;p&gt;when the server detects a cors policy violation, it generates a CORS error. CORS errors typically come in the form of JavaScript errors that you can see in the browser’s developer console. The most common CORS error is:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/res/hashnode/image/upload/v1693725382451/596cca5d-c9bc-461d-b429-f6973978f3e6.png&quot; alt/&gt;&lt;/p&gt;
&lt;p&gt;This error message indicates that a request from &lt;a href=&quot;https://yourwebsite.com&quot;&gt;&lt;code&gt;http://&lt;/code&gt;&lt;/a&gt;&lt;code&gt;localhost:4200&lt;/code&gt; to &lt;a href=&quot;https://example.com/api/data&quot;&gt;&lt;code&gt;http://localhost:3000/&lt;/code&gt;&lt;/a&gt;&lt;code&gt;balance-sheet&lt;/code&gt; has been blocked due to CORS policy violations.&lt;/p&gt;
&lt;h2 id=&quot;cors-handling-in-nodejs&quot;&gt;CORS handling in NodeJs&lt;/h2&gt;
&lt;p&gt;To handle CORS in nodejs you have to follow the following steps:&lt;/p&gt;
&lt;h3 id=&quot;1-install-cors-middleware&quot;&gt;1. Install &lt;code&gt;cors&lt;/code&gt; middleware&lt;/h3&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;npm install cors&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;2-import-the-cors-module-in-the-express-app-and-use-it&quot;&gt;2. Import the cors module in the express app and use it&lt;/h3&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; express &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; require&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;express&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; cors &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; require&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;cors&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; app &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; express&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;// Enable CORS for all routes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;use&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;cors&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;({&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; origin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; }));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;//other routes and middleware functions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;listen&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt;3000&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; ()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;  console&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;Server is running on port 3000&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;});&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;app.use(cors({origin:true}))&lt;/code&gt; it is used to allow all origin to access the server resource.&lt;/p&gt;
&lt;p&gt;Sometimes you do not want to allow all the origins to access the server resource in this case you can pass a corsOptions to this middleware function. These corsOptions can be static as mentioned below or dynamic. In dynamic options server will determine the allowed origin based on the incoming request&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; express &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; require&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;express&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; cors &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; require&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;cors&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; app &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; express&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;// Enable CORS for all routes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; options &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;  origin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;https://test-domain1.com&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;https://test-domain2.com&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;],&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt; // Allow requests from origin mentioned here&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;  methods&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;GET,PATCH,POST,DELETE&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt; // allow only specific http methods&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;  optionsSuccessStatus&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; 204&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt; // Set the HTTP status code for OPTIONS requests&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;use&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;cors&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;corsOptions&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;//other routes and middleware functions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;listen&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt;3000&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; ()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;  console&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;Server is running on port 3000&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;});&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;cors-handling-in-spring-boot&quot;&gt;CORS handling in Spring Boot&lt;/h2&gt;
&lt;p&gt;In spring boot you can handle CORS using various method&lt;/p&gt;
&lt;h3 id=&quot;1using-crossorigin-annotation&quot;&gt;1.Using &lt;code&gt;@CrossOrigin&lt;/code&gt; annotation&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;@CrossOrigin&lt;/code&gt; is a spring annotation used to handle cors in spring boot.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;RestController&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; BookController&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;GetMapping&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;/book&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;CrossOrigin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;origins &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;https://test-domain1.com&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;https://test-domain2.com&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; ResponseEntity&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;List&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Book&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; getBooks&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;        // Your controller logic here&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;2using-java-configurations&quot;&gt;2.Using Java Configurations&lt;/h3&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Configuration&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;EnableWebMvc&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; CorsConfig&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; implements&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; WebMvcConfigurer &lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Override&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; addCorsMappings&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;CorsRegistry&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; registry&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;        registry&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;addMapping&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;/**&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;            .&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;allowedOrigins&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;https://text-domain1.com&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;https://test-domain2.com&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;            .&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;allowedMethods&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;GET&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;POST&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;PUT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;DELETE&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;3using-applicationproperties-file&quot;&gt;3.Using application.properties file&lt;/h3&gt;
&lt;p&gt;You can directly handle cors in application.properties file or application.yaml file&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;# &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;CORS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; configuration&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;spring&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;mvc&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;cors&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;allowed&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;origins&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;https&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;//test-domain1.com, https://test-domain2.com&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;spring&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;mvc&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;cors&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;allowed&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;methods&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;GET&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;PUT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;DELETE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;spring&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;mvc&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;cors&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;allowed&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;headers&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=*&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I hope this article has provided you with valuable insights into CORS and its implementation in both Node.js and Spring Boot. Stay tuned for more informative articles, and see you in the next one. Follow me for more engaging software engineering content!&lt;/p&gt;
&lt;p&gt;Happy Coding🚀🚀&lt;/p&gt;</content:encoded><category>nodejs</category><category>springboot</category><category>software-development</category><category>backend</category><category>sof</category></item><item><title>Spring Annotations</title><link>https://badaljha.com/blog/spring-annotations/</link><guid isPermaLink="true">https://badaljha.com/blog/spring-annotations/</guid><description>Spring uses an Inversion of control architecture and spring container or ioc container is used to inject objects. Spring container basically manages the…</description><pubDate>Mon, 21 Aug 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Spring uses an Inversion of control architecture and spring container or ioc container is used to inject objects. Spring container basically manages the Beans and Inject into the program whenever we want. A key aspect of Spring is its configuration, which allows developers to define and configure various components and services within the application.&lt;/p&gt;
&lt;p&gt;Annotations in Java are metadata tags that provide additional information about code elements, such as classes, methods, fields, and parameters. Spring annotations are used within the Spring context to enhance the functionality of Spring beans and components. They are processed at runtime by Spring’s IoC container.&lt;/p&gt;
&lt;p&gt;Let’s understand some common spring annotations&lt;/p&gt;
&lt;h2 id=&quot;1-bean-annotation&quot;&gt;1. &lt;code&gt;@Bean&lt;/code&gt; Annotation&lt;/h2&gt;
&lt;p&gt;As in xml based configuration, we used to write bean tags to define custom beans &lt;code&gt;@Bean&lt;/code&gt; is used for the same purpose i.e. to define custom beans. By using &lt;code&gt;@Bean&lt;/code&gt; in a configuration class, you explicitly tell Spring to create and manage a bean based on the method’s return value. This is particularly useful when you need to configure or customize the bean instance before it is added to the Spring application context.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Car&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;      start&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;             System&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;started&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;         }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Configuration&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; AppConfig&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;      @&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Bean&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;       public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; Car&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; car&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;             return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Car&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;       }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here &lt;code&gt;@Configuration&lt;/code&gt; annotation used to mark AppConfig class as configuration class. Configuration classes play a vital role in Spring applications as they define how the application’s beans are created, configured, and wired together. They serve as an alternative to XML-based configuration, allowing developers to configure Spring beans using pure Java code.&lt;/p&gt;
&lt;p&gt;By default, the bean name will be the same as the method name. To customize the bean name, use its’ &lt;em&gt;name&lt;/em&gt;‘ or ‘&lt;em&gt;value&lt;/em&gt;‘ attribute.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Configuration&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; AppConfig&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Bean&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;name &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;customBeanName&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt; // Specify the desired bean name here&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; MyCarService&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; myCarService&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; MyCarServiceImpl&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1 id=&quot;2-component&quot;&gt;2. &lt;code&gt;@Component&lt;/code&gt;&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;@Component&lt;/code&gt; This annotation marks a Java class as a Spring bean, allowing the Spring container to manage it. You can use &lt;code&gt;@ComponentScan&lt;/code&gt; to specify packages for component scanning.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Component&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Car&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;///.....&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1 id=&quot;3service-repository-and-controller&quot;&gt;3.&lt;code&gt;@Service&lt;/code&gt; ,&lt;code&gt;@Repository&lt;/code&gt; and &lt;code&gt;@Controller&lt;/code&gt;&lt;/h1&gt;
&lt;p&gt;These are specialized types of &lt;code&gt;@Component&lt;/code&gt; annotations. They are used to distinguish between different type of beans.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;@Service&lt;/code&gt; annotation is used for service layer. It indicates that the class plays a service role and is typically used in the service layer of the application. Service classes that contain business logic, perform data processing or interact with multiple repositories.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;@Repository&lt;/code&gt; indicates that the class is a data repository, often for storing and retrieving data from a database. spring provides additional benefits for classes annotated with &lt;code&gt;@Repository&lt;/code&gt;, such as exception translation (converting database-specific exceptions to Spring’s DataAccessException hierarchy) and automatic transaction management.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;@Controller&lt;/code&gt; used in spring mvc aapplication used to handle incoming http requests. It indicates that the class is a controller responsible for processing and responding to HTTP requests.&lt;/p&gt;
&lt;h1 id=&quot;3autowired&quot;&gt;3.&lt;code&gt;@Autowired&lt;/code&gt;&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;@Autowired&lt;/code&gt; is the spring annotation that is used for automatic dependency injection. The &lt;code&gt;@Autowired&lt;/code&gt; annotation can be applied to fields, methods, or constructors of a class to indicate that Spring should automatically wire the required dependency. When a bean with a certain type is needed, Spring searches the application context for a suitable candidate and injects it into the annotated field, method, or constructor.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;//constructor injection using @Autowired&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Component&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Bike&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; Motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Autowired&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Bike&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;        this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;motercycle &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;//setter injection&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Component&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Bike&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; Motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Autowired&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; setMotercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;        this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;motercycle &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;//field injection&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Component&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Bike&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Autowired&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; Motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1 id=&quot;4qualifier&quot;&gt;4.&lt;code&gt;@Qualifier&lt;/code&gt;&lt;/h1&gt;
&lt;p&gt;When using &lt;code&gt;@Autowired&lt;/code&gt; spring will search for a bean that matches the type that needs to be injected. If there are multiple beans of the same type are available, you can use the &lt;code&gt;@Qualifier&lt;/code&gt; annotation to specify which bean should be injected.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Service&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; CarService&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; final&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; PaymentService&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; paymentService&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Autowired&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; OrderService&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(@&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Qualifier&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;creditCardPayment&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; PaymentService&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; paymentService&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;        this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;paymentService &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; paymentService&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this example, the creditCardPayment bean is injected into the OrderService using the @Qualifier annotation.&lt;/p&gt;
&lt;h1 id=&quot;5postconstruct-and-predestroy&quot;&gt;5.&lt;code&gt;@PostConstruct&lt;/code&gt; &lt;strong&gt;and&lt;/strong&gt; &lt;code&gt;@PreDestroy&lt;/code&gt;&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;@PostConstruct&lt;/code&gt;: This annotation is used on a method that should be executed after a bean is initialized.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;@PreDestroy&lt;/code&gt;: This annotation is used on a method that should be executed before a bean is destroyed.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There are more annotations &lt;code&gt;@Value&lt;/code&gt; , &lt;code&gt;@RequestMapping&lt;/code&gt; , &lt;code&gt;@PathVariable&lt;/code&gt;, &lt;code&gt;@RequestParam&lt;/code&gt; you can learn more about it in the spring official documentation.&lt;/p&gt;
&lt;p&gt;Spring annotations have changed the way developers work with the Spring Framework. They promote cleaner, more concise code by reducing the need for XML configuration and enabling powerful features like dependency injection and aspect-oriented programming.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Checkout my previous articles&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://badaljha.hashnode.dev/inversion-of-control-and-dependency-injection&quot;&gt;inversion of control and dependency injection&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://badaljha.hashnode.dev/jar-and-fatjar&quot;&gt;Jar and Fat Jar&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I hope you understood the these spring annotations. follow me for more engaging software engineering content!&lt;/p&gt;
&lt;p&gt;Happy Coding🚀🚀&lt;/p&gt;</content:encoded><category>springboot</category><category>spring</category></item><item><title>Understanding Inversion of Control and Dependency Injection</title><link>https://badaljha.com/blog/inversion-of-control-and-dependency-injection/</link><guid isPermaLink="true">https://badaljha.com/blog/inversion-of-control-and-dependency-injection/</guid><description>Inversion of Control and Dependency Injection are two important concepts in the Spring Framework. They are closely related and work together to achieve…</description><pubDate>Tue, 15 Aug 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Inversion of Control and Dependency Injection are two important concepts in the Spring Framework. They are closely related and work together to achieve loose coupling and maintainability in software applications.&lt;/p&gt;
&lt;h1 id=&quot;inversion-of-control-ioc&quot;&gt;Inversion of Control (IOC)&lt;/h1&gt;
&lt;p&gt;Inversion of Control is an architectural principle that tells how components interact within an application. Traditionally a class or component is responsible for creating and managing its dependencies.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Bike&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;      public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; Honda&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; honda&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Honda&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;//or&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Bike&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;      private&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; Honda&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; honda&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;      public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Bike&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;             honda&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=new&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Honda&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;       }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the above code approach class Bike is responsible for creating and managing Honda Object. The provided code snippet, the &lt;code&gt;Bike&lt;/code&gt; class has a tight coupling with the &lt;code&gt;Honda&lt;/code&gt; class&lt;/p&gt;
&lt;h3 id=&quot;issues-with-the-above-approach&quot;&gt;Issues with the above approach&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Low Flexibility:&lt;/strong&gt; The &lt;code&gt;Bike&lt;/code&gt; class directly creates an instance of the &lt;code&gt;Honda&lt;/code&gt; class using the &lt;code&gt;new&lt;/code&gt; keyword. This creates a strong dependency between &lt;code&gt;Bike&lt;/code&gt; and &lt;code&gt;Honda&lt;/code&gt;, making it difficult to switch to a different motorcycle brand without modifying the &lt;code&gt;Bike&lt;/code&gt; class&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Testing is tough:&lt;/strong&gt; Since the &lt;code&gt;Bike&lt;/code&gt; class creates an instance of &lt;code&gt;Honda&lt;/code&gt; internally, it’s hard to replace the &lt;code&gt;Honda&lt;/code&gt; instance with a mock object for testing purposes. This makes unit testing challenging, as you can’t easily isolate the behavior of the &lt;code&gt;Bike&lt;/code&gt; class from the &lt;code&gt;Honda&lt;/code&gt; class.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scalability Problem:&lt;/strong&gt; If you want to introduce more features or variations of bikes (e.g., different brands or models), you’ll need to modify the &lt;code&gt;Bike&lt;/code&gt; class, violating the Open/Closed Principle of SOLID design.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;IoC promotes the idea that a class should not directly create its dependencies but should receive them from an external source. In Spring The IoC container takes responsibility for managing the lifecycle of objects, creating and wiring them together based on the configuration provided by the developer. It decouples the components from their dependencies, promoting loose coupling and making the code more flexible and maintainable.&lt;/p&gt;
&lt;p&gt;We will learn more about spring containers or IOC containers in depth later in a different blog.&lt;/p&gt;
&lt;p&gt;Here’s how you could improve the code using IoC principles:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Bike&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; Motorcycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; motorcycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Bike&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Motorcycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; motorcycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;        this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;motorcycle &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; motorcycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, the &lt;code&gt;Bike&lt;/code&gt; class expects a &lt;code&gt;Motorcycle&lt;/code&gt; instance to be injected during its construction. This way, the responsibility of creating and providing the &lt;code&gt;Motorcycle&lt;/code&gt; instance is shifted outside of the &lt;code&gt;Bike&lt;/code&gt; class, adhering to the IoC principle.&lt;/p&gt;
&lt;h3 id=&quot;advantages&quot;&gt;Advantages:&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;High Flexibility:&lt;/strong&gt; Above code is highly flexible because there is loose coupling between &lt;code&gt;Bike&lt;/code&gt; and &lt;code&gt;Motorcycle.&lt;/code&gt; The &lt;code&gt;Bike&lt;/code&gt; class is no longer tightly coupled to a specific motorcycle brand (e.g., &lt;code&gt;Honda&lt;/code&gt;). You can inject any implementation of the &lt;code&gt;Motorcycle&lt;/code&gt; interface, allowing for easy swapping or extension without modifying the &lt;code&gt;Bike&lt;/code&gt; class&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Testing:&lt;/strong&gt; During unit testing, we can provide a mock implementation of &lt;code&gt;Motorcycle&lt;/code&gt;. to &lt;code&gt;Bike&lt;/code&gt; class to isolate the testing of the &lt;code&gt;Bike&lt;/code&gt; class’s behavior&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scalability:&lt;/strong&gt; The &lt;code&gt;Bike&lt;/code&gt; class is now open for extension (e.g., adding new brands) without modifying its existing code.&lt;/li&gt;
&lt;/ol&gt;
&lt;h1 id=&quot;dependency-injectiondi&quot;&gt;Dependency Injection(DI)&lt;/h1&gt;
&lt;p&gt;DI is a specific technique that implements IoC by managing the injection of dependencies into components. Dependency Injection is a specific implementation of IoC. It is a design pattern that allows the injection of dependencies into a class rather than the class creating or obtaining them directly.&lt;/p&gt;
&lt;p&gt;DI promotes loose coupling between classes, as the dependencies can be easily replaced or changed without modifying the dependent class.&lt;/p&gt;
&lt;p&gt;In the context of Spring, DI means that the Spring IoC container is responsible for resolving and injecting the dependencies into beans when they are created. This is typically done through constructor injection or setter injection.&lt;/p&gt;
&lt;p&gt;There are three types of DI in Spring:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Constructor Injection&lt;/strong&gt;: Constructor is used to inject the dependency in component&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Setter Injection&lt;/strong&gt;: The dependencies are set through setter methods in the dependent class.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Field Injection&lt;/strong&gt;: The dependencies are directly injected into the dependent class’s fields using annotations.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; interface&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;    // Motercycle interface methods&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Honda&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; implements&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; Motercycle &lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;    // Honda implementation of Motercycle interface&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Bike&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; Motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;    // Setter for Motercycle&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; setMotercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;        this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;motercycle &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;    // Constructor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Bike&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;        this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;motercycle &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;//configuration file&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;bean id&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;bikeReference&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;com.example.Honda&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;//setter injection&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;bean id&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;bike1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;com.example.Bike&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;property name&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;honda&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; ref&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;bikeReference&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; /&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;bean&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;//constructor injection&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;bean id&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;bike2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;com.example.Bike&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;constructor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;arg ref&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;bikeReference&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; /&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;bean&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;the &lt;code&gt;constructor-arg&lt;/code&gt; e is used for constructor injection, the property element is used for setter injection, .&lt;/p&gt;
&lt;p&gt;Now xml based configuration is a older approch. Spring Boot encourages the use of Java based configuration using annotations like @Configuration, @Bean, and @Autowired as it provides more flexibility and is more concise and it is less complex than xml based configuration.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Component&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Bike&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; Motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Autowired&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; Bike&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8A3A5A;--shiki-dark:#D9917A&quot;&gt; motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;        this&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;motercycle &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt; motercycle&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is how we do dependency injection using java based configuration using &lt;code&gt;@Autowired&lt;/code&gt; keyword. we use the &lt;code&gt;@Autowired&lt;/code&gt; annotation on the constructor of the &lt;code&gt;Bike&lt;/code&gt; class it can be also be used for setter injection. When Spring initializes the &lt;code&gt;Bike&lt;/code&gt; Bean, it will automatically inject the appropriate implementation of the &lt;code&gt;Motorcycle&lt;/code&gt; interface in this case, the &lt;code&gt;Honda&lt;/code&gt; bean .&lt;/p&gt;
&lt;p&gt;Remember, for &lt;code&gt;@Autowired&lt;/code&gt; to work, make sure you have component scanning enabled in your Spring configuration using &lt;code&gt;@ComponentScan&lt;/code&gt; or similar. Spring will automatically discover and inject the appropriate beans based on the declared types&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes paper ember&quot; style=&quot;--shiki-light:#262019;--shiki-dark:#e9e1d3;--shiki-light-bg:#ebe4d4;--shiki-dark-bg:#221e1a;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;Configuration&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;ComponentScan&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#262019;--shiki-dark:#E9E1D3&quot;&gt;basePackages &lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#2F5D62;--shiki-dark:#A3B47E&quot;&gt;com.example&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#A63A1C;--shiki-dark:#DCA15A&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7A4A0C;--shiki-dark:#E9C48F&quot;&gt; AppConfig&lt;/span&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-light-font-style:italic;--shiki-dark:#A69C8D;--shiki-dark-font-style:italic&quot;&gt;    // Configuration settings&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#685D4F;--shiki-dark:#A69C8D&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By using Java based configuration we no longer need use xml file for the bean configuration. We will learn more about Java base configuration &lt;code&gt;@Autowired&lt;/code&gt; , &lt;code&gt;@Qualifier&lt;/code&gt; in another article.&lt;/p&gt;
&lt;p&gt;In short, Inversion of Control is an architectural principle that tells how components interact within an application, while Dependency Injection is a specific implementation technique that addresses the management of component dependencies there are also other techniques like Event-Driven Architecture, Factory method pattern etc . DI is a means to achieve IoC by allowing components to receive their dependencies from an external source, resulting in more modular, flexible, and maintainable codebases. In Spring spring container also known as the IOC container provides the maintains and injects beans.&lt;/p&gt;
&lt;p&gt;I hope you understood the inversion of control and dependency injection. follow me for more engaging software engineering content!&lt;/p&gt;
&lt;p&gt;Happy Coding🚀🚀&lt;/p&gt;</content:encoded><category>springboot</category><category>java</category><category>software-engineering</category><category>spring</category></item><item><title>Understanding JAR and Fat JAR in Spring Boot</title><link>https://badaljha.com/blog/jar-and-fatjar/</link><guid isPermaLink="true">https://badaljha.com/blog/jar-and-fatjar/</guid><description>A jar(java archive) file package all the java files, associated metadata and resources such as images and configuration in single zip file.</description><pubDate>Sat, 12 Aug 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A jar(java archive) file package all the java files, associated metadata and resources such as images and configuration in single zip file.&lt;/p&gt;
&lt;p&gt;when you build a Spring Boot project, especially using tools like Maven or Gradle, you will often find two types of JAR files generated inside the target directory: the regular JAR and the Fat JAR (also known as Executable JAR or Uber JAR). Let’s delve into each of these:&lt;/p&gt;
&lt;h2 id=&quot;-jar-java-archive&quot;&gt;🔹 JAR (Java Archive):&lt;/h2&gt;
&lt;p&gt;The primary purpose of a JAR file is to conveniently distribute a Java application or library. By packaging all the necessary files into a single JAR, developers can share their Java code easily, making it portable and self-contained.&lt;/p&gt;
&lt;p&gt;Here are some key points:&lt;br/&gt;
👉 Packaging: JAR files can contain compiled Java bytecode (.class files), resources (images, XML files, properties files), and other files needed to run a Java application or library.&lt;br/&gt;
👉Compression: JAR files use compression to reduce the file size, which makes them more efficient for distribution.&lt;br/&gt;
👉 Manifest File: Each JAR file can have a manifest file (META-INF/MANIFEST.MF) that contains metadata about the JAR, such as the main class to be executed, classpath entries, and version information.&lt;br/&gt;
👉 Executable JAR: If the manifest file specifies the main class, the JAR can be executed as a standalone application using the java -jar command.&lt;br/&gt;
👉 Libraries: JAR files are commonly used to distribute Java libraries, which can be easily added to other Java projects by including the JAR file in the classpath.&lt;/p&gt;
&lt;h1 id=&quot;-fat-jar-java-archive&quot;&gt;🔹 Fat JAR (Java Archive):&lt;/h1&gt;
&lt;p&gt;In Spring Boot, a “fat JAR” refers to a type of executable JAR file that includes all the necessary classes and dependencies required to run a Spring Boot application. It is a self-contained JAR file that packages not only the application’s compiled classes but also all the third-party libraries (dependencies) that the application relies on.&lt;/p&gt;
&lt;p&gt;when you create a Fat JAR in a Spring Boot application, it includes an embedded web server, which is typically Tomcat by default. The fat JAR contains everything needed to run the application, making it highly portable and easy to distribute and deploy.&lt;/p&gt;
&lt;p&gt;Key Points:&lt;br/&gt;
🚀 Simplified deployment: With a fat JAR, you don’t need to manage external classpath settings or worry about deploying the application along with its dependencies.&lt;br/&gt;
🚀 Version Management: Fat JARs bundle specific versions of dependencies with the application. This helps avoid conflicts and ensures that the application runs with the intended versions of libraries, regardless of the system’s existing classpath.&lt;/p&gt;
&lt;p&gt;How to create the fat jars:&lt;br/&gt;
👨‍💻 To create a fat JAR in Spring Boot, you can use either the Spring Boot Maven Plugin or the Spring Boot Gradle Plugin&lt;/p&gt;
&lt;p&gt;In this photo Rest_Api-0.0.1-SNAPSHOT.jar is the fat jar and Rest_Api-0.0.1-SNAPSHOT.original.jar is the normal jar.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://cdn.hashnode.com/res/hashnode/image/upload/v1691818339041/e1f1bc87-e696-4567-a30d-cffdb2f4bf82.png&quot; alt/&gt;&lt;/p&gt;
&lt;p&gt;Follow for more such articles🤖🤖&lt;/p&gt;</content:encoded><category>springboot</category><category>java</category><category>spring</category></item></channel></rss>