<?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: Tommy</title>
    <description>The latest articles on DEV Community by Tommy (@tommyc).</description>
    <link>https://dev.to/tommyc</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%2F363315%2Fa0daf538-dc80-45e0-b512-b31eccd207ed.jpg</url>
      <title>DEV Community: Tommy</title>
      <link>https://dev.to/tommyc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tommyc"/>
    <language>en</language>
    <item>
      <title>Spring JPA - @EntityGraph</title>
      <dc:creator>Tommy</dc:creator>
      <pubDate>Fri, 02 May 2025 00:43:51 +0000</pubDate>
      <link>https://dev.to/tommyc/spring-jpa-entitygraph-24go</link>
      <guid>https://dev.to/tommyc/spring-jpa-entitygraph-24go</guid>
      <description>&lt;p&gt;By default, Spring Data JPA loads related data lazily, which can cause the N+1 problem — one query for the main data and more for each related item. &lt;strong&gt;@EntityGraph&lt;/strong&gt; annotation fixes this by letting you load related data in a single query, without changing your entity mapping. It’s simple, improves performance, and avoids writing custom JOIN FETCH queries.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;Let’s say you have a Post entity that references an Author.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Entities&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Entity
public class Post {
    @Id
    private Long id;
    private String title;

    @ManyToOne(fetch = FetchType.LAZY)
    private Author author;
}

@Entity
public class Author {
    @Id
    private Long id;
    private String name;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Repository&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public interface PostRepository extends JpaRepository&amp;lt;Post, Long&amp;gt; {

    // attributePaths specifies related entities to eagerly load
    @EntityGraph(attributePaths = {"author"})
    List&amp;lt;Post&amp;gt; findAll();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if you create a simple controller to return all posts and have SQL logging enabled, you'll see the queries printed in the console.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;@EntityGraph&lt;/code&gt;, only one query runs to fetch both the post and its author. Without it, multiple queries are executed — one for the posts and one for each author — which is the N+1 problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✅ Using @EntityGraph&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Only one query — fast and efficient.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select p.id, p.title, a.id, a.name
from post p
left join author a on p.author_id = a.id
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;🚨 Not Using @EntityGraph&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;N+1 queries — slow and wasteful.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select * from post;
select * from author where id = ?;
select * from author where id = ?;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For more details on how to use &lt;code&gt;@EntityGraph&lt;/code&gt; with Spring Data JPA, check out the &lt;a href="https://docs.spring.io/spring-data/jpa/reference/jpa/query-methods.html#jpa.entity-graph" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>java</category>
      <category>springboot</category>
      <category>hibernate</category>
    </item>
    <item>
      <title>SQL Database Normalization with Easy Examples</title>
      <dc:creator>Tommy</dc:creator>
      <pubDate>Sun, 18 Feb 2024 19:17:12 +0000</pubDate>
      <link>https://dev.to/tommyc/sql-database-normalization-5bi0</link>
      <guid>https://dev.to/tommyc/sql-database-normalization-5bi0</guid>
      <description>&lt;p&gt;In database design, there are three main ways to organize data: first normal form (1NF), second normal form (2NF), and third normal form (3NF). While there are more advanced forms like Boyce and Codd Normal Form (BCNF or 3.5NF) and fourth normal form (4NF), we'll focus on the basics: &lt;strong&gt;1NF, 2NF, and 3NF&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Normalization is Important?
&lt;/h2&gt;

&lt;p&gt;Normalization is crucial because it prevents anomalies and eliminates redundant data. Anomalies include Update Anomalies, Insertion Anomalies, and Deletion Anomalies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update Anomalies:&lt;/strong&gt; These occur when updating data inconsistently, leading to discrepancies or errors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Insertion Anomalies:&lt;/strong&gt; When adding new data, insertion anomalies arise due to the necessity of adding incomplete information because certain attributes depend on other attributes not present.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deletion Anomalies:&lt;/strong&gt; Deleting data can inadvertently remove unrelated information due to dependencies within the database.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;First Normal Form (1NF)&lt;/strong&gt;:
&lt;/h2&gt;

&lt;p&gt;In 1NF, each column in a table contains atomic values, meaning that each value is indivisible. For example, in a table storing student information, the Phone Numbers column should not contain multiple phone numbers separated by commas.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe8t6p2xy49plqruf6uxg.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe8t6p2xy49plqruf6uxg.PNG" alt="1NF Mistake Example" width="648" height="211"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Instead, each phone number should be in its own row.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxwoubxv474rm9so9dh90.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxwoubxv474rm9so9dh90.PNG" alt="1NF Example" width="638" height="208"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Second Normal Form (2NF)&lt;/strong&gt;:
&lt;/h2&gt;

&lt;p&gt;2NF builds on 1NF by ensuring that every non-key attribute is fully dependent on the primary key. In other words, each column must depend on the entire primary key, not just a part of it. For instance, in a table that stores product order details, if the primary key is OrderID, the Gender column should depend on OrderID.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3r8tlbfkrobc1u46meso.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3r8tlbfkrobc1u46meso.PNG" alt="2NF Mistake Example" width="639" height="205"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case, the Gender column is not fully dependent on the primary key; hence we remove it. This violates the 2NF because the Gender information is not directly related to the primary key.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fosw2u7771ze11e9s3ux7.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fosw2u7771ze11e9s3ux7.PNG" alt="2NF Example" width="634" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Third Normal Form (3NF)&lt;/strong&gt;:
&lt;/h2&gt;

&lt;p&gt;3NF further refines the structure by removing transitive dependencies. This means that non-key attributes should not depend on other non-key attributes. In other words, if columns A, B, and C are in a table R, and A determines B, and B determines C, then C is transitively dependent on A.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnh105cemmztdkjj9gq4g.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnh105cemmztdkjj9gq4g.PNG" alt="3NF Mistake Example" width="638" height="203"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, the Category column depends solely on the Product Name column creating a transitive dependency and violating the 3NF. Hence, we move them out to separate tables.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F43lm4q2gafye63eg4q7w.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F43lm4q2gafye63eg4q7w.PNG" alt="3NF Example" width="637" height="203"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Got something? Feel free to use the comment section.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>normalization</category>
      <category>database</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>SOLID - Software Design Principles</title>
      <dc:creator>Tommy</dc:creator>
      <pubDate>Tue, 13 Feb 2024 17:07:09 +0000</pubDate>
      <link>https://dev.to/tommyc/solid-software-design-principles-2ahe</link>
      <guid>https://dev.to/tommyc/solid-software-design-principles-2ahe</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;SOLID&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;is an acronym representing five design principles in software development: Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion. These principles aim to create more maintainable, scalable, and understandable software by promoting modular and flexible design.&lt;/p&gt;

&lt;p&gt;Let's understand each of them with examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Single Responsibility Principle&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;states that a class should have only one reason to change, meaning it should have only one responsibility or job. In the example below, the Calculator class is responsible for performing arithmetic operations, while the Printer class is responsible for printing results. Each class has a single responsibility, making the code easier to understand and maintain.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
}

public class Printer {
    public void printResult(int result) {
        System.out.println("Result: " + result);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Open/Closed Principle&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;states that software entities (classes, modules, functions, etc.) should be open for extension but closed for modification. In this example, the Shape class is open for extension as new shapes can be added by creating new subclasses like Circle and Rectangle, but it is closed for modification as existing classes don't need to be altered when new shapes are introduced.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public abstract class Shape {
    public abstract double area();
}

public class Circle extends Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    public double area() {
        return Math.PI * radius * radius;
    }
}

public class Rectangle extends Shape {
    private double width;
    private double height;

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    public double area() {
        return width * height;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Liskov Substitution Principle&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;states that objects of a superclass should be replaceable with objects of its subclasses without affecting the correctness of the program. In this example, both Duck and Sparrow are subclasses of Bird, and they override the fly() method according to their own behavior. When substituted for a Bird object, they retain the expected behavior without breaking the program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Bird {
    public void fly() {
        System.out.println("Flying");
    }
}

class Duck extends Bird {
    @Override
    public void fly() {
        System.out.println("Flying like a duck");
    }
}

class Sparrow extends Bird {
    @Override
    public void fly() {
        System.out.println("Flying like a sparrow");
    }
}

public class Main {
    public static void main(String[] args) {
        Bird duck = new Duck();
        Bird sparrow = new Sparrow();

        duck.fly(); // Output: Flying like a duck
        sparrow.fly(); // Output: Flying like a sparrow
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Interface Segregation Principle&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;states that clients should not be forced to depend on interfaces they do not use. In other words, it suggests that interfaces should be specific to the needs of the clients. In this example, the Printer and Scanner interfaces segregate the behaviors related to printing and scanning respectively. The AllInOnePrinter class implements both interfaces, but other classes can choose to implement only the interface(s) relevant to them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Printer {
    void print();
}

interface Scanner {
    void scan();
}

class AllInOnePrinter implements Printer, Scanner {
    public void print() {
        System.out.println("Printing...");
    }

    public void scan() {
        System.out.println("Scanning...");
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Dependency Inversion Principle&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;suggests that high-level modules should not depend on low-level modules but rather both should depend on abstractions. Additionally, it advocates for the use of interfaces or abstract classes to decouple classes and promote flexibility. In this example, the MessageSender class depends on the Message interface rather than concrete implementations like Email or SMS, allowing for easier swapping of message types without modifying the MessageSender class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Message {
    String getContent();
}

class Email implements Message {
    public String getContent() {
        return "This is an email message";
    }
}

class SMS implements Message {
    public String getContent() {
        return "This is an SMS message";
    }
}

class MessageSender {
    private Message message;

    public MessageSender(Message message) {
        this.message = message;
    }

    public void sendMessage() {
        System.out.println("Sending message: " + message.getContent());
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>solidprinciples</category>
      <category>java</category>
      <category>beginners</category>
      <category>oop</category>
    </item>
    <item>
      <title>Some handy PHP Array functions</title>
      <dc:creator>Tommy</dc:creator>
      <pubDate>Mon, 05 Feb 2024 09:18:40 +0000</pubDate>
      <link>https://dev.to/tommyc/some-handy-php-array-functions-3c1k</link>
      <guid>https://dev.to/tommyc/some-handy-php-array-functions-3c1k</guid>
      <description>&lt;h2&gt;
  
  
  Hey buddy, here are some handy-dandy PHP Array functions for you.
&lt;/h2&gt;

&lt;p&gt;Let's declare an example array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$languages = ["php", "javaScript", "html"];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sorts the array in ascending order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sort($languages);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reverse sorts the array in descending order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rsort($languages);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sorts the array by keys in ascending order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ksort($languages);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reverse sorts the array by keys in descending order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;krsort($languages);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sorts the array using a natural case-insensitive algorithm.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;natcasesort($languages);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Combines two arrays, using the first as keys and the second as values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array_combine($languages, [1, 2, 3]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Merges two or more arrays into a single array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array_merge($languages, [1, 2, 3], [4, 5, 6]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Searches for an element in an array and returns its index position.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array_search("PHP", $languages);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Checks if an element exists in an array, returning a boolean value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;in_array("php", $languages);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Checks if a key exists in an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array_key_exists(0, $languages);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adds an element to the end of an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array_push($languages, "java");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adds an element to the beginning of an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array_unshift($languages, "vue");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Removes the last element from an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array_pop($languages);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Removes the first element from an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array_shift($languages);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adds or removes elements from an array at a specified position. (see optional params in the docs below)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array_splice($languages, 0, 1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Extracts a portion of an array starting from a specified position. (see optional params in the docs below)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array_splice($languages, 0, 2, 100);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Removes duplicate values from the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array_unique($languages);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Returns all keys of the associative array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array_keys($languages);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Returns all values of the associative array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array_values($languages);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Flips string and integer values of an associative array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array_flip($languages);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Shuffles the order of the elements of the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;shuffle($languages);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Array pointer helper functions&lt;/strong&gt;&lt;br&gt;
Moves the array's internal pointer to the last element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;end($languages);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Returns the current element's value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;current($languages); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Moves the array's internal pointer to the previous element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;prev($languages);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Moves the array's internal pointer to the next element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;next($languages);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Got more? Feel free to add in the comment section below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Doc&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.php.net/manual/en/ref.array.php" rel="noopener noreferrer"&gt;https://www.php.net/manual/en/ref.array.php&lt;/a&gt;&lt;/p&gt;

</description>
      <category>php</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>array</category>
    </item>
    <item>
      <title>Some handy PHP String functions</title>
      <dc:creator>Tommy</dc:creator>
      <pubDate>Sun, 28 Jan 2024 12:14:56 +0000</pubDate>
      <link>https://dev.to/tommyc/some-handy-php-string-functions-4g6b</link>
      <guid>https://dev.to/tommyc/some-handy-php-string-functions-4g6b</guid>
      <description>&lt;h2&gt;
  
  
  Hey buddy, here are some handy-dandy PHP String functions for you.
&lt;/h2&gt;

&lt;p&gt;heredoc is a way to declare strings using a special syntax, allowing multiline strings without needing to escape quotes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo &amp;lt;&amp;lt;&amp;lt;"FOOBAR"
Hello World!
FOOBAR;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;nowdoc is kinda similar to heredoc but treats the enclosed string literally, ignoring variables and special characters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo &amp;lt;&amp;lt;&amp;lt;'EOD'
Example of string spanning multiple lines
using nowdoc syntax. Backslashes are always treated literally,
e.g. \\ and \'.
EOD;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Get the length of a string&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo strlen($str1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Convert a string to lowercase&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo strtolower($str1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Convert a string to uppercase&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo strtoupper($str1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Uppercase the first character of a string&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo ucfirst($str1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Uppercase the first character of each word in a string&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo ucwords($str1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Get a substring from a string starting at index 6&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo substr($str1, 6);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Get a substring from a string starting at index 6 with a length of 4&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo substr($str1, 6, 4);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Find the position of a substring in a string (case-sensitive)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo strpos($str1, "Hello");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Find the position of a substring in a string (case-insensitive)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo stripos($str1, "hello");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Convert an array into a string using implode&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$arr1 = [1, "two"];
print_r(implode($arr1));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Convert a string into an array using explode&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print_r(explode("@foo", $str1));
$str2 = "lol#oof#sheez";
print_r(explode("#", $str2));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace a substring in a string&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$str2 = "welcome back!"; 
echo str_replace("back", "home", $str2);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remove HTML tags from a string&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$html_text_content = "&amp;lt;p&amp;gt;lol&amp;lt;/p&amp;gt;";
echo strip_tags($html_text_content);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Escape special characters in a string&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$name = "tom's world";
echo addslashes($name);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remove slashes from a string&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$str_with_slashes = "\yes \sir";
echo stripslashes($str_with_slashes);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Trim white spaces from the beginning and end of a string&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$str_with_spaces = "    no!   ";
echo trim($str_with_spaces);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Shuffle characters in a string&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$shuffle_me = "hello there!";
echo str_shuffle($shuffle_me);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generate and display a 6-character OTP&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$otp = "1726341237472364782346253";
echo substr(str_shuffle($otp), 0, 6);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use md5 to a string (not recommended for password hashing)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$pwd = "password123";
echo md5($pwd);
echo password_hash($pwd, PASSWORD_DEFAULT);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Verify a password hash&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$hash = '$2y$10$.vGA1O9wmRjrwAVXD98HNOgsNpDczlqm3Jq7KnEd1rVAGv3Fykk1a';
if (password_verify('rasmuslerdorf', $hash)) {
    echo 'Password is valid!';
} else {
    echo 'Invalid password.';
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Encode and decode strings to/from base64&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$str3 = "test";
echo base64_encode($str3);
echo base64_decode("dGVzdA==j");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Do you have something more to add? Feel free to add them in the comment section to help other devs.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>php</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>What's @SpringBootApplication Annotation?</title>
      <dc:creator>Tommy</dc:creator>
      <pubDate>Fri, 03 Feb 2023 02:21:52 +0000</pubDate>
      <link>https://dev.to/tommyc/whats-springbootapplication-annotation-18m4</link>
      <guid>https://dev.to/tommyc/whats-springbootapplication-annotation-18m4</guid>
      <description>&lt;p&gt;In a nutshell, &lt;strong&gt;@SpringBootApplication&lt;/strong&gt; is a convenience annotation that is equivalent to declaring three separate annotations @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes. It is typically used on the main class of a Spring Boot application to enable auto-configuration, component scanning, and to define the application context.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;u&gt;&lt;strong&gt;@Configuration&lt;/strong&gt;&lt;/u&gt;: Indicates that the class is a source of bean definitions for the application context.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;u&gt;&lt;strong&gt;@EnableAutoConfiguration&lt;/strong&gt;&lt;/u&gt;: Enables Spring Boot's auto-configuration mechanism, which automatically configures the application based on the dependencies that are added in the classpath.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;u&gt;&lt;strong&gt;@ComponentScan&lt;/strong&gt;&lt;/u&gt;: Enables component scanning so that the annotated components are automatically discovered and registered as beans in the application context.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By using the @SpringBootApplication annotation, developers can write less code and make their code easier to read and understand. &lt;/p&gt;

&lt;p&gt;ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.example.myapplication;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>regex</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Spring Autowiring</title>
      <dc:creator>Tommy</dc:creator>
      <pubDate>Wed, 01 Feb 2023 02:18:35 +0000</pubDate>
      <link>https://dev.to/tommyc/spring-autowiring-54gi</link>
      <guid>https://dev.to/tommyc/spring-autowiring-54gi</guid>
      <description>&lt;p&gt;Spring &lt;u&gt;Autowiring&lt;/u&gt; is a feature of the Spring Framework that allows for the automatic injection of dependencies between beans in a Spring application. It allows developers to define the dependencies of a bean in the configuration file, rather than hard-coding them in the bean's class.&lt;/p&gt;

&lt;p&gt;Autowiring can greatly simplify the process of creating and configuring beans in a Spring application, making it easier to manage dependencies and improve code maintainability.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to autowire beans?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;XML Configuration: Autowiring can be specified in XML configuration files by setting the "autowire" attribute on the  definition to one of the autowiring modes, such as "byType" or "byName".
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;bean id="exampleBean1" class="com.example.ExampleBean1" autowire="byType"/&amp;gt;

&amp;lt;bean id="exampleBean2" class="com.example.ExampleBean2" autowire="byName"/&amp;gt;

&amp;lt;bean id="exampleBean3" class="com.example.ExampleBean3"/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Java Configuration: Autowiring can be specified in Java configuration files by using the @Autowired annotation on the setter methods or fields that should be autowired. Alternatively, you can use the @Autowired constructor to autowire by constructor. In the example below, we annotate the fields.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Component
public class ExampleConfig {

  @Autowired
  private ExampleBean1 exampleBean1;

  // You can use @Qualifier annotation along with @Autowired to autowire beans by name.
  @Autowired
  @Qualifier("exampleBean2")
  private ExampleBean2 exampleBean2;

...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The default autowiring mode in Java configuration is "byType", while in XML configuration, it is set to "no". &lt;/p&gt;

&lt;h2&gt;
  
  
  Autowiring Modes:
&lt;/h2&gt;

&lt;p&gt;Autowiring modes are different ways in which the Spring framework can wire beans together automatically.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;u&gt;autodetect&lt;/u&gt;: When autowiring is set to "autodetect", Spring framework will first look for a constructor to resolve the bean dependencies, and if a constructor is not found, it will fall back to using the "byType" mode.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;u&gt;no&lt;/u&gt;: When autowiring is set to "no", it means that the beans are not wired together automatically and the developer must manually specify the dependencies between the beans in the configuration. This mode provides the greatest degree of control over how beans are wired together, but it also requires the most effort on the part of the developer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;u&gt;byName&lt;/u&gt;: This mode looks for a bean with the same name as the property that is being set. For example, if a class has a property called "processor", the Spring container will look for a bean named "processor" and wire it to the property.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;u&gt;byType&lt;/u&gt;: This mode looks for a bean of the same type as the property that is being set. For example, if a class has a property of type "Processor", the Spring container will look for a bean of type "Processor" and wire it to the property.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;u&gt;constructor&lt;/u&gt;: Autowiring can also be done on constructors, in which case the Spring container will look for beans of the same type as the constructor arguments and wire them to the constructor.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>discuss</category>
      <category>algorithms</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Spring Bean</title>
      <dc:creator>Tommy</dc:creator>
      <pubDate>Tue, 31 Jan 2023 21:20:35 +0000</pubDate>
      <link>https://dev.to/tommyc/spring-bean-3j8f</link>
      <guid>https://dev.to/tommyc/spring-bean-3j8f</guid>
      <description>&lt;h2&gt;
  
  
  Spring Bean
&lt;/h2&gt;

&lt;p&gt;A Spring Bean is an object managed by the Spring framework. It is created by the Spring IoC container and is managed throughout the lifecycle of an application.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Create a Spring Bean?
&lt;/h2&gt;

&lt;p&gt;A Spring Bean can be defined using XML configuration and annotations. In the examples below, we will use annotations since these have become a very common and modern way of defining a bean in Spring framework. Annotation-based configuration provides a more concise and easy-to-read way of defining beans, compared to traditional XML-based configuration.&lt;/p&gt;

&lt;p&gt;The most common annotations used for defining a bean are @Component, @Service, @Repository, @Controller and &lt;a class="mentioned-user" href="https://dev.to/bean"&gt;@bean&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;@Component&lt;/strong&gt; is a general-purpose, class-level stereotype annotation that indicates that a class is a component in the Spring application. The @Component annotation can be used to mark any class as a candidate for auto-detection as a Spring Bean.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Component
public class Employee {
   // ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;@Controller&lt;/strong&gt; is a specialization of @Component that is used to indicate that a class is a controller component. The @Controller annotation is used to mark a class that provides HTTP request handling services.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Controller
public class EmployeeController {
   // ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;@Service&lt;/strong&gt; is a specialization of @Component that is used to indicate that a class is a service component. The @Service annotation is used to mark a class that provides business logic services.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Service
public class EmployeeService {
   // ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;@Repository&lt;/strong&gt; is a specialization of @Component that is used to indicate that a class is a repository component. The @Repository annotation is used to mark a class that provides data access services.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Repository
public interface EmployeeRepository extends CrudRepository&amp;lt;T, ID&amp;gt;{
   // ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a class="mentioned-user" href="https://dev.to/bean"&gt;@bean&lt;/a&gt;&lt;/strong&gt; annotation is used to define a bean in Spring framework, specifically in a Java configuration class. This a method-level annotation that is used in conjunction with the &lt;strong&gt;@Configuration&lt;/strong&gt; annotation to define beans and manage their lifecycle within the Spring IoC container. Notice that the body of the method contains the logic responsible for creating the instance.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Configuration
public class AppConfig {

   @Bean
   public EmployeeService employeeService() {
      return new EmployeeService();
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Spring Bean Scope
&lt;/h2&gt;

&lt;p&gt;Spring Beans have several different scopes that define the lifecycle and scope of the bean within the Spring IoC container. The most common scopes are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Singleton&lt;/strong&gt;: This is the default scope for a Spring Bean, and it means that there is only one instance of the bean in the IoC container. This instance is shared by all objects that request it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prototype&lt;/strong&gt;: A prototype scope means that a new instance of the bean is created each time it is requested. This is useful for beans that are stateful or have a large amount of data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Request&lt;/strong&gt;: This scope is used for beans that are associated with an HTTP request. A new instance of the bean is created for each request, and the bean is only accessible within the scope of that request.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Session&lt;/strong&gt;: This scope is used for beans that are associated with an HTTP session. A new instance of the bean is created for each session, and the bean is only accessible within the scope of that session.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Global Session&lt;/strong&gt;: This scope is used for beans that are associated with a global HTTP session, typically in a Portlet context.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Spring Bean vs Java Bean
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Java Bean&lt;/strong&gt; is a simple Java class that follows certain conventions, such as having a no-arg constructor, private fields, and public getter and setter methods for accessing those fields. Java Beans are often used to store data and transfer data between different parts of an application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// example of a Java bean

public class Employee {
   private String name;
   private int age;

   // No-arg constructor
   public Employee() { }

   // Getters and Setters
   public String getName() { return name; }
   public void setName(String name) { this.name = name; }

   public int getAge() { return age; }
   public void setAge(int age) { this.age = age; }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the Employee class is a simple Java Bean that follows the conventions of having a no-arg constructor, private fields, and public getter and setter methods.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;Spring Bean&lt;/strong&gt;, on the other hand, is an object managed by the Spring framework, specifically by the Spring IoC (Inversion of Control) container. A Spring Bean is created by the IoC container and is managed throughout the lifecycle of an application. It can be a simple Java Bean, but it is also commonly used to represent services, repositories, controllers, and other types of components within a Spring-based application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// example of a Spring Bean

@Service
public class EmployeeService {
   private EmployeeDao employeeDao;

   @Autowired
   public EmployeeService(EmployeeDao employeeDao) {
      this.employeeDao = employeeDao;
   }

   public List&amp;lt;Employee&amp;gt; getAllEmployees() {
      return employeeDao.findAll();
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the EmployeeService class is a Spring Bean that is annotated with @Service. This annotation marks the class as a service component, and it will be automatically discovered and managed by the Spring IoC container. The EmployeeService class also has a constructor that is annotated with @Autowired, which will allow Spring to inject an instance of EmployeeDao into the bean when it is created.&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
    <item>
      <title>Spring Dependency Injection, IoC and Autowiring</title>
      <dc:creator>Tommy</dc:creator>
      <pubDate>Tue, 31 Jan 2023 19:43:03 +0000</pubDate>
      <link>https://dev.to/tommyc/spring-dependency-injection-2752</link>
      <guid>https://dev.to/tommyc/spring-dependency-injection-2752</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;What's Dependency Injection (DI)?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;u&gt;Dependency Injection (DI) is a design pattern&lt;/u&gt; that implements the principles of Inversion of Control (IoC) by providing an object with its required dependencies from an external source instead of having the object create or locate them on its own. This not only improves code maintainability, flexibility, and testability, but also follows the IoC concept of shifting control from the object to the external dependencies, thereby decoupling the object from its dependencies and promoting modularity&lt;/p&gt;

&lt;h2&gt;
  
  
  What is IoC?
&lt;/h2&gt;

&lt;p&gt;&lt;u&gt;Inversion of control (IoC) is a general concept&lt;/u&gt; that refers to a program or system where the control flow of a program is determined by a framework or container, rather than by the program itself. In Spring, IoC is achieved through dependency injection.&lt;/p&gt;

&lt;p&gt;In Spring, there is an implementation known as the &lt;b&gt;IoC container&lt;/b&gt;, which connects the dependencies, locates, and manages the life cycle of beans. The Spring IoC container is represented by &lt;b&gt;ApplicationContext&lt;/b&gt;, which is responsible for creating, setting up, and putting together the beans mentioned. The container follows the instructions for which objects to create, configure, and assemble from the configuration metadata it reads.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Autowiring?
&lt;/h2&gt;

&lt;p&gt;In the context of Spring, &lt;u&gt;Autowiring can be used to implement Dependency Injection&lt;/u&gt;. When you enable Autowiring for a bean, the Spring framework will automatically resolve and wire the dependencies for that bean, using one of the DI techniques, such as Constructor-based, Setter-based, or Field-based Dependency Injection.&lt;/p&gt;

&lt;p&gt;In other words, Autowiring is a way to simplify the process of implementing Dependency Injection in Spring. By using Autowiring, you can eliminate the need to explicitly specify the relationships between beans in the configuration file, allowing you to make changes to your code more easily.&lt;/p&gt;

&lt;p&gt;In this example, we will use the &lt;strong&gt;@Autowired&lt;/strong&gt; annotation to enable Autowiring in Spring.&lt;/p&gt;

&lt;h2&gt;
  
  
  The 3 types of Dependency Injection in Spring are:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Constructor-based&lt;/strong&gt; Dependency Injection: The dependencies are provided to an object via constructors.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class MyService {
   private MyDependency myDependency;

   // constructor injection
   @Autowired
   public MyService(MyDependency myDependency) {
      this.myDependency = myDependency;
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Setter-based&lt;/strong&gt; Dependency Injection: The dependencies are provided to an object via setter methods.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class MyService {
   private MyDependency myDependency;

   @Autowired
   public void setMyDependency(MyDependency myDependency) {
      this.myDependency = myDependency;
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Field-based&lt;/strong&gt; Dependency Injection: The dependencies are provided to an object via fields.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class MyService {
   @Autowired
   private MyDependency myDependency;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>spring</category>
      <category>java</category>
      <category>dependencyinjection</category>
      <category>autowiring</category>
    </item>
    <item>
      <title>Recursive Algorithm</title>
      <dc:creator>Tommy</dc:creator>
      <pubDate>Sun, 05 Jun 2022 14:49:48 +0000</pubDate>
      <link>https://dev.to/tommyc/recursive-algorithm-28pe</link>
      <guid>https://dev.to/tommyc/recursive-algorithm-28pe</guid>
      <description>&lt;p&gt;&lt;strong&gt;Recursion&lt;/strong&gt; is where a function invokes itself. Various algorithms utilize recursion, hence it's important to know this concept.&lt;/p&gt;

&lt;p&gt;Always remember when creating a recursive function that you need 2 parts: the &lt;strong&gt;base case&lt;/strong&gt;, and the &lt;strong&gt;recursive case&lt;/strong&gt;. The recursive case is when the function calls itself. The base case is when the function doesn’t call itself again (this prevents the function go into an infinite loop)&lt;/p&gt;

&lt;h2&gt;
  
  
  Sample Code
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;In this example, we try to find the factorial of 5&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Main {
    public static void main(String[] args) {
        // test the recursion method
        Factorial f = new Factorial();
        System.out.println(f.recursion(5));
    }
}

class Factorial {
    public long recursion(long n) {
        // this is our base case
        // always have a base case or else your recursion will never end
        if (n &amp;lt;= 1) {
            // here we are returning a value of 1
            // in programming, this means the output is met or valid
            return 1;
        } else {
            //recrusive case or also known as the method calling itself
            return n * recursion(n - 1);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Note:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Be careful with recursion as it can consume a lot of memory, and can create a program that never terminates. However, when used correctly recursion can be a very efficient and mathematically-elegant approach to programming.&lt;/li&gt;
&lt;li&gt;There’s no performance benefit to using recursion; in fact, loops are sometimes better for performance. Simply choose which is more important in your situation!&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Hibernate - @Formula</title>
      <dc:creator>Tommy</dc:creator>
      <pubDate>Mon, 30 May 2022 02:15:04 +0000</pubDate>
      <link>https://dev.to/tommyc/hibernate-formula-2mkb</link>
      <guid>https://dev.to/tommyc/hibernate-formula-2mkb</guid>
      <description>&lt;h2&gt;
  
  
  Formula Annotation
&lt;/h2&gt;

&lt;p&gt;The @Formula annotation is used to calculate the dynamic value of a property. @Formula takes an expression (this could be simple expression or a complex query) as a parameter. During fetch time, it evaluates the expression and assigns the evaluated value to the property.&lt;/p&gt;

&lt;h2&gt;
  
  
  Examples
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ex 1
@Formula("lower(datediff(curdate(), birth_date / 365)")
private int age;

// ex 2
@Formula("(select min(s.survey) from statistics s) ")
private float total;

// ex 3
private int num1;
private int num2;
private int num3;

@Formula(" num1 + num2 + num3")
private float totalSum;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;br&gt;
-This is only inserted in a SELECT clause!&lt;br&gt;
-Be careful when using this because the SQL command you may be using may be vendor specific. In other words, this will create coupling with a particular SQL vendor.&lt;/p&gt;

</description>
      <category>hibernate</category>
      <category>java</category>
      <category>database</category>
    </item>
    <item>
      <title>Spring AOP - Reusable Pointcut Expressions</title>
      <dc:creator>Tommy</dc:creator>
      <pubDate>Mon, 30 May 2022 01:16:03 +0000</pubDate>
      <link>https://dev.to/tommyc/spring-aop-reusable-pointcut-expressions-26d5</link>
      <guid>https://dev.to/tommyc/spring-aop-reusable-pointcut-expressions-26d5</guid>
      <description>&lt;h2&gt;
  
  
  Pointcut
&lt;/h2&gt;

&lt;p&gt;A pointcut is a collection of one or more joinpoints where an advice should be invoked. You can declare pointcuts utilizing expressions or patterns like you'll see in the examples below.&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;@Pointcut(" execution(modifiers? return-type declaring-type? method-name(param) throws?) ")&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the pattern is optional if it has a &lt;strong&gt;?&lt;/strong&gt; symbol&lt;/li&gt;
&lt;li&gt;patterns can use wildcards * (matches everything)&lt;/li&gt;
&lt;li&gt;the &lt;strong&gt;@Pointcut&lt;/strong&gt; annotation contains a set of one or more JoinPoints where an advice should be executed&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;modifiers?&lt;/strong&gt; (optional) is the method's access modifier like public, protected, private&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;return-type&lt;/strong&gt; is the method's return type&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;declaring-type?&lt;/strong&gt; (optional) is the package or class name&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;method-name(param)&lt;/strong&gt; is the method's name with its parameters&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;param&lt;/strong&gt; could take zero or more arguments. &lt;em&gt;() matches a method that takes no parameters, whereas (..) matches any number of parameters (zero or more). The pattern (*) matches a method taking one parameter of any type&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;throws?&lt;/strong&gt; (optional) is the exception type&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Combining Pointcut Expressions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;You can combine pointcut declarations using:&lt;/strong&gt;&lt;br&gt;
&amp;amp;&amp;amp;, ||, ! operators&lt;/p&gt;
&lt;h2&gt;
  
  
  Examples
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Declare pointcut methods (must not have a body)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Pointcut("execution(public * dev.company.*.service.*.get*(..))")
public void pointCutTemplateServiceForGet() {}

@Pointcut("execution(public * dev.company.*.service.*.post*(..))")
public void pointCutTemplateServiceForPost() {}

@Pointcut("execution(public * dev.company.*.service.*.delete*(..))")
public void pointCutTemplateServiceForDelete() {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Reuse a pointcut&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;In this example, we use the pointcut template pointCutTemplateServiceForGet() twice for two different advice&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Before("pointCutTemplateServiceForGet()")
// implementation goes here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@After("pointCutTemplateServiceForGet()")
// implementation goes here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Combine multiple pointcuts using &amp;amp;&amp;amp;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@After(pointCutTemplateServiceForGet() &amp;amp;&amp;amp; pointCutTemplateServiceForPost())
// implementation goes here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Exclude a pointCut using !&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Before(pointCutTemplateServiceForGet() &amp;amp;&amp;amp; !(pointCutTemplateServiceForDelete()))
// implementation goes here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Exclude multiple pointcuts&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Before(pointCutTemplateServiceForDelete() &amp;amp;&amp;amp; !(pointCutTemplateServiceForGet() || pointCutTemplateServiceForPost()))
// implementation goes here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are many more possibilities when mixing and matching pointcut expressions, but I'll leave it up to your imagination.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;See &lt;a href="https://dev.to/tommyc/spring-aop-quick-start-3o39"&gt;this article&lt;/a&gt; to learn more about Spring AOP.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>spring</category>
      <category>aop</category>
      <category>java</category>
    </item>
  </channel>
</rss>
