Android Roboguice Usage Guide (3) Bindings overview

Source: Internet
Author: User
Tags bind java se

The dependencies between classes in an application can be very complex, created in a class instance, you need to first create an example of the class on which the class depends, and you create instances of the dependent classes, which may depend on other classes, and so on. So when you create a class instance, you are creating an object graph object.

Creating object graph by hand is a tedious and error-prone process, and it's hard to test your code, and Guice or roboguice can help you create object graph, and the job is to configure dependencies between classes and classes.

The module (Modules) is the basic building block Guice constructs object graph, and the work of constructing object graph in Guice is done by a class called "Injector".

Guice is a subclass of Abstractmoudule for modules, while Roboguice is a subclass of Abstractandroidmodule for modules. Roboguice uses Injector to create objects that are dependent, and Injector constructs a diagram of classes in reference to bindings defined in module.

For example, if you are familiar with make file or other build systems (such as WiX). You use makefile to define the source files that are dependent on the objects you want to compile, which may depend on other libraries or header files. These dependencies defined by makefile correspond to the bindings defined in Roboguice for the module.

When you compile a target program with make, makes looks at the dependencies in the makefile, compiles the dependent objects first, and then eventually compiles target. corresponding to Roboguide (Guice) creates an object for injector, which first creates the dependent objects based on the defined bindings until the desired object is created.

In the HelloWorld example, we did not see the direct use of injector, because Roboguice called injector to create the Igreetingservice object for us.

If, in some cases, you want to use injector directly, you can use the Roboactivity getinjector ().

For example, modify Guicedemo, remove @inject igreetingservice Greetingservce and use injector getinstance to create Igreetingservice instances.

Public 

class Guicedemo extends roboactivity  {     
          
 @InjectView (R.id.hello) TextView Hellolabel;     
 @Inject Igreetingservice Greetingservce;     

 @Override public 
 void OnCreate (Bundle savedinstancestate) {     
 super.oncreate (savedinstancestate);     
 Setcontentview (r.layout.main);     

 Injector injector=getinjector ();     
 Igreetingservice greetingservce     
 =injector.getinstance (igreetingservice.class);     
 Hellolabel.settext (Greetingservce.getgreetings ());     
 }     

The module is still bound to Hellochina.

public class 

Greetingmodule extends abstractandroidmodule{     

 @Override 
 protected void Configure () {     
 // Bind (Igreetingservice.class). to (Helloworld.class);     
 Bind (Igreetingservice.class). to (Hellochina.class);     

 }

}

Injector's job is to construct object Graph, and when you call getinstance to construct an object of a type, Injector automatically creates an instance of the desired class based on the dependencies between the classes.

The way to define dependencies between classes is to overload their configure methods by extending Abstractandroidmodule. Define various bindings in the Configure method. These methods also do type detection, and if the type used is incorrect, the compiler will give an error.

The binding bindings can have the following types:

Linked bindings

Instance Bindings

@provider methods

Provider Bindings

Constructor bindings

untargetted Bindings

Built-in bindings

Just-in-time Bindings

Providers, etc.

These bindings are described one after the other, these bindings are generic and Android platforms are not very relevant, can be used for Java EE, Java SE Platform, Roboguice provides the Android platform-related dependency Injector, the following is also described in detail.

View a full set of articles: Http://www.bianceng.cn/OS/extra/201301/34950.htm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.