<?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>Spring Builders: Aniwange Tertese Amos</title>
    <description>The latest articles on Spring Builders by Aniwange Tertese Amos (@aniwange).</description>
    <link>https://springbuilders.dev/aniwange</link>
    <image>
      <url>https://springbuilders.dev/images/RUzb1Vv2NuxZfVmmkrY86SakjcDrb1EXVxOw1Bwl7lw/rs:fill:90:90/g:sm/mb:500000/ar:1/aHR0cHM6Ly9zcHJp/bmdidWlsZGVycy5k/ZXYvdXBsb2Fkcy91/c2VyL3Byb2ZpbGVf/aW1hZ2UvMjY4Lzgx/ODE5MWQ2LWE0MTAt/NGIzMi1iYzY2LWNk/YmY2MmM4Y2VlOS5q/cGc</url>
      <title>Spring Builders: Aniwange Tertese Amos</title>
      <link>https://springbuilders.dev/aniwange</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://springbuilders.dev/feed/aniwange"/>
    <language>en</language>
    <item>
      <title>Unlocking the Power of Spring Proxies: Enhance Your Java Applications with Dynamic Behavior</title>
      <dc:creator>Aniwange Tertese Amos</dc:creator>
      <pubDate>Thu, 16 May 2024 10:38:48 +0000</pubDate>
      <link>https://springbuilders.dev/aniwange/unlocking-the-power-of-spring-proxies-enhance-your-java-applications-with-dynamic-behavior-a7p</link>
      <guid>https://springbuilders.dev/aniwange/unlocking-the-power-of-spring-proxies-enhance-your-java-applications-with-dynamic-behavior-a7p</guid>
      <description>&lt;p&gt;In the realm of Java development, building robust and maintainable applications often entails managing cross-cutting concerns like logging, security, and transaction management. While traditional approaches might involve scattering these concerns throughout your codebase, Spring Framework offers a more elegant solution through its proxying mechanism.&lt;/p&gt;

&lt;p&gt;At the heart of this mechanism lies the concept of Spring proxies – dynamic objects that intercept method invocations to provide additional functionality. In this article, we'll explore how Spring proxies work and how they can be leveraged to enhance the functionality of your applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding Spring Proxies:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Spring proxies serve as intermediaries between client code and target objects. They intercept method calls to the target object, allowing for the introduction of cross-cutting concerns and dynamic behavior without altering the target object's code directly. This separation of concerns promotes modularity, maintainability, and code reusability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types of Spring Proxies:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Spring supports two main types of proxies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;JDK Dynamic Proxies: These proxies are interface-based and are created at runtime using Java's reflection API. They require the target object to implement at least one interface.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CGLIB Proxies: When the target object doesn't implement any interfaces, Spring resorts to using CGLIB (Code Generation Library) to create proxies. CGLIB proxies work by subclassing the target object at runtime.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Practical Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Consider a scenario where we have a &lt;code&gt;StudentRepository&lt;/code&gt; interface for interacting with student data. We want to add logging functionality to the &lt;code&gt;findById&lt;/code&gt; method without modifying the existing implementation. By leveraging Spring proxies and aspect-oriented programming (AOP), we can achieve this seamlessly:&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;@Aspect&lt;/span&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;LoggingAspect&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;"execution(* com.example.repository.StudentRepository.findById(..))"&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;logBeforeFindById&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;"Logging: Finding student by ID"&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;With this aspect in place, Spring dynamically creates a proxy around the &lt;code&gt;StudentRepository&lt;/code&gt; bean, intercepting calls to the &lt;code&gt;findById&lt;/code&gt; method and executing the logging advice before delegating the call to the actual implementation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Benefits of Spring Proxies:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Modularity: Proxies enable the modularization of cross-cutting concerns, leading to cleaner and more maintainable code.&lt;/li&gt;
&lt;li&gt;Dynamic Behavior: Proxies allow for the addition of behavior at runtime, facilitating features like logging, caching, and security.&lt;/li&gt;
&lt;li&gt;Non-Invasive: Proxies promote non-invasive development by allowing developers to enhance functionality without modifying existing code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Spring proxies are a powerful tool in the Java developer's arsenal, offering a flexible way to address cross-cutting concerns and introduce dynamic behavior into applications. By understanding how proxies work and how to leverage them effectively, developers can write cleaner, more maintainable code while enhancing the functionality of their applications.&lt;/p&gt;

&lt;p&gt;So, the next time you find yourself needing to add logging, security, or any other cross-cutting concern to your Java application, consider harnessing the power of Spring proxies to unlock new possibilities in your development journey.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Understanding HTTP Verbs: POST vs. PUT in RESTful APIs</title>
      <dc:creator>Aniwange Tertese Amos</dc:creator>
      <pubDate>Sat, 13 Apr 2024 11:47:17 +0000</pubDate>
      <link>https://springbuilders.dev/aniwange/understanding-http-verbs-post-vs-put-in-restful-apis-13jo</link>
      <guid>https://springbuilders.dev/aniwange/understanding-http-verbs-post-vs-put-in-restful-apis-13jo</guid>
      <description>&lt;p&gt;When designing RESTful APIs, one of the fundamental decisions is choosing the appropriate HTTP verb for creating resources. While the HTTP standard doesn't explicitly specify whether POST or PUT should be preferred for a Create operation, understanding the nuances between these verbs is crucial for designing an effective API. Let's delve into the considerations and implications of using POST and PUT verbs for resource creation.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Surrogate and Natural Keys:
The choice between POST and PUT for resource creation can be elucidated by examining the distinction between Surrogate and Natural Keys. The server typically generates surrogate keys, while the client supplies Natural Keys.
POST:
Creates a sub-resource under the request URI.
It is suitable when the server needs to return the URI of the created resource.
Utilized in scenarios where the URI of the resource is determined by the server, such as in the Cash Card API where the URI depends on the generated ID.
PUT:
Creates or replaces a resource at a specific request URI.
Appropriate when the resource URI is known at creation time.&lt;/li&gt;
&lt;li&gt;Resources and Sub-Resources:
Another perspective to discern between POST and PUT is by considering resources and sub-resources.
POST:
Creates a sub-resource under or within the request URI.
Applied in scenarios where the client initiates the creation of a child resource.
PUT:
Creates or replaces a resource at a specific request URI.
Suitable for directly creating or updating a resource.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Response Body and Status Code:&lt;br&gt;
Deciding whether to allow PUT for resource creation also involves determining the appropriate response status code and body.&lt;br&gt;
Option 1:&lt;br&gt;
Return 201 CREATED (for object creation) or 200 OK (for object replacement).&lt;br&gt;
Recommended to include the object in the response body, especially if server-side data modifications are made.&lt;br&gt;
Option 2:&lt;br&gt;
Return 204 NO CONTENT and an empty response body.&lt;br&gt;
Suitable when the client doesn't require any information back after the PUT operation.&lt;/p&gt;

&lt;p&gt;In summary, while both POST and PUT can be utilized for creating resources, understanding the nuances and implications of each verb is essential for designing a robust and intuitive API. By carefully considering factors such as key types, resource relationships, and response handling, API designers can make informed decisions to ensure the effectiveness and clarity of their APIs. &lt;/p&gt;

&lt;h1&gt;
  
  
  SpringBoot #RESTful #API #HappyCoding
&lt;/h1&gt;

</description>
    </item>
  </channel>
</rss>
