Spring bean singleton duplication on low performance computers
<p>So I have this really weird and unsettling problem. On our low performance computers some of our singleton beans are duplicated during the Spring Context initialization. This only occurs on computers with low performing hardware and it happens consistently.</p>

<p>What I can tell so far is that it seems to be happening to a bunch of classes that are in a circular dependency and I suspect it may have something to do with the bean init method. The circular dependency is solved by injecting the LockScreen into the MainContentPane through the init method instead of the constructor.</p>

<p>I have two logs, one from a normal performance computer and one from a low performance computer. The logs shows the difference and the issue. The numbers at the end of the lines are the instance id from the method System.identityHashCode(object). The log format is:</p>

<pre><code>LOGLEVEL  [Thread ID]  LoggingClass  Message
</code></pre>

<p>On a normal performance computer the following printout occurs.</p>

<pre><code>INFO  [JavaFX Application Thread]  MainContentPane      Constructor: 869589588
INFO  [JavaFX Application Thread]  MainContentPane      Getting LockScreen In Spring Init....

INFO  [JavaFX Application Thread]  SessionHandler       Constructor: 939274676
INFO  [JavaFX Application Thread]  SessionHandler       Injected MainContentPane Instance: 869589588
INFO  [JavaFX Application Thread]  UserStateBinder      Constructor: 2010765576
INFO  [JavaFX Application Thread]  UserStateBinder      Injected SessionHandler Instance: 939274676
INFO  [JavaFX Application Thread]  LockScreenLockedController Constructor: 1866179042
INFO  [JavaFX Application Thread]  LockScreenLockedController Injected UserStateBinder Instance: 2010765576
INFO  [JavaFX Application Thread]  LockScreen           Constructor: 204176749
INFO  [JavaFX Application Thread]  LockScreen           Injected LockScreenLockedController Instance: 1866179042
INFO  [JavaFX Application Thread]  LockScreen           This instance: 204176749
INFO  [JavaFX Application Thread]  LockScreen           Bean Factory instance: 1371189401
INFO  [JavaFX Application Thread]  MainContentPane      Injected LockScreen Instance: 204176749
</code></pre>

<p>Here there are no duplicates.</p>

<p>But if we look at the log from a low performance computer we can see that there are duplicates created after the initialization similar to the one above.</p>

<pre><code>INFO  [JavaFX Application Thread]  MainContentPane      Constructor: 22324067
INFO  [JavaFX Application Thread]  MainContentPane      Getting LockScreen In Spring Init....

INFO  [JavaFX Application Thread]  SessionHandler       Constructor: 32463502
INFO  [JavaFX Application Thread]  SessionHandler       Injected MainContentPane Instance: 22324067
INFO  [JavaFX Application Thread]  UserStateBinder      Constructor: 19793387
INFO  [JavaFX Application Thread]  UserStateBinder      Injected SessionHandler Instance: 32463502
INFO  [JavaFX Application Thread]  LockScreenLockedController Constructor: 29065840
INFO  [JavaFX Application Thread]  LockScreenLockedController Injected UserStateBinder Instance: 19793387
INFO  [JavaFX Application Thread]  LockScreen           Constructor: 12729388
INFO  [JavaFX Application Thread]  LockScreen           Injected LockScreenLockedController Instance: 29065840
INFO  [JavaFX Application Thread]  LockScreen           This instance: 12729388
INFO  [JavaFX Application Thread]  LockScreen           Bean Factory instance: 30716643
INFO  [JavaFX Application Thread]  MainContentPane      Injected LockScreen Instance: 12729388

INFO  [JavaFX Application Thread]  SessionHandler       Constructor: 11043228
INFO  [JavaFX Application Thread]  SessionHandler       Injected MainContentPane Instance: 22324067
INFO  [JavaFX Application Thread]  UserStateBinder      Constructor: 24902967
INFO  [JavaFX Application Thread]  UserStateBinder      Injected SessionHandler Instance: 32463502
INFO  [JavaFX Application Thread]  LockScreenLockedController Constructor: 17521714
INFO  [JavaFX Application Thread]  LockScreenLockedController Injected UserStateBinder Instance: 19793387
INFO  [JavaFX Application Thread]  LockScreen           Constructor: 16791356
INFO  [JavaFX Application Thread]  LockScreen           Injected LockScreenLockedController Instance: 29065840
INFO  [JavaFX Application Thread]  LockScreen           This instance: 16791356
INFO  [JavaFX Application Thread]  LockScreen           Bean Factory instance: 30716643
</code></pre>

<p>Here we can see that there is a second set of instances created of all the classes except for the MainContentPane class. The new set of classes are dependency injected with the previous set of instances (check the id's) and the bean factory is the same instance as before.</p>

<p>All these messages are printed on the main thread (JavaFX Application Thread) so there doesn't seem to be a concurrency problem either.</p>

<p>The project also includes an embedded Jetty http server. I don't know if there is anything in Jetty that could cause this issue on a low performance computer.</p>

<p>Versions:</p>

<pre><code>JRE(incl. JavaFX): 1.8.0.101
Spring: 4.3.3.RELEASE
Jetty + Websocket: 9.3.6.v20151106
</code></pre>

<p>I suspected that this problem may have been solved by setting the Spring context setting setAllowBeanDefinitionOverriding(false). But that didn't help either.</p>

<pre><code>AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.setAllowBeanDefinitionOverriding(false);
        context.register(ClientContext.class,
                MainContext.class,
                CommonContext.class,
                CciContext.class,
                PersistenceContext.class,
                SimulatorContext.class);
        context.refresh();
</code></pre>

<p>Please tell me if there is any additional information that you need and thank you for any time you put into helping me.</p>

<p><strong>Edit:</strong> </p>

<p>I have now confirmed that all initilization and all bean access are happening from a single thread. 
I was only able to find two interesting facts in the trace log. 
First, it seems that all initilization happens in the same order even though it is the exact same software version (is Spring initializing in paralell?). 
Second, the medium performance computers does have duplication! They only have duplication (or maybe it's re-initilization?) on the UserStateBinder class. The high performance development computer does not have this issue at all. </p>

<p>We will probably remove spring from our project starting tomorrow as we have been unable to find a solution for this issue. I will still have the current version of the project available to me if anyone else would like me to test any theory.</p>