[1] use of the injection framework RoboGuice: (A brief example of what RoboGuice does), roboguicebrief

Source: Internet
Author: User

[1] use of the injection framework RoboGuice: (A brief example of what RoboGuice does), roboguicebrief

When developing Android projects, for example, we often need to use various View Controls, declare them, findViewById, and perform strong conversion. Every time you write such code, it is very cumbersome and error-prone. In this case, Dependency injection can greatly reduce dependencies between classes and describe dependencies between classes through annotation (Java, this avoids directly calling similar constructors or using Factory to participate in the required classes, which reduces coupling between classes or modules, improves code reuse and enhances code maintainability. Google Guice provides a lightweight Dependency injection framework on the Java platform and supports Android application development. The following describes how to use Google Guice on the Android platform.

To put it simply, Guice reduces the call of the new and Factory functions in Java code. You can regard Guice's @ Inject as a substitute for new. You may need to write some Factory methods to use Guice, but your code will not rely on these Factory methods to create instances. Using Guice to modify code makes code reuse easier for unit tests.

GitHub Open Source Address: https://github.com/roboguice/roboguice

RoboGuice is a library developed based on Google Guice on the Android platform. It can greatly simplify the code for Android Application Development and some tedious and repetitive code. For example, you may need to use findViewById to search for a View in XML and forcibly convert it to the desired type. onCreate may have a lot of similar code. RoboGuice allows annotation to describe the relationship between views. The rest of the work is done by the roboGuice library. To give you an idea, take a look at this simple example of a typical Android activity:

Let's take a look at an example of a more common Activity:

<span style="font-size:18px;"> class AndroidWay extends Activity {         TextView name;         ImageView thumbnail;         LocationManager loc;         Drawable icon;         String myName;         public void onCreate(Bundle savedInstanceState) {             super.onCreate(savedInstanceState);             setContentView(R.layout.main);            name      = (TextView) findViewById(R.id.name);             thumbnail = (ImageView) findViewById(R.id.thumbnail);             loc       = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);             icon      = getResources().getDrawable(R.drawable.icon);             myName    = getString(R.string.app_name);             name.setText( "Hello, " + myName );         }     } </span>
If you are reading the code in the onCreate () method, you must skip the five lines of control or Resource Initialization code, then we can see the most critical code (name. settext ("Hello," + myName ));

Now we use the RoboGuice framework to implement the same functions:

<span style="font-size:18px;"> @ContentView(R.layout.main)    class RoboWay extends RoboActivity {         @InjectView(R.id.name)             TextView name;         @InjectView(R.id.thumbnail)        ImageView thumbnail;         @InjectResource(R.drawable.icon)   Drawable icon;         @InjectResource(R.string.app_name) String myName;         @Inject                            LocationManager loc;         public void onCreate(Bundle savedInstanceState) {             super.onCreate(savedInstanceState);             name.setText( "Hello, " + myName );         }     } </span>
In this example, onCreate () removes a lot of resources. The control initialization Code directly enters the business logic. You only need to use @ InjectView to describe the relationship between the view and Id, roboGuice automatically completes the remaining work. Is it more concise.

In addition, RoboGuice can be used for system services, resource files, View controls, and even custom objects. The above is just a brief introduction to RoboGuice. I will continue to explain the specific use of RoboGuice in the following articles.

Reference address:

RoboGuice Home: https://github.com/roboguice/roboguice/wiki

RoboGuice jar package required download: http://www.imobilebbs.com/wordpress/archives/3111



Android RoboGuice User Guide (12): How to bind a generic type

[Java] class Example {@ Injectvoid setList (List <String list ){...}} class Example {@ Injectvoid setList (List <String list ){...}} you can use TypeLiteral to create this binding. TypeLiteral is a special type that can be used to represent parameterized types. [Java] @ Overridepublic void configure () {bind (new TypeLiteral <List <String (){}). toInstance (new ArrayList <String () ;}@ Override public void configure () {bind (new TypeLiteral <List <String (){}). toInstance (new ArrayList <String ();} or use the @ Provides method: [java] @ ProvidesList <String providesListOfString () {return new ArrayList <String ();} @ Provides List <String providesListOfString () {return new ArrayList <String () ;} So far, I have basically introduced Google Guice usage. The above usage is also applicable to JavaSE and Java EE platforms. For more information, see the English documentation, next we will introduce the Dependency Injection (Roboguice) usage related to the Android platform.

Android RoboGuice User Guide (12): How to bind a generic type

[Java] class Example {@ Injectvoid setList (List <String list ){...}} class Example {@ Injectvoid setList (List <String list ){...}} you can use TypeLiteral to create this binding. TypeLiteral is a special type that can be used to represent parameterized types. [Java] @ Overridepublic void configure () {bind (new TypeLiteral <List <String (){}). toInstance (new ArrayList <String () ;}@ Override public void configure () {bind (new TypeLiteral <List <String (){}). toInstance (new ArrayList <String ();} or use the @ Provides method: [java] @ ProvidesList <String providesListOfString () {return new ArrayList <String ();} @ Provides List <String providesListOfString () {return new ArrayList <String () ;} So far, I have basically introduced Google Guice usage. The above usage is also applicable to JavaSE and Java EE platforms. For more information, see the English documentation, next we will introduce the Dependency Injection (Roboguice) usage related to the Android platform.

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.