Spring - Singleton scope example using @Scope annotation

Posted on January 16, 2017


Technologies used:   JDK 1.8.0_71 | Spring 4.3.5.RELEASE | Maven 3.9.9 | Eclipse Mars.2 (4.5.2)

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 singleton using the scope="singleton" attribute of the <bean/> element or using the @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON) annotation.

In this post we will learn how to create a bean, scoped as singleton, using the @Scope annotation.

Singleton scope + @Scope + @Bean example

Create a AccountService bean as follows.

AccountService.java

package com.boraji.tutorial.spring;

public class AccountService {

   private String accountHolder;

   public String getAccountHolder() {
      return accountHolder;
   }

   public void setAccountHolder(String accountHolder) {
      this.accountHolder = accountHolder;
   }

}

Declare the AccountService bean in java based configuration class.

AppConfig.java

package com.boraji.tutorial.spring;

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;

/**
 * @author imssbora
 */
@Configuration
public class AppConfig {
   @Bean("accountService")
   @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
   public AccountService getAccountService() {
      return new AccountService();
   }
}

Create main class and run application.

MainApp.java

package com.boraji.tutorial.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author imssbora
 */
public class MainApp {
   public static void main(String[] args) {
      AnnotationConfigApplicationContext context = 
            new AnnotationConfigApplicationContext(AppConfig.class);

      AccountService service1 = context.getBean("accountService", AccountService.class);
      service1.setAccountHolder("BORAJI");
      System.out.println(service1.getAccountHolder());

      AccountService service2 = context.getBean("accountService", AccountService.class);
      System.out.println(service2.getAccountHolder());
      context.close();
   }
}

Output

BORAJI
BORAJI

 

Singleton scope + @Scope + @Component example

Create a UserService bean annotated by a type-level annotation @Component as follows.

UserService.java

package com.boraji.tutorial.spring;

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 UserService {

   private String userName;

   public String getUserName() {
      return userName;
   }

   public void setUserName(String userName) {
      this.userName = userName;
   }
}

Create a java based configuration class as follows.

package com.boraji.tutorial.spring;

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

/**
 * @author imssbora
 */
@Configuration
@ComponentScan(basePackages = "com.boraji.tutorial.spring")
public class AppConfig {

}

@ComponentScan annotation scans all beans whose class is annotated by the @Component annotation in a package specified by basePackages attribute.

Create main class and run application.

MainApp.java

package com.boraji.tutorial.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author imssbora
 */
public class MainApp {
   public static void main(String[] args) {
      AnnotationConfigApplicationContext context = 
            new AnnotationConfigApplicationContext(AppConfig.class);


      UserService userService1 = context.getBean(UserService.class);
      userService1.setUserName("BORAJI.COM");
      System.out.println(userService1.getUserName());

      UserService userService2 = context.getBean(UserService.class);
      System.out.println(userService2.getUserName());
      context.close();
   }
}

Output

BORAJI.COM
BORAJI.COM