Spring @Scope annotation with Singleton Scope Example

In this article, we will discuss how to create a bean, scoped as a singleton, using the @Scope annotation.
We use @Scope to define the scope of a @Component class or a @Bean definition. It can be either singleton, prototype, request, session, globalSession or some custom scope. In this article, we will discuss a singleton scope with an example.
When a Spring bean is scoped as a singleton, the Spring IoC container creates exactly one instance of the object defined by that bean definition.
By default, the Spring IoC container creates and initializes all beans as a singleton. But we can define the scope of a bean as a singleton using the scope="singleton" attribute of the element or using the @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON) annotation.
We will demonstrate this example using Annotation based (@Component) as well as Java-based configuration(@Bean).

Spring @Scope Annotation + Singleton Scope + @Component Example

Let's create an example to demonstrates usage of use @Scope annotation with a singleton scope in a spring application.

Tools and technologies used

  • Spring Framework - 5.1.0.RELEASE
  • JDK - 8 or later
  • Maven - 3.2+
  • IDE - Eclipse Mars/STS

Create a Simple Maven Project

Create a simple maven project using your favorite IDE and refer below section for packaging structure. If you are new to maven then read this article How to Create a Simple Maven Project.

Project Structure

Below diagram shows a project structure for your reference - 

The pom.xml File

<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.javaguides.spring</groupId>
    <artifactId>spring-dependson-annotation</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>spring-scope-example</name>
    <url>http://maven.apache.org</url>

    <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                 <version>3.5.1</version>
                 <configuration>
                     <source>1.8</source>
                     <target>1.8</target>
                 </configuration>
            </plugin>
        </plugins>
    </build>
</project>
Create MessageService interface as follows.

MessageService.java

package net.javaguides.spring.scope;

public interface MessageService {

    String getMessage();

    void setMessage(String message);
}
Let's create TwitterMessageService class which implements MessageService interface.

TwitterMessageService.java

package net.javaguides.spring.scope;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
public class TwitterMessageService implements MessageService {

    private String message;

    @Override
    public String getMessage() {
        return message;
    }

    @Override
    public void setMessage(String message) {
        this.message = message;
    }
}

Annotation Based Configuration - AppConfig.java

Declare the above beans in java based configuration class.
package net.javaguides.spring.scope;

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

@Configuration
@ComponentScan(basePackages = "net.javaguides.spring")
public class AppConfig {
 
}
@ComponentScan annotation scans all beans whose class is annotated by the @Component annotation in a package specified by basePackages attribute.

Running Spring Application - Application.java

Let's create a main class and run an application.
package net.javaguides.spring.scope;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Application {
 public static void main(String[] args) {
  AnnotationConfigApplicationContext  context = new AnnotationConfigApplicationContext(AppConfig.class);
  MessageService messageService = context.getBean(MessageService.class);
  messageService.setMessage("TwitterMessageService Implementation");
  System.out.println(messageService.getMessage());
  
  MessageService messageService1 = context.getBean(MessageService.class);
  System.out.println(messageService1.getMessage());
  context.close();
 }
}

Output

TwitterMessageService Implementation
TwitterMessageService Implementation
Let's develop the same example using Java-based configuration with @Bean annotation.

Spring @Scope Annotation + Singleton Scope + @Bean Example

Create MessageService interface as follows.

MessageService.java

package net.javaguides.spring.scope;

public interface MessageService {

    String getMessage();

    void setMessage(String message);
}
Let's create TwitterMessageService class which implements MessageService interface.

TwitterMessageService.java

package net.javaguides.spring.scope;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

public class TwitterMessageService implements MessageService {

    private String message;

    @Override
    public String getMessage() {
        return message;
    }

    @Override
    public void setMessage(String message) {
        this.message = message;
    }
}

Annotation Based Configuration - AppConfig.java

Declare the above beans in java based configuration class.
package net.javaguides.spring.scope;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class AppConfig {

    @Bean
    @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
    public MessageService messageService() {
        return new TwitterMessageService();
    }
}
@ComponentScan annotation scans all beans whose class is annotated by the @Component annotation in a package specified by basePackages attribute.

Running Spring Application - Application.java

Let's create a main class and run an application.
package net.javaguides.spring.scope;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Application {
 public static void main(String[] args) {
  AnnotationConfigApplicationContext  context = new AnnotationConfigApplicationContext(AppConfig.class);
  MessageService messageService = context.getBean(MessageService.class);
  messageService.setMessage("TwitterMessageService Implementation");
  System.out.println(messageService.getMessage());
  
  MessageService messageService1 = context.getBean(MessageService.class);
  System.out.println(messageService1.getMessage());
  context.close();
 }
}

Output

TwitterMessageService Implementation
TwitterMessageService Implementation
The source code of this article is available on my GitHub repository https://github.com/RameshMF/spring-core-tutorial


Comments