Sobes.tech
Middle+

Is it possible to create two Singletons in Spring?

sobes.tech AI

Answer from AI

Yes, it is possible:

  1. Different contexts: Each Spring context (for example, two AnnotationConfigApplicationContext) will manage its own set of beans, including singletons. Each context will have its own singleton instance.

    // Singleton bean
    public class MySingleton {
        private static int counter = 0;
        private int instanceId;
    
        public MySingleton() {
            instanceId = ++counter;
        }
    
        public int getInstanceId() {
            return instanceId;
        }
    }
    
    // Bean configuration
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class AppConfig {
        @Bean
        public MySingleton mySingleton() {
            return new MySingleton();
        }
    }
    
    // Using different contexts
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    public class Main {
        public static void main(String[] args) {
            ApplicationContext context1 = new AnnotationConfigApplicationContext(AppConfig.class);
            MySingleton singleton1 = context1.getBean(MySingleton.class);
            System.out.println("Singleton 1 from context 1, ID: " + singleton1.getInstanceId()); // Output: 1
    
            ApplicationContext context2 = new AnnotationConfigApplicationContext(AppConfig.class);
            MySingleton singleton2 = context2.getBean(MySingleton.class);
            System.out.println("Singleton 2 from context 2, ID: " + singleton2.getInstanceId()); // Output: 2
        }
    }
    
  2. Different bean IDs (if the bean is defined multiple times): If the same class is registered in one context under different bean IDs, Spring by default will create a separate singleton for each ID. This is not exactly "two singletons of this class," but rather two different beans, each with singleton scope and using the same implementation class.

    // Configuration with different IDs for the same class
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class DifferentIdConfig {
        @Bean(name = "singletonA")
        public MySingleton mySingletonA() {
            return new MySingleton();
        }
    
        @Bean(name = "singletonB")
        public MySingleton mySingletonB() {
            return new MySingleton();
        }
    }
    
    // Using beans with different IDs
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    public class MainDifferentId {
        public static void main(String[] args) {
            ApplicationContext context = new AnnotationConfigApplicationContext(DifferentIdConfig.class);
            MySingleton singletonA = context.getBean("singletonA", MySingleton.class);
            MySingleton singletonB = context.getBean("singletonB", MySingleton.class);
    
            System.out.println("Singleton A ID: " + singletonA.getInstanceId()); // Output: 1
            System.out.println("Singleton B ID: " + singletonB.getInstanceId()); // Output: 2
        }
    }
    

Despite the possibilities, the standard and most common use of Singleton in Spring implies a single instance of a bean of a specific class within one ApplicationContext.