<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abdulcelil Cercenazi</title>
    <description>The latest articles on DEV Community by Abdulcelil Cercenazi (@jarjanazy).</description>
    <link>https://dev.to/jarjanazy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F595508%2F25bfab90-0a98-4678-8270-4fa40d43b82a.JPG</url>
      <title>DEV Community: Abdulcelil Cercenazi</title>
      <link>https://dev.to/jarjanazy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jarjanazy"/>
    <language>en</language>
    <item>
      <title>An Intro to Software Build Tools</title>
      <dc:creator>Abdulcelil Cercenazi</dc:creator>
      <pubDate>Tue, 15 Mar 2022 08:50:05 +0000</pubDate>
      <link>https://dev.to/jarjanazy/an-intro-to-software-build-tools-49fo</link>
      <guid>https://dev.to/jarjanazy/an-intro-to-software-build-tools-49fo</guid>
      <description>&lt;h2&gt;
  
  
  What are Build Tools 🏗
&lt;/h2&gt;

&lt;p&gt;Build tools are programs that automate the process of building production-ready software from source code. &lt;/p&gt;

&lt;p&gt;This process includes 👇🏽&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Downloading third party dependencies. &lt;/li&gt;
&lt;li&gt;Compiling code with the right configuration.&lt;/li&gt;
&lt;li&gt;Running tests and reporting their results.&lt;/li&gt;
&lt;li&gt;Packaging the compiled code into executable form.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Why use them 🧐
&lt;/h2&gt;

&lt;p&gt;To understand why we might want to use such tools, let's look on how to build a production ready JAR file from Java source code without using a build tool.&lt;/p&gt;

&lt;p&gt;Let's address each task of the ones stated above.&lt;/p&gt;

&lt;h4&gt;
  
  
  Downloading third party dependencies 🗂
&lt;/h4&gt;

&lt;p&gt;In the Java world, dependencies are packaged into Jar files which we have to find and download to our local machine. &lt;/p&gt;

&lt;p&gt;Instead of going through the hassle of finding the Jars online, build tools such as &lt;a href="https://maven.apache.org/"&gt;Maven&lt;/a&gt; and &lt;a href="https://gradle.org/"&gt;Gradle&lt;/a&gt; ask you to define which dependencies you want and they will search through an online repository (&lt;a href="https://mvnrepository.com/"&gt;Maven Repository&lt;/a&gt;) and download the Jars,&lt;/p&gt;




&lt;h4&gt;
  
  
  Compiling code with the right configuration 🔧
&lt;/h4&gt;

&lt;p&gt;A typical Java project might have tens or hundreds of Java classes, each of those have to be compiled with the right configuration.&lt;/p&gt;

&lt;p&gt;Now, running the &lt;code&gt;javac&lt;/code&gt; command with all the classes' names doesn't seem to be a reasonable thing to do as we will have a very long command which we have to update on every new addition of deletion of classes.&lt;/p&gt;

&lt;p&gt;Build tools scripts takes care of this for us, as they ask us to follow a convention for our project directory names and they will ensure that everything is compiled and linked correctly.&lt;/p&gt;




&lt;h4&gt;
  
  
  Running tests and reporting their results🚦
&lt;/h4&gt;

&lt;p&gt;Running the tests without a build tool requires us to run commands from the command line.&lt;br&gt;
Let's take the JUnit testing framework for example:&lt;br&gt;
Running tests in the class &lt;code&gt;MyClassWithUnitTests&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;java -cp /path/to/my.jar:/path/to/junit.jar junit.textui.TestRunner com.mypackage.MyClassWithUnitTests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the long command and the need to be careful with the classpath and the location of the Jars. This will get unmaintainable with a big number of tests.&lt;/p&gt;

&lt;p&gt;On the other hand, build tools like &lt;strong&gt;Maven&lt;/strong&gt; allow us to run the all tests with a single command &lt;code&gt;mvn test&lt;/code&gt;. It also offers the ability to run tests in one or  more classes using only the classes' names.&lt;/p&gt;




&lt;h4&gt;
  
  
  Packaging the compiled code into executable form 💾
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;jar&lt;/code&gt; command allows us to create a Jar file from our compiled classes.&lt;br&gt;
Let's look at a simple example of creating a Jar file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;jar cfe JarExample.jar com.tutorial.JarExample com/tutorial/*.class
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;c&lt;/code&gt; option indicates that we're creating a file.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;f&lt;/code&gt; option specifies the file name (JarExample.jar).&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;e&lt;/code&gt; option specifies our entry point, and the jar command will add it to the generated manifest file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Building tools like Maven does the work for us using a single command &lt;code&gt;mvn package&lt;/code&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Build Tools Examples
&lt;/h2&gt;

&lt;p&gt;The Java ecosystem has &lt;a href="https://www.filecloud.com/blog/2019/06/top-10-build-automation-software-for-2019/#.Yi8B9Y9Bzrc"&gt;many building tools&lt;/a&gt; that are interesting and worth overviewing. We will cover the most important two however.&lt;/p&gt;

&lt;h3&gt;
  
  
  Maven 🖊
&lt;/h3&gt;

&lt;p&gt;It was released in 2004 as an improvement to Apache Ant. It is an XML-based build tool. It has many plugins to customize the build process.&lt;/p&gt;

&lt;p&gt;A Maven project is primarily defined by Project Object Model (POM) file written in XML. This POM.xml file contain the project’s dependencies, plugins, properties, and configuration data. &lt;/p&gt;

&lt;h3&gt;
  
  
  Gradle 🐘
&lt;/h3&gt;

&lt;p&gt;Gradle was on Maven’s concepts, it was introduced as Maven’s successor. Rather than using Maven’s XML-based project configuration, it introduced a domain-specific language (DSL) based on the Groovy and Kotlin programming languages.&lt;/p&gt;




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

&lt;p&gt;Build tools automate the repetitive work (Compile, Link, Test, Bundle) we do to get from code to production ready artifacts.&lt;/p&gt;

</description>
      <category>java</category>
      <category>android</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>What is Inversion of Control (IoC) in Spring</title>
      <dc:creator>Abdulcelil Cercenazi</dc:creator>
      <pubDate>Tue, 22 Feb 2022 13:49:22 +0000</pubDate>
      <link>https://dev.to/jarjanazy/what-is-inversion-of-control-ioc-in-spring-2b1k</link>
      <guid>https://dev.to/jarjanazy/what-is-inversion-of-control-ioc-in-spring-2b1k</guid>
      <description>&lt;h2&gt;
  
  
  Meet Mr. Beans 🥸
&lt;/h2&gt;

&lt;p&gt;A Bean is a &lt;strong&gt;Java object.&lt;/strong&gt; That's it.&lt;br&gt;
Java objects are units that hold data and logic which make our application do its work. Objects can depend on other objects (dependencies) to complete a certain task.&lt;/p&gt;

&lt;p&gt;In Java, creating dependencies is usually done in the constructor of the class using the &lt;code&gt;new&lt;/code&gt; keyword.&lt;/p&gt;


&lt;h3&gt;
  
  
  Example of Traditional Bean Creation 🧑‍🔧
&lt;/h3&gt;

&lt;p&gt;Say we have a &lt;code&gt;DownloadService&lt;/code&gt; that depends on a &lt;code&gt;ConnectionService&lt;/code&gt;.&lt;br&gt;
The &lt;code&gt;ConnectionService&lt;/code&gt; needs an &lt;strong&gt;address&lt;/strong&gt; and a &lt;strong&gt;password&lt;/strong&gt; to set up its connection.&lt;br&gt;
&lt;/p&gt;

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

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ConnectionService&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="c1"&gt;// use address and password to set up the connection  &lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;  
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="c1"&gt;// connecting things  &lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if the &lt;code&gt;DownloadService&lt;/code&gt; wanted to use the &lt;code&gt;ConnectionService&lt;/code&gt; it has to know how to construct it. It has to get the password, the address and call the &lt;code&gt;ConnectionService&lt;/code&gt; constructor.&lt;br&gt;
&lt;/p&gt;

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

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;DownloadService&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getPassword&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getAddress&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
        &lt;span class="n"&gt;connectionService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ConnectionService&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;download&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="n"&gt;connectionService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;connect&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
        &lt;span class="c1"&gt;// download things  &lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;  
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getAddress&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"address"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getPassword&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"pass"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is all fine and dandy, but what if we had 10 or more classes that use the &lt;code&gt;ConnnectionService&lt;/code&gt;? Then they all have to know how to get the password, the address and call the constructor. &lt;/p&gt;

&lt;p&gt;This is certainly not maintainable and hard to follow around.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inversion of Control (IoC) to the Rescue 🏋️‍♂️
&lt;/h2&gt;

&lt;p&gt;In the software development world, there is a well-known rule called the Single Responsibility Principle. It urges developers to assign a single responsibility to a method or a class.&lt;br&gt;
So in our above example, the &lt;code&gt;DownloadService&lt;/code&gt; should not know how to construct the complicated &lt;code&gt;ConnectionService&lt;/code&gt;, it should just use it.&lt;/p&gt;

&lt;p&gt;What the &lt;strong&gt;IoC pattern&lt;/strong&gt; suggests is that the &lt;code&gt;DownloadService&lt;/code&gt; service should invert the responsibility of creating the &lt;code&gt;ConnectionService&lt;/code&gt; to some other entity. &lt;/p&gt;

&lt;p&gt;This entity could be, for example, a &lt;strong&gt;factory class&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

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

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ConnectionService&lt;/span&gt; &lt;span class="nf"&gt;createConnectionService&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getPassword&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getAddress&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ConnectionService&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getAddress&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"address"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getPassword&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"pass"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then in the &lt;code&gt;DownloadService&lt;/code&gt;, we call the &lt;code&gt;ConnectionFactoy&lt;/code&gt; which knows how to construct a &lt;code&gt;ConnectionService&lt;/code&gt; object.&lt;br&gt;
&lt;/p&gt;

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

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;DownloadService&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="n"&gt;connectionService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ConnectionFactory&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;createConnectionService&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;download&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="n"&gt;connectionService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;connect&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
        &lt;span class="c1"&gt;// download things  &lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  IoC in Spring ☘️
&lt;/h2&gt;

&lt;p&gt;Spring offers a helping hand using the &lt;strong&gt;&lt;a href="https://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/beans.html"&gt;IoC Spring Container&lt;/a&gt;&lt;/strong&gt;. All we have to do is describe our beans to the container in a configuration class and it will give them to us whenever we want.&lt;/p&gt;

&lt;p&gt;Let's test this with our above code:&lt;br&gt;
&lt;/p&gt;

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

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ConnectionService&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="c1"&gt;// use address and password to setup the connection  &lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;  
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Connecting..."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
        &lt;span class="c1"&gt;// connecting things  &lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's introduce the configuration class which will provide us with the &lt;code&gt;ConnectionService&lt;/code&gt; bean.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Configuration&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppConfiguration&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
  &lt;span class="nd"&gt;@Bean&lt;/span&gt;  
  &lt;span class="nc"&gt;ConnectionService&lt;/span&gt; &lt;span class="nf"&gt;connectionService&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getAddress&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getPassword&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ConnectionService&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getAddress&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"address"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getPassword&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"pass"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two new things to note here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;@Configuration&lt;/code&gt; annotation indicates that the class has &lt;code&gt;@Bean&lt;/code&gt; definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;@Bean&lt;/code&gt; declares the object returned from the method as a Bean to be managed by Spring IoC container.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, let's check out our &lt;code&gt;DownloadService&lt;/code&gt; class which will have the &lt;code&gt;ConnectionService&lt;/code&gt; Bean injected in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DownloadService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;ConnectionService&lt;/span&gt; &lt;span class="n"&gt;connectionService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  

    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;DownloadService&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ConnectionService&lt;/span&gt; &lt;span class="n"&gt;connectionService&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;connectionService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;connectionService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;download&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="n"&gt;connectionService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;connect&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
        &lt;span class="c1"&gt;// download things  &lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An important detail to pay attention to here is the &lt;code&gt;@Component&lt;/code&gt; and &lt;code&gt;@Autowired&lt;/code&gt; annotations. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;@Component&lt;/code&gt; will tell Spring to manage the &lt;code&gt;DownloadService&lt;/code&gt; as a Bean. Doing this, Spring will create the class and inject any Beans it depends on (&lt;code&gt;ConnectionService&lt;/code&gt; in this example) into the class.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;@Autowired&lt;/code&gt; will tell Spring to get the objects declared in the constructor parameters from the Spring IoC container, which in our case was declared in the &lt;code&gt;AppConfiguration&lt;/code&gt; class.&lt;/p&gt;




&lt;p&gt;Now, that was a good move to have the IoC principle implemented in our system. Spring, however, offers an even more convenient way to handle IoC.&lt;/p&gt;

&lt;p&gt;We can remove the  &lt;code&gt;AppConfiguration&lt;/code&gt; class and simply have the logic of instantiating the &lt;code&gt;ConnectionService&lt;/code&gt; inside of it and have Spring create the Bean and make it ready for use.&lt;br&gt;
&lt;/p&gt;

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

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ConnectionService&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="c1"&gt;// use address and password to setup the connection  &lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getAddress&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getPassword&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="c1"&gt;// connecting things  &lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;  

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getAddress&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"address"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getPassword&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"pass"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note the &lt;code&gt;@Component&lt;/code&gt; annotation which tells Spring to treat this class as a Bean and have it in the Spring IoC ready for the &lt;code&gt;DownloadService&lt;/code&gt; to use.&lt;/p&gt;

&lt;h2&gt;
  
  
  Beans' Life Cycle 🌕 🌖 🌗 🌘 🌑
&lt;/h2&gt;

&lt;p&gt;Spring Bean factory manages the lifecycle of beans created through the Spring container and controls the creation and destruction of the beans.&lt;/p&gt;

&lt;p&gt;Let's explore the phases of a Spring Bean's life cycle:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Bean definition. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's defined by the &lt;code&gt;@Bean&lt;/code&gt; annotation over methods in the configuration file (the one annotated with &lt;code&gt;@Configuration&lt;/code&gt; file). &lt;/li&gt;
&lt;li&gt;Or using the &lt;code&gt;@Component&lt;/code&gt; annotation and its derivatives (&lt;code&gt;@Service&lt;/code&gt;, &lt;code&gt;@Controller&lt;/code&gt;, etc..) over classes.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Bean Instantiation.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spring instantiate bean objects and loads the objects into the ApplicationContext.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Populating Bean properties.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spring scans the beans and sets relevant properties as id, scope, and the default values.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Pre-Initialization&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spring’s &lt;a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/config/BeanPostProcessor.html"&gt;BeanPostProcessors&lt;/a&gt; do their work.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;AfterPropertiesSet&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spring executes the &lt;code&gt;afterPropertiesSet()&lt;/code&gt; methods of the beans which implement InitializingBean.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Custom Initialization&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spring triggers any initialization methods that we define. &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Post-initialization&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spring’s &lt;a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/config/BeanPostProcessor.html"&gt;BeanPostProcessors&lt;/a&gt; do their work for one more time.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Ready&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Bean is ready and injected into all other dependencies.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Pre-Destroy&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spring triggers &lt;code&gt;@PreDestroy&lt;/code&gt; annotated methods in this phase &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Destroy&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spring executes the destroy() methods of DisposableBean implementations&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Custom Destruction&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spring triggers any custom destruction methods that we've defined.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion ✍️
&lt;/h2&gt;

&lt;p&gt;The principle of Inversion of Control helps us write clean, maintainable code.&lt;/p&gt;

&lt;p&gt;This can be achieved by separating the logic of creating complex objects into a class of their own (separation of concerns).&lt;/p&gt;

&lt;p&gt;Spring offers a neat solution using Spring's &lt;code&gt;ApplicationContext&lt;/code&gt; and Spring's IoC container. The container treats our objects as Beans which have life cycles managed by Spring and custom methods that we can introduce.&lt;/p&gt;

</description>
      <category>java</category>
      <category>tutorial</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Get To Know Annotations in Java</title>
      <dc:creator>Abdulcelil Cercenazi</dc:creator>
      <pubDate>Fri, 04 Feb 2022 18:04:22 +0000</pubDate>
      <link>https://dev.to/jarjanazy/get-to-know-annotations-in-java-4l41</link>
      <guid>https://dev.to/jarjanazy/get-to-know-annotations-in-java-4l41</guid>
      <description>&lt;h2&gt;
  
  
  What Are Annotations? 🧐
&lt;/h2&gt;

&lt;p&gt;They allow us to mark (annotate) certain Java language elements as having certain properties (annotations).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What Elements?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; Package, Class, Method, Field, Parameter.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What Are They Used For? 🤓
&lt;/h2&gt;

&lt;p&gt;There has been a growing trend in the Java world towards annotating elements as having particular attributes that indicate they should be processed in special ways by &lt;em&gt;development tools&lt;/em&gt;, &lt;em&gt;deployment tools&lt;/em&gt;, or &lt;em&gt;run-time libraries&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Mainly, they are used either on:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compile time 👇&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To examine classes and issue compilation errors or warnings.&lt;/li&gt;
&lt;li&gt;To create new source files.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Run time 👇&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;take actions to do accomplish some task based on the annotations, for example to validate parameters given to a method.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Let's look at two examples using Java annotations 👀
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  Embed instructions for the Java compiler or source code processing tools (please generate getters for my fields using Lombok annotations).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Getter&lt;/span&gt; &lt;span class="nd"&gt;@Setter&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Shipment&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;serialNumber&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;  Embed instructions that can be read on run time (using reflection) by your app or third party libraries (Spring for example).
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;






&lt;h2&gt;
  
  
  Creating Your Own Annotations 🤝
&lt;/h2&gt;

&lt;p&gt;Let's create an annotation called &lt;code&gt;CoolAnnotation&lt;/code&gt; which we want to be accessible in compilation time and usable only by types (Classes, Interfaces or Enums).&lt;br&gt;
We also want to add a &lt;code&gt;name&lt;/code&gt; value which must be specified by the user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Retention&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;RetentionPolicy&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;SOURCE&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
&lt;span class="nd"&gt;@Target&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ElementType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;TYPE&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nd"&gt;@interface&lt;/span&gt; &lt;span class="nc"&gt;CoolAnnotation&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's use it on a class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@CoolAnnotation&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"myName"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ShipmentService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// buisness things &lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  How To Use This Annotation? 🧠
&lt;/h2&gt;

&lt;h3&gt;
  
  
  On Runtime 👈
&lt;/h3&gt;

&lt;p&gt;We can read those annotations at runtime and choose to take actions based on their values&lt;/p&gt;

&lt;p&gt;Let's introduce the same cool annotation we had before, but this time let's make it visible on runtime&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Retention&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;RetentionPolicy&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;RUNTIME&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
&lt;span class="nd"&gt;@Target&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ElementType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;TYPE&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nd"&gt;@interface&lt;/span&gt; &lt;span class="nc"&gt;CoolAnnotation&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And annotate it to the service class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@CoolAnnotation&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"myName"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ShipmentService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's test it on runtime&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.junit.jupiter.api.Test&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;static&lt;/span&gt; &lt;span class="n"&gt;org&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;junit&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;jupiter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;api&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Assertions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ShipmentAnnotationTest&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
  &lt;span class="nd"&gt;@Test&lt;/span&gt;  
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;testAnnotation&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ShipmentService&lt;/span&gt;  
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;  
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getAnnotation&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CoolAnnotation&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  

        &lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"myName"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// lights green &lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  On Compile time 👈
&lt;/h3&gt;

&lt;p&gt;This is done via compile time &lt;strong&gt;annotation processors&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Processors can&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Create new resource files.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create new code source files.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Issue note, warnings and errors which can cause the compilation to fail (picked up by the IDE).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;They can't modify existing resources or source code files.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Writing a processor ✍️
&lt;/h3&gt;

&lt;p&gt;It's a topic that requires a post on its own, however you can watch this excellent  &lt;a href="https://youtu.be/IPlDL4EsY08?t=811"&gt;YouTube&lt;/a&gt; video on the topic.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion 👩‍🎓 🧑‍🎓
&lt;/h2&gt;

&lt;p&gt;Annotations allow us to give Java elements some properties (meta-data) that are used either at compile time or runtime.&lt;/p&gt;

&lt;p&gt;This offers major benefits to the Java world like offering static type checking at compile time, automated processing at runtime. This is used heavily in frameworks like Spring and Hibernate. &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>java</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Don't Miss These Two Important Hibernate Tips</title>
      <dc:creator>Abdulcelil Cercenazi</dc:creator>
      <pubDate>Mon, 01 Nov 2021 18:40:56 +0000</pubDate>
      <link>https://dev.to/jarjanazy/dont-miss-these-two-important-hibernate-tips-36ef</link>
      <guid>https://dev.to/jarjanazy/dont-miss-these-two-important-hibernate-tips-36ef</guid>
      <description>&lt;h2&gt;
  
  
  Avoid Bi-directional Relationships ☝️
&lt;/h2&gt;

&lt;p&gt;In bi-directional relations we have to remember to set both ends of the relation to reference each other whenever we want to persist one of them.&lt;/p&gt;

&lt;p&gt;If we don't one of the two ends will have a null reference to the other and we would mess up our data 😿&lt;/p&gt;

&lt;p&gt;Moreover it's very uncommon for us to traverse relationships both ways.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Let's look at an example 👇
&lt;/h3&gt;

&lt;p&gt;Say we have Order and OrderLine entities that have a one-to-many relationship.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let’s do it the bi-directional way 🚫&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Entity&lt;/span&gt;
&lt;span class="nd"&gt;@Table&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"ORDERS"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@Data&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Id&lt;/span&gt;
    &lt;span class="nd"&gt;@GeneratedValue&lt;/span&gt;
    &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@OneToMany&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mappedBy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"order"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cascade&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CascadeType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ALL&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;OrderLine&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;orderLines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@Entity&lt;/span&gt;
&lt;span class="nd"&gt;@Data&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderLine&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Id&lt;/span&gt;
    &lt;span class="nd"&gt;@GeneratedValue&lt;/span&gt;
    &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@ManyToOne&lt;/span&gt;
    &lt;span class="nc"&gt;Order&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, to make sure we have both ends set up we need to&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="nc"&gt;OrderLine&lt;/span&gt; &lt;span class="n"&gt;orderLine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;OrderLine&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;orderLine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setOrder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getOrderLines&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderLine&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;entityManager&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;persist&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;And now let’s do it the unidirectional way ✅&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Entity&lt;/span&gt;
&lt;span class="nd"&gt;@Table&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"ORDERS"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@Data&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Id&lt;/span&gt;
    &lt;span class="nd"&gt;@GeneratedValue&lt;/span&gt;
    &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@OneToMany&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cascade&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CascadeType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ALL&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nd"&gt;@JoinColumn&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"ORDER_ID"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;OrderLine&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;orderLines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@Entity&lt;/span&gt;
&lt;span class="nd"&gt;@Data&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderLine&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Id&lt;/span&gt;
    &lt;span class="nd"&gt;@GeneratedValue&lt;/span&gt;
    &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Doing the following should persist both entities.&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="nc"&gt;OrderLine&lt;/span&gt; &lt;span class="n"&gt;orderLine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;OrderLine&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getOrderLines&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderLine&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;entityManager&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;persist&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Limit Relationships Via Numeric IDs ☝🏾
&lt;/h2&gt;

&lt;p&gt;For entities that have different contexts, represent the relationship between them using Ids and not actual classes.&lt;/p&gt;

&lt;p&gt;Say we have an OrderLine entity and a Product entity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Entity&lt;/span&gt;
&lt;span class="nd"&gt;@Data&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderLine&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Id&lt;/span&gt;
    &lt;span class="nd"&gt;@GeneratedValue&lt;/span&gt;
    &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@ManyToOne&lt;/span&gt;
    &lt;span class="nc"&gt;Product&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@Entity&lt;/span&gt;
&lt;span class="nd"&gt;@NoArgsConstructor&lt;/span&gt;
&lt;span class="nd"&gt;@AllArgsConstructor&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Product&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Id&lt;/span&gt;
    &lt;span class="nd"&gt;@GeneratedValue&lt;/span&gt;
    &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case if we fetch an OrderLine object then the Product will be fetched eagerly as well because the default behavior of &lt;code&gt;ManyToOne&lt;/code&gt; is an &lt;code&gt;EAGER&lt;/code&gt; fetch 🏃🏽‍♂️&lt;/p&gt;

&lt;p&gt;Plus BDD principals says we should keep the Product and OrderLine entities separate from each other because they belong to different contexts.&lt;/p&gt;

&lt;p&gt;The best way is to include Product in OrderLine as an ID.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example time 🏋🏽‍♂️
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Entity&lt;/span&gt;
&lt;span class="nd"&gt;@Data&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderLine&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Id&lt;/span&gt;
    &lt;span class="nd"&gt;@GeneratedValue&lt;/span&gt;
    &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;productId&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the code to set the product ID would be like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Product&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Product&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
&lt;span class="n"&gt;entityManager&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;persist&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  

&lt;span class="nc"&gt;Order&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  

&lt;span class="nc"&gt;OrderLine&lt;/span&gt; &lt;span class="n"&gt;orderLine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;OrderLine&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getOrderLines&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderLine&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
&lt;span class="n"&gt;orderLine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setProductId&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getId&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;  

&lt;span class="n"&gt;entityManager&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;persist&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Credits 🙌🏾
&lt;/h2&gt;

&lt;p&gt;This post was made mainly derived from Victor Rentea's &lt;a href="https://www.youtube.com/channel/UC_RV_tw0mK1aStb6h1eX77g"&gt;Designing Expressive and Performant Persistence Models for Relational DBs&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>java</category>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Be careful Of This Java Optional Method</title>
      <dc:creator>Abdulcelil Cercenazi</dc:creator>
      <pubDate>Wed, 13 Oct 2021 22:21:16 +0000</pubDate>
      <link>https://dev.to/jarjanazy/be-careful-of-this-java-optional-method-439</link>
      <guid>https://dev.to/jarjanazy/be-careful-of-this-java-optional-method-439</guid>
      <description>&lt;h2&gt;
  
  
  Let's Remember Java Optional 🤓
&lt;/h2&gt;

&lt;p&gt;According to &lt;strong&gt;Oracle&lt;/strong&gt; it's "A container object which may or may not contain a non-null value."&lt;br&gt;
Optional was introduced in Java 8 and has been used by the SpringBoot team in many projects.&lt;/p&gt;



&lt;p&gt;The most common usage of Optionals is in the Spring Data project. Let's look at the &lt;code&gt;JpaRepository&lt;/code&gt; interface and an example method.&lt;br&gt;
Say we have a User entity with an Id type of integer and that we have a JpaRepository for it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Repository&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IUserRepo&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;JpaRepository&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  
&lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByUserName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;userName&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We defined a method that searches for a user via their user name and returns an &lt;code&gt;Optional&lt;/code&gt; of a User.&lt;/p&gt;




&lt;h2&gt;
  
  
  Optional's Convenience Methods 🙌
&lt;/h2&gt;

&lt;p&gt;Optional comes in with many method meant to enable us to write clean and readable code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;map(..).or(...)&lt;/li&gt;
&lt;li&gt;map(...).orElse(...)&lt;/li&gt;
&lt;li&gt;check out Oracle's &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html"&gt;docs&lt;/a&gt; for the full list.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;However, there is one method with a dangerously unexpected behavior&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Meet The &lt;code&gt;orElse&lt;/code&gt; Method 👀
&lt;/h2&gt;

&lt;p&gt;According to Oracle's doc:&lt;/p&gt;

&lt;blockquote&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public T orElse(T other) 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Return the value if present, otherwise return other.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, we can add a method call as the parameter of the orElse, which will be run if the Optional is empty, right?&lt;/p&gt;

&lt;p&gt;Yes, that's correct, BUT, what if I tell you that it will run anyways regardless of the presence of the value in Optional or not.&lt;/p&gt;




&lt;p&gt;Let's test it ✍️&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Test&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;orElseTest&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
&lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;orElse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;someMethod&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;  
    &lt;span class="n"&gt;assertThat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;isEqualTo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;someMethod&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
&lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I am running !!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"hola"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The test does pass, but we notice that on the console we have the string &lt;strong&gt;"I am running"&lt;/strong&gt; printed out.&lt;/p&gt;




&lt;h3&gt;
  
  
  Why is that? 🤨
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Java runs the method to provide a value to be returned in the &lt;em&gt;Else&lt;/em&gt; case. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  So Be Careful ⛔️
&lt;/h2&gt;

&lt;p&gt;We want to be careful if the method inside the &lt;code&gt;orElse&lt;/code&gt; might have a side effect, because it will be run anyways.&lt;/p&gt;




&lt;h2&gt;
  
  
  What To Do then?
&lt;/h2&gt;

&lt;p&gt;You can use the &lt;code&gt;OrElseGet&lt;/code&gt; method which takes a supplier method to be executed if the Optional exists&lt;/p&gt;

&lt;p&gt;Check out this wonderful &lt;a href="https://dev.to/alexismanin/comment/1iol8"&gt;comment&lt;/a&gt; for more details.&lt;/p&gt;




&lt;h3&gt;
  
  
  👉🏾 Also check out &lt;a href="https://dev.to/jarjanazy/how-to-escape-nullpointerexceptions-in-java-using-optional-pek"&gt;How to escape NullPointerExceptions in Java using Optional&lt;/a&gt;
&lt;/h3&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>🖐 5 Things Every Developer Must Know about Software Architecture</title>
      <dc:creator>Abdulcelil Cercenazi</dc:creator>
      <pubDate>Sun, 26 Sep 2021 23:02:03 +0000</pubDate>
      <link>https://dev.to/jarjanazy/5-things-every-developer-must-know-about-software-architecture-35ki</link>
      <guid>https://dev.to/jarjanazy/5-things-every-developer-must-know-about-software-architecture-35ki</guid>
      <description>&lt;h2&gt;
  
  
  1. Software Architecture Isn’t About Big Design Upfront ☝️
&lt;/h2&gt;

&lt;p&gt;Big designs upfront are usually done in waterfall models of software development.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The waterfall model is predicted to be bad by its own inventor :)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The upfront design should be a basic, good one done with an iterative and incremental process.&lt;/p&gt;

&lt;h3&gt;
  
  
  When Do We Stop The Architecture Planning? ⛔️
&lt;/h3&gt;

&lt;p&gt;When we have a solid understanding of the following aspects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The significant architectural drivers

&lt;ul&gt;
&lt;li&gt;Cost, Response Time, etc...&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Requirements.&lt;/li&gt;
&lt;li&gt;Constraints.&lt;/li&gt;
&lt;li&gt;The context and scope of the product.&lt;/li&gt;
&lt;li&gt;The significant design choices.&lt;/li&gt;
&lt;li&gt;Technology used.&lt;/li&gt;
&lt;li&gt;Is our application a Monolith or is a set of Microservices?&lt;/li&gt;
&lt;li&gt;The risks associated with the product.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2. Every Software Team/Product Needs To Consider Architecture And Needs To Have A Technical Leader ✌️
&lt;/h2&gt;

&lt;p&gt;This is crucial to have consistent patterns in the product.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. What's The Software Architect Role About 👨‍🔧
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Coding.&lt;/li&gt;
&lt;li&gt;Keeping an eye on what’s going on with the code base.&lt;/li&gt;
&lt;li&gt;Coaching.&lt;/li&gt;
&lt;li&gt;Collaboration with others to incrementally draw the architecture of the application.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It’s a continuous role that spans the life of the project.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. An Architect Doesn’t Need To Use UML 📐
&lt;/h2&gt;

&lt;p&gt;There are several other tools that  enable us to create architecture diagrams using GUIs or even using code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/plantuml-stdlib/C4-PlantUML"&gt;C4-PlantUML&lt;/a&gt; (code based)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.diagrams.net/"&gt;Diagrams.net&lt;/a&gt; (GUI based)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://structurizr.com/"&gt;Structurizr&lt;/a&gt; (both code and GUI based)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  5. A Good Software Architecture Enables Agility ❤️
&lt;/h2&gt;




&lt;h2&gt;
  
  
  Credits 🙌
&lt;/h2&gt;

&lt;p&gt;This post has been prepared using the lecture of Simon Brown Five &lt;a href="https://www.youtube.com/watch?v=9Az0q2XHtH8&amp;amp;t=543s"&gt;Things Every Developer Should Know about Software Architecture&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>python</category>
    </item>
    <item>
      <title>How To Do System Performance Testing</title>
      <dc:creator>Abdulcelil Cercenazi</dc:creator>
      <pubDate>Sat, 18 Sep 2021 20:48:42 +0000</pubDate>
      <link>https://dev.to/jarjanazy/how-to-do-system-performance-testing-259k</link>
      <guid>https://dev.to/jarjanazy/how-to-do-system-performance-testing-259k</guid>
      <description>&lt;h2&gt;
  
  
  What Is Meant By System Performance 🧐
&lt;/h2&gt;

&lt;p&gt;It's usually described by scalability and usability. However, there are two more important criteria that we should consider systems' performance and the method to test it.&lt;/p&gt;




&lt;h3&gt;
  
  
  What are Those Criteria 👀
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Throughput&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The rate at which a system can process information.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Latency&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The duration between the initiation of an action and its result.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those two together represent the system's responsiveness, even though they are not necessarily related.&lt;/p&gt;




&lt;h2&gt;
  
  
  How To Test The Performance 🤨
&lt;/h2&gt;

&lt;p&gt;From the previous two criteria we can see that measuring throughput and latency and comparing them against some values we see fit is basically our testing process.&lt;/p&gt;

&lt;h3&gt;
  
  
  It's not that simple though. Why 😲
&lt;/h3&gt;

&lt;p&gt;We have so many variables that can affect our system performance test.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The test network traffic (if it's run on the organization's network).&lt;/li&gt;
&lt;li&gt;The OS status (is it performing some home cleaning process?)&lt;/li&gt;
&lt;li&gt;The run environment status (is the JVM performing garbage collection?)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  How To Do It Then? 💁‍♂️
&lt;/h2&gt;

&lt;p&gt;We need to take two things into consideration in order to perform informative performance tests.&lt;/p&gt;

&lt;h3&gt;
  
  
  1.  Control the variables ⚙️
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;We should have a dedicated testing environment which is a clone of our production environment.&lt;/li&gt;
&lt;li&gt;We should control the processes of this environment.

&lt;ul&gt;
&lt;li&gt;Network traffic.&lt;/li&gt;
&lt;li&gt;Garbage collection, etc..&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Test the performance of our performance tests ⚖️
&lt;/h3&gt;

&lt;p&gt;Wait, What? why do you want to test your test?&lt;/p&gt;

&lt;p&gt;Well, if the test is slow to start and initiate that would affect the actual code performance measurements and we would have failing tests for code that is actually fast.&lt;/p&gt;

&lt;p&gt;How to make sure that our tests are fast enough in order not to make our actual code test fail?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We should test the code using stub methods that do the bare minimum.

&lt;ul&gt;
&lt;li&gt;Return hard coded values for example.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Then, if the test passes (meets the throughput and latency requirements) do we insert and test the actual code.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  This post was mainly derived from Dave Farley's &lt;a href="https://www.youtube.com/watch?v=7koEc8iX7AM&amp;amp;t=13s"&gt;How To Test Software Performance&lt;/a&gt; YouTube video 🙏🏾
&lt;/h3&gt;

</description>
      <category>webdev</category>
      <category>tutorial</category>
      <category>testing</category>
      <category>programming</category>
    </item>
    <item>
      <title>Read Spring Properties Like a Pro</title>
      <dc:creator>Abdulcelil Cercenazi</dc:creator>
      <pubDate>Sat, 04 Sep 2021 22:57:43 +0000</pubDate>
      <link>https://dev.to/jarjanazy/read-spring-configs-like-a-pro-1do3</link>
      <guid>https://dev.to/jarjanazy/read-spring-configs-like-a-pro-1do3</guid>
      <description>&lt;h2&gt;
  
  
  Configuration Management Is Important ☝️
&lt;/h2&gt;

&lt;p&gt;The necessity of reading configuration in a clean, organized way is growing rapidly especially with the spread of cloud application development and micro-service architecture which requires a lot of integration and connection settings.&lt;/p&gt;




&lt;h2&gt;
  
  
  What's Wrong With The Typical Way Of Config Reading 🤷
&lt;/h2&gt;

&lt;p&gt;Nothing. However, it can get complicated and messy when we want to inject many of those configs in our code.&lt;/p&gt;

&lt;p&gt;Let's look at an example with a single properties file and a test&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;application.properties&lt;/strong&gt; ⚙️&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight properties"&gt;&lt;code&gt;&lt;span class="py"&gt;demo.test.name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;testName  &lt;/span&gt;
&lt;span class="py"&gt;demo.test.age&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;16&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;DemoApplicationTests.java&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@SpringBootTest&lt;/span&gt;  
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DemoApplicationTests&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
   &lt;span class="nd"&gt;@Value&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"${demo.test.name}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
   &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  

  &lt;span class="nd"&gt;@Value&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"${demo.test.age}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
   &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  

  &lt;span class="nd"&gt;@Test&lt;/span&gt;  
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;loadProperty&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
      &lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"testName"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
      &lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
  &lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  Now, imagine we have 5 or 10 of those properties, that would cause our code to be cluttered and hard to follow 🥴
&lt;/h4&gt;

&lt;h2&gt;
  
  
  @ConfigurationProperties To The Rescue 🤠
&lt;/h2&gt;

&lt;p&gt;It allows us to inject values from the &lt;code&gt;application.properties&lt;/code&gt; file into a custom class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;  
&lt;span class="nd"&gt;@ConfigurationProperties&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prefix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"demo.test"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
&lt;span class="nd"&gt;@Setter&lt;/span&gt;  
&lt;span class="nd"&gt;@Getter&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DemoTestConfigs&lt;/span&gt;  
&lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;@Component&lt;/code&gt; annotation is to tell Spring to manage this class as a bean and provide it for injection.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;@ConfigurationProperties&lt;/code&gt; is what does the magic

&lt;ul&gt;
&lt;li&gt;It looks inside the property files in the class path and loads the properties that start with &lt;code&gt;demo.test&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;The Lombok &lt;code&gt;@Setter&lt;/code&gt; is to enable &lt;code&gt;@ConfigurationProperties&lt;/code&gt; to populate the values in the &lt;code&gt;DemoTestConfigs&lt;/code&gt; class.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  We can then simply inject the &lt;code&gt;DemoTestConfigs&lt;/code&gt; bean into our services. 🤝
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Let's check it in a test
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@SpringBootTest&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ConfigurationPropertiesTest&lt;/span&gt;  
&lt;span class="o"&gt;{&lt;/span&gt;  
  &lt;span class="nd"&gt;@Autowired&lt;/span&gt;  
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;DemoTestConfigs&lt;/span&gt; &lt;span class="n"&gt;demoTestConfigs&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  

  &lt;span class="nd"&gt;@Test&lt;/span&gt;  
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;loadPropertiesUsingConfigurationProperties&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;  
        &lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"testName"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;demoTestConfigs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;  
        &lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;demoTestConfigs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getAge&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;  
  &lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;p&gt;We've seen how &lt;code&gt;@ConfigurationProperties&lt;/code&gt; helps us bundle our similar configurations into a single component class which we can inject and use instead of specifying each and every one of them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code On &lt;a href="https://github.com/Jarjanazy/spring-tutorials/tree/master/demo"&gt;GitHub 💻&lt;/a&gt;
&lt;/h3&gt;

</description>
      <category>webdev</category>
      <category>tutorial</category>
      <category>cloud</category>
      <category>java</category>
    </item>
    <item>
      <title>The Simple Guide To Dockerizing Spring Boot</title>
      <dc:creator>Abdulcelil Cercenazi</dc:creator>
      <pubDate>Sat, 21 Aug 2021 16:30:56 +0000</pubDate>
      <link>https://dev.to/jarjanazy/the-simple-guide-to-dockerizing-spring-boot-og4</link>
      <guid>https://dev.to/jarjanazy/the-simple-guide-to-dockerizing-spring-boot-og4</guid>
      <description>&lt;h2&gt;
  
  
  Why Should I Use A Docker Container? 🤨
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt; They are a great replacement for Virtual Machines.

&lt;ul&gt;
&lt;li&gt;Why?

&lt;ul&gt;
&lt;li&gt;VMs are big in size (Gigabytes) and slow to run (1-5 mins).

&lt;ul&gt;
&lt;li&gt;Containers on the other hand:

&lt;ul&gt;
&lt;li&gt;Use a shared operating system, this means you can leave behind the useless &lt;strong&gt;99.9&lt;/strong&gt; percent VM junk.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;They are Linux processes that are isolated from each other.&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.netapp.com%2Fmedia%2FScreen-Shot-2018-03-20-at-9.24.09-AM_tcm19-56643.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.netapp.com%2Fmedia%2FScreen-Shot-2018-03-20-at-9.24.09-AM_tcm19-56643.png" alt="enter image description here"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Containers enable us to easily pack, ship, and run any application as a lightweight, portable, self-sufficient container, which can run virtually anywhere.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Containers are easy to deploy in the cloud.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Why Should I Wrap My App In A Docker Container 🤓
&lt;/h3&gt;

&lt;p&gt;Well, to give our app all the benefits we've just described.&lt;/p&gt;




&lt;h2&gt;
  
  
  How To Do It? 👀
&lt;/h2&gt;

&lt;p&gt;First, create a JAR file from your application.&lt;br&gt;
Given that you are using Maven, run the following command in the &lt;code&gt;pom.xml&lt;/code&gt; file directory&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mvn clean package
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;This will create a &lt;code&gt;target&lt;/code&gt; directory, in which you can find your JAR file.&lt;/p&gt;

&lt;p&gt;Then, create a Docker image using the JAR file we've just created.&lt;br&gt;
Start by creating a file named &lt;code&gt;Dockerfile&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; openjdk:8  &lt;/span&gt;
&lt;span class="k"&gt;ADD&lt;/span&gt;&lt;span class="s"&gt; target/demo-0.0.1-SNAPSHOT.jar demo-0.0.1-SNAPSHOT.jar  &lt;/span&gt;
&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt;  8085  &lt;/span&gt;
&lt;span class="k"&gt;ENTRYPOINT&lt;/span&gt;&lt;span class="s"&gt; java -jar -Dspring.profiles.active=dev demo-0.0.1-SNAPSHOT.jar&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means ☝️&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Using the OpenJDK’s Java 8 machine (FROM)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add this JAR to Docker’s host and name it demo-0.0.1-SNAPSHOT.jar (ADD)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Expose this app at port &lt;strong&gt;8085&lt;/strong&gt; (EXPOSE)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And start the application using this command (ENTRYPOINT)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Then build the Docker image&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; docker build -t demo .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This means ☝️&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Build an image and give it the tag demo.&lt;/li&gt;
&lt;li&gt;Look for the &lt;code&gt;Dockerfile&lt;/code&gt; in the current directory.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Finally, run the image.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -p 8080:8085 demo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This means ☝️&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run the image called &lt;code&gt;demo&lt;/code&gt;, and map the requests coming from the host machine on port &lt;strong&gt;8080&lt;/strong&gt; to the Docker port &lt;strong&gt;8085&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  How To Send Requests To Our App Inside Docker? 🤩
&lt;/h2&gt;

&lt;p&gt;Simply send the requests local host on the port we've specified &lt;strong&gt;&lt;a href="http://localhost:8080/" rel="noopener noreferrer"&gt;http://localhost:8080/&lt;/a&gt;&lt;/strong&gt; and they will mapped to the port &lt;strong&gt;8085&lt;/strong&gt; inside Docker which will lead to our app.&lt;/p&gt;




&lt;h2&gt;
  
  
  Last Words ✍️
&lt;/h2&gt;

&lt;p&gt;The Docker world is a wonderful and big place, I recommend you diver deeper into it. &lt;br&gt;
Check out those great resources.&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=p28piYY_wv8" rel="noopener noreferrer"&gt;Full YouTube Tutorial💎&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.baeldung.com/dockerizing-spring-boot-application" rel="noopener noreferrer"&gt;Dockerizing a Spring Boot Application⚓️&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Code On &lt;a href="https://github.com/Jarjanazy/spring-boot-docker-demo" rel="noopener noreferrer"&gt;GitHub💻&lt;/a&gt;
&lt;/h2&gt;

</description>
      <category>tutorial</category>
      <category>docker</category>
      <category>webdev</category>
      <category>cloud</category>
    </item>
    <item>
      <title>Intro To Spring's Aspects</title>
      <dc:creator>Abdulcelil Cercenazi</dc:creator>
      <pubDate>Sat, 14 Aug 2021 15:48:21 +0000</pubDate>
      <link>https://dev.to/jarjanazy/intro-to-spring-s-aspects-4pee</link>
      <guid>https://dev.to/jarjanazy/intro-to-spring-s-aspects-4pee</guid>
      <description>&lt;h2&gt;
  
  
  What Is A Spring Aspect? 😲
&lt;/h2&gt;

&lt;p&gt;Simply put, it's a reusable part of code (Advice) that is injected into a certain part (joinPoint) of the application (defined by Pointcuts) at runtime.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use it? 🤨
&lt;/h2&gt;

&lt;p&gt;Address common concerns across our application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Like what?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Logging 📝&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Transaction management 🗄&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Caching 🔗&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Security 🔐&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  It's used all over the place in Spring framework
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Aspect Oriented Programming (AOP) is one of the two core concepts that the Spring framework is built on, the second one being &lt;strong&gt;Dependency Injection&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Let's Get To Know the agents behind Spring Aspects 🥷🏽
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Join point 🔧
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A joinpoint is a candidate point in the Program Execution of the application where an aspect can be plugged in.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This point could be a method being called, an exception being thrown, or even a field being modified.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;These are the points where your aspect’s code can be inserted into the normal flow of your application to add new behavior.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Advice 📖
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;  The code that addresses system wide concerns (logging, security checks, etc...) representing the action to perform at a joinpoint specified by a pointcut.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Pointcut 📌
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;  A pointcut is an expression that defines at what joinpoints, the associated Advice should be applied.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Aspect 🔍
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;  Aspect is the class that defines the aspect’s behavior and a pointcut defining where the aspect should be executed.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Ready For Some Coding? 🤩
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Let's write a simple use-case of logging something to the console on the call of a certain method.
&lt;/h3&gt;

&lt;p&gt;We'll start by adding the maven dependency for AOP&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.springframework.boot&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;spring-boot-starter-aop&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, let's create a class and annotate it with &lt;code&gt;@Aspect&lt;/code&gt; to mark that this class will contain &lt;strong&gt;&lt;em&gt;Advice methods&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;Pointcuts&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
We also need to annotate it with &lt;code&gt;@Component&lt;/code&gt; so it becomes managed by Spring.&lt;/p&gt;

&lt;p&gt;This class will handle the simple task of logging to the console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="nd"&gt;@Aspect&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LoggingAspect&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Pointcut&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"@annotation(Loggable)"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;logAllMethodCallsPointcut&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;    
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="nd"&gt;@Before&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"logAllMethodCallsPointcut()"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;logAllMethodCallsAdvice&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"From Aspect"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to create the loggable annotation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Target&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ElementType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;METHOD&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
&lt;span class="nd"&gt;@Retention&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;RetentionPolicy&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;RUNTIME&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nd"&gt;@interface&lt;/span&gt; &lt;span class="nc"&gt;Loggable&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  What did we just write? 👀
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;@Pointcut("@annotation(Loggable)")&lt;/code&gt; declares the &lt;code&gt;logAllMethodCallsPointcut&lt;/code&gt; method as a pointcut for all method annotated with &lt;code&gt;Loggable&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;code&gt;@Before("logAllMethodCallsPointcut()")&lt;/code&gt;  declares &lt;code&gt;logAllMethodCallsAdvice&lt;/code&gt; advice which will be called before any method annotated with &lt;code&gt;Loggable&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Now let's create a service that will act as our join point 🧠
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Service&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HomeService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
  &lt;span class="nd"&gt;@Loggable&lt;/span&gt;
  &lt;span class="c1"&gt;// this here is what's called a join point  &lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;homePage&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;  
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"From Service"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when we call the &lt;code&gt;homePage&lt;/code&gt; method, the &lt;code&gt;logAllMethodCallsAdvice&lt;/code&gt; advice method will be called before it.&lt;/p&gt;

&lt;h3&gt;
  
  
  So we will see the following logs 👇
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;From Aspect &lt;br&gt;
 From Service&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Remind Me Why Are We doing This? 😢
&lt;/h2&gt;

&lt;p&gt;This example seems useless for the simple case we have, &lt;strong&gt;however&lt;/strong&gt; imagine having a system with &lt;strong&gt;hundreds&lt;/strong&gt; of methods that you want to log something about them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Would you call the same code before each and everyone? 🤔&lt;/li&gt;
&lt;li&gt;What happens if you want to change the implementation of the logging process?

&lt;ul&gt;
&lt;li&gt;You need to search for all the usages and change them 😖&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Or, you can create a service and call it from all those places, but imagine having more things other than logging.

&lt;ul&gt;
&lt;li&gt;You need to create  and call separate service for everyone 😤&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Aspects are a life saver as you can now see. 😍😍
&lt;/h3&gt;




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

&lt;ul&gt;
&lt;li&gt;Aspects is one of the most important concepts of the Spring framework.&lt;/li&gt;
&lt;li&gt;It's quite handy and provides a mean for clean reusable code.&lt;/li&gt;
&lt;li&gt;It has a wide range of use-cases (We only discuss logging).&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  For more details you can check this rich &lt;a href="https://www.journaldev.com/2583/spring-aop-example-tutorial-aspect-advice-pointcut-joinpoint-annotations"&gt;resource 🤝&lt;/a&gt;.
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Source code on &lt;a href="https://github.com/Jarjanazy/Spring-AOP-Demo"&gt;GitHub 💻&lt;/a&gt;.
&lt;/h3&gt;

</description>
      <category>webdev</category>
      <category>tutorial</category>
      <category>programming</category>
      <category>java</category>
    </item>
    <item>
      <title>Handle Spring Exceptions Like A Pro</title>
      <dc:creator>Abdulcelil Cercenazi</dc:creator>
      <pubDate>Wed, 04 Aug 2021 19:42:00 +0000</pubDate>
      <link>https://dev.to/jarjanazy/handle-spring-exceptions-like-a-pro-1m5e</link>
      <guid>https://dev.to/jarjanazy/handle-spring-exceptions-like-a-pro-1m5e</guid>
      <description>&lt;h2&gt;
  
  
  Typical Exception Handling In Java ☕️
&lt;/h2&gt;

&lt;p&gt;It's a common thing in Java to try-catch parts of our code that we except to fail for some reason&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Missing files, corrupt data, etc...
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="n"&gt;buggyMethod&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Done!"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;RuntimeException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;  
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"An error happened!"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Exception Handling In Spring 🍃
&lt;/h2&gt;

&lt;p&gt;Let's view the workflow of Spring as a web framework:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Listen to requests from the client.&lt;/li&gt;
&lt;li&gt;Take some actions based on our business logic.&lt;/li&gt;
&lt;li&gt;Return a response to the client containing the result of our work.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, we ideally want to catch any exception (error) that might arise at level 2 (action taking).&lt;br&gt;
We can write a try catch block at every controller method that handles the exceptions in a standard way 🙌🏽&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RestController&lt;/span&gt;
&lt;span class="nd"&gt;@RequiredArgsConstructor&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TestController&lt;/span&gt;  
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;ExceptionHandler&lt;/span&gt; &lt;span class="n"&gt;exceptionHandler&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/test1"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;test1&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;  
        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;  
          &lt;span class="c1"&gt;// test 1 things  &lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;  
          &lt;span class="n"&gt;exceptionHandler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;handleException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
      &lt;span class="o"&gt;}&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  
    &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/test2"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;test2&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;  
        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;  
          &lt;span class="c1"&gt;// test 2 things  &lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;  
         &lt;span class="n"&gt;exceptionHandler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;handleException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
      &lt;span class="o"&gt;}&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👎🏽 The problem with this approach however is that it get quite tedious when we have many more controller methods.&lt;/p&gt;




&lt;h3&gt;
  
  
  Why capture all exceptions? and not just let them occur?🤷🏼
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;We want our application to be user friendly and handle all edge cases, thus we want it to return responses with standard format.&lt;/li&gt;
&lt;li&gt;We might also want to log those exceptions in a backlog to get back to them and investigate them, or do whatever we like with them.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  @ControllerAdvice To The Rescue💪🏾
&lt;/h2&gt;

&lt;p&gt;The idea is that we declare a method that will handle any unhandled exceptions in the application.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to do it? 👀
&lt;/h3&gt;

&lt;p&gt;First, we need to declare a class and annotate it with &lt;code&gt;@ControllerAdvice&lt;/code&gt;.&lt;br&gt;
Then, we declare methods, each handling a class of exception.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@ControllerAdvice&lt;/span&gt; &lt;span class="nd"&gt;@Slf4j&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GlobalErrorHandler&lt;/span&gt;  
&lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="nd"&gt;@ResponseStatus&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;INTERNAL_SERVER_ERROR&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
    &lt;span class="nd"&gt;@ResponseBody&lt;/span&gt;  
    &lt;span class="nd"&gt;@ExceptionHandler&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;methodArgumentNotValidException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="c1"&gt;// you can take actions based on the exception  &lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"An unexpected error has happened"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"An internal error has happened, please report the incident"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
  &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="nd"&gt;@ResponseStatus&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;BAD_REQUEST&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
    &lt;span class="nd"&gt;@ResponseBody&lt;/span&gt;  
    &lt;span class="nd"&gt;@ExceptionHandler&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;InvalidParameterException&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;invalidParameterException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;InvalidParameterException&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"This is a BAD REQUEST"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
   &lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What does the above code do?☝️
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Declares two methods that will be run whenever an exception of class &lt;code&gt;Exception&lt;/code&gt;, &lt;code&gt;InvalidParameterException&lt;/code&gt; (or subclass of them) is thrown and not handled locally in their thread of execution.&lt;/li&gt;
&lt;li&gt;They return a string response back the client.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that we can specify more than one handler in the class annotated with &lt;code&gt;@ControllerAdvice&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;Now, let's code some endpoints for us to validate against. Let's code three endpoints&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One that handles the exception thrown.&lt;/li&gt;
&lt;li&gt;The other two leave the handling to the global exception handler
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RestController&lt;/span&gt; &lt;span class="nd"&gt;@RequiredArgsConstructor&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TestController&lt;/span&gt;  
&lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/buggyMethod"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;testMeWithExceptionHandler&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;  
        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;  
            &lt;span class="n"&gt;buggyMethod&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Done!"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
      &lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;RuntimeException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;  
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"An error happened!"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
        &lt;span class="o"&gt;}&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  
    &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/potentialBuggyMethod"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;testMeWithoutExceptionHandler&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;  
        &lt;span class="n"&gt;undercoverBuggyMethod&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Done!"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
      &lt;span class="o"&gt;}&lt;/span&gt;  
    &lt;span class="nd"&gt;@PostMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/invalidParamMethod"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;testForInvalidParam&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;  
        &lt;span class="n"&gt;buggyParameters&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Done"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;buggyMethod&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;  
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;RuntimeException&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;undercoverBuggyMethod&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;  
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;RuntimeException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"oops"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;buggyParameters&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;  
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;InvalidParameterException&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt; 
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Let's Verify It With Some Tests 🧠
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@WebMvcTest&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;controllers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;TestController&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GlobalExceptionHandlerTest&lt;/span&gt;  
&lt;span class="o"&gt;{&lt;/span&gt;  
  &lt;span class="nd"&gt;@Autowired&lt;/span&gt;  
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;MockMvc&lt;/span&gt; &lt;span class="n"&gt;mockMvc&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  

  &lt;span class="nd"&gt;@Test&lt;/span&gt;  
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;givenAGetRequestToBuggyEndPoint_DetectErrorMessage&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="nc"&gt;MvcResult&lt;/span&gt; &lt;span class="n"&gt;mvcResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mockMvc&lt;/span&gt;  
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;perform&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/buggyMethod"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;  
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;andExpect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;isOk&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;  
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;andReturn&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mvcResult&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getResponse&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getContentAsString&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
        &lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"An error happened!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
  &lt;span class="o"&gt;}&lt;/span&gt;  
    &lt;span class="nd"&gt;@Test&lt;/span&gt;  
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;givenAGetRequestToPotentialBuggyMethod_DetectErrorMessage&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="nc"&gt;MvcResult&lt;/span&gt; &lt;span class="n"&gt;mvcResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mockMvc&lt;/span&gt;  
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;perform&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/potentialBuggyMethod"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;  
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;andExpect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;is5xxServerError&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;  
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;andReturn&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mvcResult&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getResponse&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getContentAsString&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
        &lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"An internal error has happened, please report the incident"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
  &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="nd"&gt;@Test&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;givenAPostRequestToBuggyMethod_DetectInvalidParameterErrorMessage&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;  
&lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="nc"&gt;MvcResult&lt;/span&gt; &lt;span class="n"&gt;mvcResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mockMvc&lt;/span&gt;  
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;perform&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/invalidParamMethod"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;  
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;andExpect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;isBadRequest&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;  
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;andReturn&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
      &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mvcResult&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getResponse&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getContentAsString&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
      &lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"This is a BAD REQUEST"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;p&gt;Unexpected and general errors should be handled elegantly to sustain a smooth experience for our application clients. This is best done using Spring's &lt;strong&gt;ControllerAdvice&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Check this article for more details &lt;a href="https://www.baeldung.com/exception-handling-for-rest-with-spring"&gt;Error Handling for REST with Spring&lt;/a&gt;👈
&lt;/h3&gt;




&lt;h3&gt;
  
  
  Check the code on &lt;a href="https://github.com/Jarjanazy/spring-global-exception-handling"&gt;GitHub&lt;/a&gt;🥷
&lt;/h3&gt;

</description>
      <category>webdev</category>
      <category>java</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>6 Responsibilities Of Controllers (Spring As An Example)</title>
      <dc:creator>Abdulcelil Cercenazi</dc:creator>
      <pubDate>Wed, 21 Jul 2021 17:24:28 +0000</pubDate>
      <link>https://dev.to/jarjanazy/6-responsibilities-of-controllers-spring-as-an-example-4h3k</link>
      <guid>https://dev.to/jarjanazy/6-responsibilities-of-controllers-spring-as-an-example-4h3k</guid>
      <description>&lt;h2&gt;
  
  
  What Is A Controller Anyways? 🧐
&lt;/h2&gt;

&lt;p&gt;It acts as the entry point to our backend application from the outside world.&lt;br&gt;
By outside world I mean&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Frontend applications that run on browsers.&lt;/li&gt;
&lt;li&gt;Other backend applications that run on servers.&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  How It's Done?👀
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The controller defines entry points that clients from the outside world can talk with using REST operations (GET, POST, DELETE, etc..).&lt;/li&gt;
&lt;li&gt;A request that has the matching properties defined by a certain entry point will trigger the function of that entry point.

&lt;ul&gt;
&lt;li&gt;The function then calls business logic functions that will do some stuff and return a response to the client.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RestController&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GradeController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/grade/{id}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;getGradeById&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@PathVariable&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;  
        &lt;span class="c1"&gt;// business stuff  &lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The example above describes an entry point at &lt;code&gt;baseUrl/grade/someGradeId&lt;/code&gt; that is run when a GET request is made with a URL matching the pattern (&lt;code&gt;baseUrl/grade/1&lt;/code&gt; for example).&lt;/p&gt;


&lt;h2&gt;
  
  
  What Are The Responsibilities Of A Controller?☝️
&lt;/h2&gt;

&lt;p&gt;Let's list them in order from the moment a request is received to the moment of returning a response.&lt;/p&gt;
&lt;h3&gt;
  
  
  1️⃣ Listening to HTTP requests👂🏼
&lt;/h3&gt;

&lt;p&gt;What URL/HTTP Types/Request Parameters should trigger this function?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the example above, we are saying that the function getGradeById will be run when a GET request is made to the url &lt;code&gt;baeUrl/grade/{id}&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt;
  
  
  2️⃣ Deserializing the input from the incoming request ✍️
&lt;/h3&gt;

&lt;p&gt;Turn the request body into Java objects. Turn the request parameters into Integers, Strings, etc...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RestController&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GradeController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="nd"&gt;@PostMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/grade/add"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;getGradeById&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@RequestBody&lt;/span&gt; &lt;span class="nc"&gt;Grade&lt;/span&gt; &lt;span class="n"&gt;grade&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;  
        &lt;span class="c1"&gt;// business stuff  &lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Grade class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Grade&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;  
  &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
  &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;letter&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
  &lt;span class="c1"&gt;// getters and setters and constructors&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, turn the POST request body into a Grade object.&lt;br&gt;
The POST requests body should something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"letter"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"A+"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  3️⃣ Validating the Deserialized objects🧙
&lt;/h3&gt;

&lt;p&gt;We can specify validation rules against the inputs our controllers take. An error will be thrown if the rules weren't met.&lt;br&gt;
For example, the id parameter shouldn't be negative.&lt;/p&gt;
&lt;h4&gt;
  
  
  We can do the validation manually
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RestController&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GradeController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="nd"&gt;@PostMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/grade/add"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;getGradeById&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@RequestBody&lt;/span&gt; &lt;span class="nc"&gt;Grade&lt;/span&gt; &lt;span class="n"&gt;grade&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;grade&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getId&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Exception&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Or we can use Spring's validation mechanisms
&lt;/h4&gt;

&lt;p&gt;The Grade class with the validation annotation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Grade&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nd"&gt;@Min&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; 
  &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
  &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;letter&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The controller with the annotation to activate the validation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@PostMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/grade/add"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;getGradeById&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@RequestBody&lt;/span&gt; &lt;span class="nd"&gt;@Validated&lt;/span&gt; &lt;span class="nc"&gt;Grade&lt;/span&gt; &lt;span class="n"&gt;grade&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
&lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="c1"&gt;// business stuff  &lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Learn more about Spring validation from my other blog &lt;a href="https://dev.to/jarjanazy/validate-your-endpoints-using-spring-4l3e"&gt;post&lt;/a&gt;.👈&lt;/p&gt;




&lt;h3&gt;
  
  
  4️⃣ Calling business logic 💼
&lt;/h3&gt;

&lt;p&gt;The body of the controller function is executed. It should ideally call functions from the service layer which runs business code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RestController&lt;/span&gt; &lt;span class="nd"&gt;@RequiredArgsConstructor&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GradeController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;GradeService&lt;/span&gt; &lt;span class="n"&gt;gradeService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
  &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/grade"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;getGradeById&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="n"&gt;gradeService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;doThings&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
  &lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note the usage of Lombok to write cleaner code. Check out my bog post about Lombok &lt;a href="https://dev.to/jarjanazy/speed-up-your-java-coding-using-lombok-4b53"&gt;here&lt;/a&gt;.👈🏻&lt;/p&gt;




&lt;h3&gt;
  
  
  5️⃣ Serializing the output of the controller function 🛠
&lt;/h3&gt;

&lt;p&gt;The returned value from the function is is turned into a HTTP response.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RestController&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GradeController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/grade/add"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;APIResponse&lt;/span&gt; &lt;span class="nf"&gt;getGradeById&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;  
    &lt;span class="o"&gt;{&lt;/span&gt;  
       &lt;span class="c1"&gt;// do things  &lt;/span&gt;
       &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;APIResponse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"success"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
  &lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="nd"&gt;@AllArgsConstructor&lt;/span&gt;  
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;APIResponse&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the response will be a 200 with a JSON value of&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"message"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"success"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  6️⃣ Handle Exceptions ⛔️
&lt;/h3&gt;

&lt;p&gt;If an exception happens and goes unhandled by the business logic code, the controller translates it into a meaningful response.&lt;/p&gt;




&lt;h2&gt;
  
  
  Important Note ☢️
&lt;/h2&gt;

&lt;p&gt;As we've seen, the controller has a lot to do! It's very important not to include business logic code in the controller.&lt;/p&gt;

&lt;p&gt;I've seen this mistake done many times. I've also seen controllers with thousands of lines of business code.&lt;/p&gt;

&lt;p&gt;This leads to creating a timed bomb that could explode on a Monday at 9 PM 💣&lt;/p&gt;




&lt;h3&gt;
  
  
  This post was inspired from &lt;a href="https://reflectoring.io/spring-boot-web-controller-test/"&gt;this&lt;/a&gt; amazing post.
&lt;/h3&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>java</category>
    </item>
  </channel>
</rss>
