Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Singleton guarantees single instance inside Spring container. That mean that you could have as many <code>singleton</code> instances as there are containers (even if they use the same JVM class loader). See <a href="http://static.springsource.org/spring/docs/3.2.1.RELEASE/spring-framework-reference/html/beans.html#beans-factory-scopes-singleton" rel="nofollow noreferrer">The singleton scope</a> and this answer for <a href="https://stackoverflow.com/questions/2637864/singleton-design-pattern-vs-singleton-beans-in-spring-container">Singleton Design Pattern vs. Singleton Bean</a> </p> <p>In your case you're instantiating two contexts - rootContext and servletContext and that were log statements are coming from.</p> <p>If you want to have single instance of every singleton - you could merge these two contexts.</p> <hr> <p>Small java class which illustrates such behavior(apologies for weird code formatting - wanted to make it more concise ):</p> <pre><code>package singleton; import java.util.UUID; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; public class AppMain { public static void main(String[] args) { final AnnotationConfigApplicationContext rootContext = new AnnotationConfigApplicationContext(); rootContext.register(AppRootContext.class); rootContext.refresh(); final AnnotationConfigApplicationContext servletContext = new AnnotationConfigApplicationContext(); servletContext.register(AppServletContext.class); servletContext.refresh(); final Singleton one = (Singleton) rootContext.getBean("singleton"); final Singleton andAnotherOne = (Singleton) servletContext.getBean("singleton"); System.out.println("Singletons are " + (one.ID.equals(andAnotherOne.ID) ? "equal" : "different")); } } @Component class Singleton {public String ID = UUID.randomUUID().toString();} @Configuration @ComponentScan(basePackages = { "singleton" }) class AppRootContext {} @Configuration @ComponentScan(basePackages = { "singleton" }) class AppServletContext {} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload