Terminologies of Spring Framework
Spring Framework

Terminologies of Spring Framework

I have been learning Spring framework and I have come across of a lot of new terminologies related to Spring framework. This article is the explanation of the terms.


1. What is Spring Container / Spring Context / IOC (Inversion of Control) Container?

In simple terms, it basically manages Spring beans and their lifecycle. You can think it as a container that takes your Java classes as input and creates a running system. Think it like a manager who takes care of creating and managing objects (called beans) in a Spring application. It basically gathers everything in your coding environment, put them in the right places and make a ready system to do its work.

There are two popular types of Spring Containers:

  • Bean Factory: Basic Spring container. (Not usually used that much)
  • Application Context: Advanced Spring container that is easy to use in web application. It is recommended for web applications, REST APIs and microservices.


Spring Container

From the figure, we can see how a Spring Container works. Once you create the Java classes (POJO, we will learn about it after a while) and configurations (setup that defines how the Spring Container should manage the application's objects), the IOC container creates the runtime system.


2. What is a POJO (Plain Old Java Object)?

Look at the following code:

public class Pojo {
    private String text;
    private int number;

    public String to_string(){
        return text + ":" + number;
    }
}

public class Main {
    public static void main(String[] args) {
        Pojo pojo = new Pojo();
        pojo.text = "Hello";
        pojo.number = 100;
        System.out.println(pojo);
    }
}        

In a single line, any Java object is a POJO. This is as simple as that. This is just a regular Java object that doesn’t follow any specific rules or implement any special interfaces from a framework or library.


3. What is a Java Bean?

On the other hand, discussing about Java Beans, it is not as rule-less like POJO. It has some restrictions. Actually, Java Bean has three rules. Firstly, there must be a public non-argument constructor. Secondly, getter() and setter() methods implementation is must. Thirdly, Java Beans must implement the 'Serializable' interface. (The serializable interface in Java is used to enable an object to be converted into a sequence of bytes, which can then be saved to a file, sent over a network, or stored in a database.). Look at the following code for better clarity:

public class JavaBean implements Serializable {

    //1. Must have a public no-arg constructor
public JavaBean() {}

    private String text;
    private int number;


    //2. Must have getter and setter methods
    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String to_string(){
        return text + ":" + number;
    }
}        

4. What is a Spring Bean?

Now, let's discuss Spring Beans. In simpler terms, anything that is managed by Spring framework is called Spring Beans. Spring uses IOC Container (Bean Factory or Application Context, as discussed earlier) to manage these objects.

Look at the following code:

record Person(String name, int age) { }

@Configuration
public class SpringConfiguration {
    @Bean 
    public String name(){
        return "Abir";
    }

    @Bean 
    public int age(){
        return 15;
    }

    @Bean
    public Person person(){
        return new Person("Abir", 15);
    }
}        

Here, the 'SpringConfiguration' class is a configuration class. The annotation '@Configuration' marks the class as a configuration class, telling Spring to look here for bean definitions. Each method of the class (name(), age(), person()) is a Spring Bean and is annotated by '@Bean'. Look at the following picture for better clarity.

Spring Bean

In the Java Virtual Machine, Spring manages the Spring Beans 'name', 'age' and 'person' as shown in the code above.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了