Sobes.tech
Middle+

Υπάρχει δυνατότητα δημιουργίας δύο Singleton στο Spring;

sobes.tech AI

Απάντηση από AI

Ναι, είναι δυνατό:

  1. Διάφορα contexts: Κάθε Spring context (π.χ., δύο AnnotationConfigApplicationContext) θα διαχειρίζεται το δικό του σύνολο beans, συμπεριλαμβανομένων των singleton. Κάθε context θα έχει το δικό του instance singleton.

    // Singleton bean
    public class MySingleton {
        private static int counter = 0;
        private int instanceId;
    
        public MySingleton() {
            instanceId = ++counter;
        }
    
        public int getInstanceId() {
            return instanceId;
        }
    }
    
    // Διαμόρφωση bean
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class AppConfig {
        @Bean
        public MySingleton mySingleton() {
            return new MySingleton();
        }
    }
    
    // Χρήση διαφορετικών 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()); // Έξοδος: 1
    
            ApplicationContext context2 = new AnnotationConfigApplicationContext(AppConfig.class);
            MySingleton singleton2 = context2.getBean(MySingleton.class);
            System.out.println("Singleton 2 from context 2, ID: " + singleton2.getInstanceId()); // Έξοδος: 2
        }
    }
    
  2. Διαφορετικά IDs beans (αν το bean οριστεί πολλές φορές): Αν το ίδιο κλάσμα καταχωριστεί σε ένα context με διαφορετικά bean IDs, το Spring θα δημιουργήσει ξεχωριστό singleton για κάθε ID. Αυτό δεν είναι ακριβώς "δύο singleton αυτού του κλάσματος", αλλά μάλλον δύο διαφορετικά beans, καθένα με singleton scope και το ίδιο κλάσμα υλοποίησης.

    // Διαμόρφωση με διαφορετικά IDs
    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();
        }
    }
    
    // Χρήση beans με διαφορετικά 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()); // Έξοδος: 1
            System.out.println("Singleton B ID: " + singletonB.getInstanceId()); // Έξοδος: 2
        }
    }
    

Παρά το γεγονός ότι υπάρχουν αυτές οι δυνατότητες, η τυπική και πιο συνηθισμένη χρήση του Singleton στο Spring υποδηλώνει ένα αντίγραφο του bean ενός συγκεκριμένου κλάσματος εντός ενός ApplicationContext.