Correct way of making a singleton a Spring bean

前端 未结 6 809
野性不改
野性不改 2020-12-08 03:12

I am converting a singleton to a Spring bean, so that if the singleton fails to initialize, then entire web application\'s spring context doesn\'t load properly.

Th

6条回答
  •  悲哀的现实
    2020-12-08 03:47

    True singleton are hard to get working.

    Volatile double-checked locking also does not work property. Read about it on wiki http://en.wikipedia.org/wiki/Double-checked_locking

    Your best bet is to simply do this

    public class MySingleton {
    
        private static MySingleton INSTANCE = new MySingleton();
    

    That is if you do not have any constructor parameters in your real code.

提交回复
热议问题