In the previous article, we briefly introduced the use of roboguice ([4] use of the injection framework roboguice :( your first System Service injection). Today, let's take a look at the method of using annotations for common Java objects:
(1) To use annotations for common Java objects in an activity, you must perform the following two steps:
①: Create an activity that inherits the roboactivity
②: Use @ inject to annotate pojo (common Java object)
Previously, we introduced the View Controls, resource files, system services, and other Android platform-specific annotation methods, roboguice also supports annotation plain old Java objects (common Java object ). However, roboguice only calls the non-parameter constructor of the object by default. Therefore, the class to be annotated must have the non-parameter constructor.
Let's look at the example below:
class MyActivity extends RoboActivity { @Inject Foo foo; // this will basically call new Foo();}
This annotation creates the instance by default through the non-parameter constructor.
(2) Use the constructor Annotation with Parameters
In some cases, we do not want to use the default constructor, but use a constructor with a specific parameter. You have the following two options:
①: You can annotate pojo constructor.
②: You can create a provider and a binding for implementation.
In the second method, I will write a special article to introduce it. At that time, you can take a look at the children's shoes. Today, let's focus on the first method.
Now let's assume that the foo class has a bar instance as the constructor of the parameter, and then annotate the constructor of the parameter Foo with @ inject, in this case, roboguice will use the constructor with parameters for instantiation. See the implementation below:
class Foo { Bar bar; @Inject public Foo(Bar bar) { this.bar = bar; }}
Now, how does roboguice obtain the bar instance? Like the rules of other annotators, bar's default constructor will still be used unless bar constructor with parameters also uses the @ inject annotation.
(3) use variable field annotations
We have obtained the foo instance through the default constructor or the parameter constructor annotated with @ inject. However, we can also use these two methods to initialize variables. That is, field injection is used.
In the previous articles, we have annotated the view space and resource files (resources). Now we can add the annotation of attribute fields in the foo class.
class Foo { @Inject Bar bar; // Roboguice doesn't need a constructor, but you might...}
In the code above, when roboguice creates a foo instance, it will first create a bar instance and directly annotate it to the field named after bar. This is equivalent to the preceding example of constructor injection. In this way, our code is more concise and easy to read. However, if other common Java objects do not use roboguice, it is difficult for us to use Foo objects.
If you use field or constructor, you can view the details of the Annotation on the following website:
Http://code.google.com/p/google-guice/wiki/Injections
[5] use of the injection framework roboguice: (your first pojo injection)