Spring Core Essentials: What You Need To Know

Spring Core Essentials: What You Need To Know

Introduction

In this article we'll cover all the Spring Core concepts covered in pervious articles (in brief) along with a few more annotations you need to know before getting started with Spring Boot.

FOR NEW AUDIENCE: Just a quick side note, if you're new to this series no worries this is just the 7th article in Spring Boot series. If you'd like to, you may start reading from PART 1 or to jump straight into Spring Boot, you can start off right from this article which covers Spring Core in brief, all you need to know to get started with Spring Boot!

Recommendation: Do read previous articles for better understanding :)

Bean Scopes

The scope of a Spring Bean is used to define lifecycle and visibility of Bean in the contexts in which it is used.

Singleton

This is the default scope, only one instance of Bean is created per Spring IoC container.

Prototype

A new instance of Bean is created every time it is requested. To use prototype scope:

@Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Component
public class MyBean {
...
}

There are several other types of scopes such as Application Scope and Session scope (a new instance is created per HTTP session).

Bean Lifecycle Management

To indicate the methods that should be executed after bean's initialization and before bean's destruction, "@PostConstruct" and "@PreDestroy" annotations are used respectively.

import javax.annotation.PostConstruct;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

    @PostConstruct
    public void init() {
        // Initialization code here
        System.out.println("Bean is going through init.");
    }

    public void performTask() {
        // Business logic here
        System.out.println("Performing task.");
    }

    @PreDestroy
    public void destroy() {
        // Cleanup code here
        System.out.println("Bean is going through destroy.");
    }
}

Jakarta CDI vs Spring Framework

Jakarta Context and Dependency Injection (CDI) is a specification and Spring Framework implements the specification along with many other features. For example:

Use caseJakarta CDI AnnotationsSpring Framework Annotations
For dependency injection@Inject @Qualifier @Produces @Named@Autowired @Qualifier @Component @bean
Scope@RequestScoped @SessionScoped @ApplicationScoped@Scope( ... ) : Singleton Prototype Request Session Application

Important Annotations in Spring

AnnotationDescription
@ConfigurationTo indicate that a class declares one/more @Bean methods for Spring to generate Bean definitions.
@ComponentScanDefine specific packages to scan for components managed by Spring. Default is current package.
@BeanTo indicate a method produces a bean managed by Spring IoC.
@ComponentTo indicate an annotated class is a component.
@ServiceTo indicate that the annotated class has business logic.
@ControllerTo indicate that the annotated class is a controller (example : web controller)
@RepositoryTo indicate that the annotated class used to interact with database.
@PrimaryTo indicate that a bean should be given preference when multiple beans are qualified to autowire a single valued dependency.
@QualifierTo use on a field or a class for candidate beans when autowiring.
@LazyTo indicate that a bean has to be initialized upon its request for creation.

Summary

In this article, we provide a brief overview of Spring Core concepts and important annotations that are essential for getting started with Spring Boot. We'll cover bean scopes, lifecycle management, and a comparison between Jakarta CDI and Spring Framework annotations. Key annotations such as @Configuration, @ComponentScan, @Bean, @Component, @Service, @Controller, and others are discussed to help you understand their roles within the Spring ecosystem. Whether you're continuing from previous parts of this series or jumping in right here, this guide will equip you with the foundational knowledge needed for Spring Boot.