The singleton bean scope

In Spring, any bean that has a singleton scope has only one instance of the bean created for an application context, where it is defined for the entire application. This is the default behavior of the Spring container. But it is different from the singleton pattern as defined in the Gang of Four (GoF) patterns book. In Java, singleton means per object of a particular class per Classloader in the JVM. But in Spring, it implies per instance of a bean per bean definition per Spring IoC container. This is explained in the following diagram:

As you can see in the preceding diagram, the same instance of the object is defined by the bean definition, accountRepository, injected to other collaborating beans in the same IoC container. Spring stores all singleton bean instances in a cache, and all collaborating beans fetch the dependency of that object returned by the cache.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset