To the current location, the Afinal development framework has been used for several months, remember the first time when using annotations to complete the initialization of the control and event binding, then the mood is how excited-the code can be written like this! Then with the continuous learning, but also slowly on the IOC framework and annotation reflection and other things have a little simple understanding, a previous article briefly introduced the Java reflection mechanism, today's article, completed a simple, based on the IOC small demo, let everyone slowly to the IOC have a little simple understanding.
First, what is the IOC?
Control inversion (inversion of the English abbreviation for IOC) is an important object-oriented programming principle to reduce the coupling problem of computer programs, and is also the core of the lightweight spring framework. Control reversals are generally divided into two types, dependency injection (Dependency injection, short di), and dependent lookups. The application of dependency injection is quite extensive.
What we're going to do next is the implementation of dependency injection in Android.
First, look at our project structure.
The structure is simple, a base class, a subclass, a custom annotation type, a layout file.
Here's a look at the specific implementation of the Code
First look at the most important base class
Baseactivity.java
Package Com.example.iocdemo;import Java.lang.reflect.field;import Android.app.activity;import Android.content.context;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.toast;public class Baseactivity extends Activity {protected Context Mcontext = this;/** * Implement IOC injection * * @par Am baseactivity */public void Initinjectedview (Object baseactivity) {//Get all member variables field[] fields = Baseactivity.getclass ( ). Getdeclaredfields (); if (Fields! = null && fields.length > 0) {//Traverse member variable for (Field field:fields) {try {//Suppress Permission Check field.setaccessible (TRUE);//Gets the comment class of the member variable Viewinject viewinject = field.getannotation (Viewinject.class);// If the annotation class is not empty, that is, the member variable is declared using the comment method if (viewinject! = null) {//Gets the Idint ID in the comment = Viewinject.id ();//Set the field value Field.set (This, ( Activity) baseactivity. Findviewbyid (ID));//Remove the materialized View object from view view = (view) field.get (this);// Bind Listener Event View.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {toast.maketext (Mcontext, " Don't order it! "ToAst. Length_short). Show ();}});}} catch (Exception e) {e.printstacktrace ();}}}}
In this class, we have completed the preparation of the IOC injection method, and the custom annotation type code is as follows:
Package Com.example.iocdemo;import Java.lang.annotation.elementtype;import Java.lang.annotation.retention;import Java.lang.annotation.retentionpolicy;import java.lang.annotation.Target; @Target (Elementtype.field) @Retention ( Retentionpolicy.runtime) Public @interface the viewinject {public int id ();}
@Target (Elementtype.field)This code implements the control of the position of the comment as a field, or a member variable, because we are going to complete the comment of the control, and the time binding, so we set it toElementtype.fieldWe can.
@Retention is an enum type with a total of three values, Source,class and RUNTIME, respectively.
source represents the annotation type of information will only be retained in the program source code, if the source code is compiled, the annotation data will disappear, and will not be kept in the compiled. class file .
Class means that this annotation type of information is kept in the program source code, but also in the compiled. class file, which does not load this information into the virtual machine (JVM) when executed. . Note that when you do not set a retention value for a annotation type, the system default value is class.
RUNTIME, which means that the information is kept in the source code, the compiled. class file, and the information is loaded into the JVM when it is executed .
Because we need to complete the injection after the JVM has loaded our class file, we select this property.
Well, now that we know how to simply define a custom annotation type, and code injection and event binding, let's look at how we use it in our program.
Here is the code for our activity
Package Com.example.iocdemo;import Android.os.bundle;import Android.widget.button;public class MainActivity extends baseactivity {//@viewinject (id = r.id.btn) Button BTN for control initialization with annotations, @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);// To inject Initinjectedview (this);}}
Here are the results of our operation