What does GWT.create mean and why should I use it?

I am new to GWT. I have a line of code.

SomeClientServiceAsync someService = GWT.create(SomeClientService.class); 

What does the above do, and why can't I use any other alternatives to create it?

Please help me!

Thanks.

+4
source share
3 answers

GWT.create used for deferred binding. This allows you to provide different implementations of the same service based on a user browser. See the next question:

Why use GWT.create () instead of a new one?

If you do not need to have several implementations of your service, just create it using the new one!

+7
source

GWT works by creating a service like RMI does. Here you create the service SomeClientService, which is located in the client package. It contains all the functions that can be called server-side.

+1
source

GWT.create works differently:

  • He tries to see if there are any declarations in the gwt.xml files about which implementation to use depending on the GWT property. This GWT property can be a well-known user agent, which in this case will have the effect of choosing different implementations for each browser, but it can also be used for other things, for example, to disable logging (the fact that logging is enabled or not has nothing to do with which browser it works in)

Example:

 <replace-with class="com.x.app.client.ui.base.button.CustomSlicedButtonCellAppearance"> <when-type-is class="com.x.app.client.ui.base.button.CustomButtonCellAppearance" /> <when-property-is name="gxt.css3.enabled" value="false"/> <when-property-is name="gxt.theme" value="themeName" /> </replace-with> 

In this case, it will use CustomSlicedButtonCellAppearance to call GWT.create (CustomButtonCellAppearance.class) only if css3 is not supported for this topic either. Note that "when-property-is" is optional, and if it is not specified, it will always use this implementation for this interface.

  • It also looks for generators, in which case a new class is generated at compile time by GWT (or in devmode), usually based on the annotations that are present in the interface passed to the create method.

Example:

 <generate-with class="org.fusesource.restygwt.rebind.RestServiceGenerator"> <when-type-assignable class="org.fusesource.restygwt.client.RestService" /> </generate-with> 

In this case, RestServiceGenerator will generate the code to send the request. Another example is how UIBinder works: in addition to using annotations in the interface, it also generates code based on what is inside the ui.xml file.

  • If no declaration matches the class / interface passed to the GWT.create method, then it will try to make a new one in this class (in case of interface failure).

Declarations in gwt.xml files can be overwritten by other declarations, which are subsequently processed, therefore, if you use a module that declares a rule, you can change this rule by declaring a new rule after inheriting the declaration of the module containing the original declaration.

+1
source

Source: https://habr.com/ru/post/1391409/


All Articles