Jonathan
Jonathan

Reputation: 705

Does GWT deferred binding return singletons of the same type within the scope of a single EntryPoint?

Let's say I have a number of GWT modules which are used as libraries, and one module with an entry point which inherits all of the library modules.

Each of the submodules needs to access a single instance of SomeClass.

If I call GWT.create(SomeClass.class) in modules A & B, do I get the same instance? If so, is this guaranteed?

Upvotes: 3

Views: 770

Answers (1)

Colin Alworth
Colin Alworth

Reputation: 18331

No. GWT.create(SomeClass.class) compiles to new SomeClass(), unless there is a rebind rule of some kind - a replace-with or a generate-with rule will cause this to instead invoke the default constructor of whatever type is selected by those rules.

This means that GWT.create is not a suitable way to provide access to a singleton instance. Instead, consider some DI tool like Gin, or manual DI by always passing around the same instance. It is also possible to use the static keyword to keep a single instance where all code compiled into the same app can reference it.

Upvotes: 5

Related Questions