What is difference between singleton and prototype bean?

前端 未结 9 1861
攒了一身酷
攒了一身酷 2020-11-30 00:18

i\'m new to spring and i read this :

Basically a bean has scopes which defines their existence on the application

Singleton: means single bean def

9条回答
  •  孤城傲影
    2020-11-30 01:05

    Prototype scope: A new object is created each time it is injected.
    Singleton scope: The same object is returned each time it is injected.

    Prototype scope is used for all beans that are stateful, while the singleton scope should be used for stateless beans. Let me explain with my example. Please copy and run it by yourself to get a clear understanding. Consider an interface Coach.

    public interface Coach {
    
        public String getDailyWorkout();
    
        public String getDailyFortune();
    
    }
    

    We have another class called TrackCoach which implements Coach.

    public class TrackCoach implements Coach {
    
        private FortuneService fortuneService;
    
    
        public TrackCoach(FortuneService fortuneService) {
            this.fortuneService = fortuneService;
        }
    
        @Override
        public String getDailyWorkout() {
            return "Run a hard 5k";
        }
    
        @Override
        public String getDailyFortune() {
            return "Just Do it: " + fortuneService.getFortune();
        }    
    }
    

    Now there is a FortuneService interface.

    public interface FortuneService {
    
        public String getFortune();
    
    }
    

    It is implemented by our class HappyFortuneService.

    public class HappyFortuneService implements FortuneService {
    
        @Override
        public String getFortune() {
            return "Today is your lucky day!";
        }
    
    }
    

    Let's wire the two classes and inject an object bean of one class into another using Xml. Let's perform dependency injection. Note that we can do this using java annotation too.

    
    
    
    
        
    
        
        
        
    
        
    
    
            
            
        
    
    
    

    Notice that scope = singleton.

    Now let's define our BeanScopeDemoApp, which has our main method.

    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class BeanScopeDemoApp {
    
        public static void main(String[] args) {
    
            // load the spring configuration file 
            ClassPathXmlApplicationContext context = 
                    new ClassPathXmlApplicationContext("beanScope-applicationContext.xml");
    
            // retrieve bean from spring container 
            Coach theCoach = context.getBean("myCoach", Coach.class);
    
            Coach alphaCoach = context.getBean("myCoach", Coach.class);
    
            // check if they are the same 
            boolean result = (theCoach == alphaCoach);
    
            // print out the results 
            System.out.println("\nPointing to the same object: " + result);
    
            System.out.println("\nMemory location for theCoach: " + theCoach);
    
            System.out.println("\nMemory location for alphaCoach: " + alphaCoach +"\n");
    
            // close the context 
            context.close();
        }
    
    }
    

    Once you run the above code, you will see the following results:

    Pointing to the same object: true
    
    Memory location for theCoach: com.luv2code.springdemo.TrackCoach@16515bb7
    
    Memory location for alphaCoach: com.luv2code.springdemo.TrackCoach@16515bb7
    

    It's pointing the same object and occupies the same memory location after calling it twice. Now let's change the scope = prototype in our Xml file, save it and run the BeanScopeDemoApp again.
    You will see the following results:

    Pointing to the same object: false
    
    Memory location for theCoach: com.luv2code.springdemo.TrackCoach@6d4d203d
    
    Memory location for alphaCoach: com.luv2code.springdemo.TrackCoach@627fbcda
    

    It's pointing the different object and occupies the different memory locations after calling it twice. This would be a graphical illustration of what i have just said.

提交回复
热议问题