DEV Community

Cover image for Dive in Spring framework - Beginner Guide
NRJ
NRJ

Posted on • Updated on

Dive in Spring framework - Beginner Guide

Many fresher want to get SDE role in big banks like Deutsche Bank, UBS, Barclays, HSBC, etc.. But they don't know is that in every bank main tech stack is Spring.

So now let's shallow dive in Spring and Spring Boot.

Pre-requisite - Java basics with OOP's concepts.

So lets understand using basic example

Creation of object as a normal Java with new keyword

public class MyClass {
    private String message;

    public MyClass() {
        this.message = "Hello, I'm created using the new keyword!";
    }

    public String getMessage() {
        return message;
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating an instance of MyClass using the 'new' keyword
        MyClass obj = new MyClass();

        // Accessing the message
        System.out.println(obj.getMessage());
    }
}
Enter fullscreen mode Exit fullscreen mode

Object Creation using Spring:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
    @Bean
    public MyClass myClass() {
        return new MyClass();
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating Spring application context
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

        // Retrieving bean from Spring context
        MyClass obj = context.getBean(MyClass.class);

        // Accessing the message
        System.out.println(obj.getMessage());

        // Closing Spring context
        context.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

Let's compare the two approaches:

Using the new Keyword:

In the first code snippet:

MyClass obj = new MyClass();
Enter fullscreen mode Exit fullscreen mode

Here, an instance of MyClass is created directly within the main method using the new keyword. The MyClass constructor is invoked, setting the message field to a specific value. This approach is straightforward and doesn't involve any external dependencies or frameworks.

Using Spring Dependency Injection:

In the second code snippet:

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MyClass obj = context.getBean(MyClass.class);
Enter fullscreen mode Exit fullscreen mode

Here, the Spring Framework is used to manage the creation and initialization of the MyClassinstance. In the AppConfigclass, a method annotated with @Bean is defined to indicate that a bean of type MyClass should be created and managed by the Spring container. In the main method, an application context is created based on the configuration defined in AppConfig, and then the MyClassbean is retrieved from the context using context.getBean(MyClass.class). This approach allows for loose coupling, easier unit testing, and better management of dependencies, especially in large-scale applications.

Top comments (0)