Sobes.tech
Middle+
279

Est-il possible de créer deux Singleton dans Spring?

Réponse de l'IA

sobes.tech IA

Oui, c'est possible :

  1. Différents contextes : Chaque contexte Spring (par exemple, deux AnnotationConfigApplicationContext) gérera son propre ensemble de beans, y compris les singletons. Dans chaque contexte, il y aura sa propre instance du singleton.

    // Bean Singleton
    public class MySingleton {
        private static int counter = 0;
        private int instanceId;
    
        public MySingleton() {
            instanceId = ++counter;
        }
    
        public int getInstanceId() {
            return instanceId;
        }
    }
    
    // Configuration du bean
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class AppConfig {
        @Bean
        public MySingleton mySingleton() {
            return new MySingleton();
        }
    }
    
    // Utilisation de différents contextes
    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()); // Affiche : 1
    
            ApplicationContext context2 = new AnnotationConfigApplicationContext(AppConfig.class);
            MySingleton singleton2 = context2.getBean(MySingleton.class);
            System.out.println("Singleton 2 from context 2, ID : " + singleton2.getInstanceId()); // Affiche : 2
        }
    }
    
  2. Différents IDs de beans (si le bean est défini plusieurs fois) : Si la même classe est enregistrée dans un contexte avec différents IDs de bean, Spring créera par défaut un singleton séparé pour chaque ID. Ce n'est pas exactement "deux Singletons de cette classe", mais plutôt deux beans différents, chacun avec une portée Singleton et utilisant la même classe d'implémentation.

    // Configuration avec différents IDs pour une classe
    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();
        }
    }
    
    // Utilisation de beans avec différents 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("ID du Singleton A : " + singletonA.getInstanceId()); // Affiche : 1
            System.out.println("ID du Singleton B : " + singletonB.getInstanceId()); // Affiche : 2
        }
    }
    

Malgré ces possibilités, l'utilisation standard et la plus courante du Singleton dans Spring implique un seul exemplaire d'un bean d'une classe donnée dans un ApplicationContext.