Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between singleton and eagersingleton for Gin or Guice?

I was wondering what is the different between singleton and eagersingleton for Gin or Guice?

like image 566
user_1357 Avatar asked May 02 '13 15:05

user_1357


People also ask

What is singleton in Guice?

Guice comes with a built-in @Singleton scope that reuses the same instance during the lifetime of an application within a single injector. Both javax. inject. Singleton and com.

What is an eager singleton?

Eager initialization: This is the simplest method of creating a singleton class. In this, object of class is created when it is loaded to the memory by JVM. It is done by assigning the reference of an instance directly.


1 Answers

From Guice's Wiki

Eager singletons reveal initialization problems sooner, and ensure end-users get a consistent, snappy experience. Lazy singletons enable a faster edit-compile-run development cycle. Use the Stage enum to specify which strategy should be used.

  • Eager: "create an instance of this class when the Module is being run"
  • Lazy: "create an instance of this class when it's first requested for dependency injection"
like image 104
durron597 Avatar answered Oct 17 '22 23:10

durron597