github地址:https://github.com/feb07/IOCPro
1、概述
什么叫IOC,控制反轉(Inversion of Control,英文縮寫為IOC)。IOC框架也叫控制反轉框架,依賴注入框架。IOC的核心是解耦,解耦的目的是修改耦合對象時不影響另外的對象,降低關聯性,從Spring來看,在Spring中IOC更多的依賴的是xml配置,而Android的IOC不使用xml配置,使用注解+反射。一個類里面有很多個成員變量,傳統的寫法,要用這些成員變量,就new 出來用。IOC的原則是:不要new,這樣耦合度太高;配置個xml文件,里面標明哪個類,里面用了哪些成員變量,等待加載這個類的時候,注入(new)進去;
2、實現
這里的Android IOC框架,主要是幫大家注入所有的控件,布局文件,點擊事件。
舉個例子,一個activity有十幾個view,傳統做法是設置布局文件,然后一個個findViewById。現在的做法,Activity類上添加個注解,幫我們自動注入布局文件;聲明View的時候,添加一行注解,然后自動幫我們findViewById。
3、編碼
1)注入布局文件
定義注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface InjectContentView {
int value();
}
InjectContentView用于在類上使用,主要用于標明該Activity需要使用的布局文件。
@InjectContentView(R.layout.activity_main)
public class MainActivity extends BaseActivity {
}
簡單說下注解,關鍵字@interface @Target @Retention。
@Target表示該注解可以用于什么地方,可能的類型TYPE(類),FIELD(成員變量),可能的類型:
public enum ElementType {
/** Class, interface (including annotation type), or enum declaration */
TYPE,
/** Field declaration (includes enum constants) */
FIELD,
/** Method declaration */
METHOD,
/** Formal parameter declaration */
PARAMETER,
/** Constructor declaration */
CONSTRUCTOR,
/** Local variable declaration */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** Package declaration */
PACKAGE,
/**
* Type parameter declaration
*
* @since 1.8
*/
TYPE_PARAMETER,
/**
* Use of a type
*
* @since 1.8
*/
TYPE_USE
}
@Retention表示:表示需要在什么級別保存該注解信息;我們這里設置為運行時。
可能的類型:
public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
*/
SOURCE,
/**
* Annotations are to be recorded in the class file by the compiler
* but need not be retained by the VM at run time. This is the default
* behavior.
*/
CLASS,
/**
* Annotations are to be recorded in the class file by the compiler and
* retained by the VM at run time, so they may be read reflectively.
*
* @see java.lang.reflect.AnnotatedElement
*/
RUNTIME
}
注入布局文件代碼,通過獲取注解的值,反射setContentView方法
public static void injectLayout(Activity activity) {
Class<? extends Activity> clazz = activity.getClass();
//獲取InjectContentView注解
InjectContentView contentview = clazz.getAnnotation(InjectContentView.class);
if (contentview != null) {
int layout = contentview.value();
try {
//獲取setContentView方法
Method method = clazz.getMethod("setContentView", int.class);
method.setAccessible(true);
method.invoke(activity, layout);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2)注入view
定義注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface InjectView {
int value();
}
@InjectContentView(R.layout.activity_main)
public class MainActivity extends BaseActivity {
@InjectView(R.id.btn)
private Button btn;
@InjectView(R.id.tv)
private TextView tv;
}
注入view,獲取所有的類的屬性變量,通過獲取注解的值,反射findViewById方法,設置給屬性
public static void injectViews(Activity activity) {
Class<? extends Activity> clazz = activity.getClass();
//獲取所有屬性
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
InjectView injectview = field.getAnnotation(InjectView.class);
if (injectview != null) {
try {
int viewId = injectview.value();
Method method = clazz.getMethod("findViewById", int.class);
Object view = method.invoke(activity, viewId);
field.setAccessible(true);
field.set(activity, view);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
3)點擊事件注入
正常寫法:有三個要素,listener綁定方法setOnClickListener,事件類型View.OnClickListener,事件回調方法onClick
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "today is 2019-2-13", Toast.LENGTH_LONG).show();
}
});
定義注解
@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface EventBase {
String listenerSetter();//setOnClickListener方法
Class<?> listenerType();//View.OnClickListener
String methodName();//onclick回調方法
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@EventBase(listenerSetter = "setOnClickListener", listenerType = View.OnClickListener.class, methodName = "onClick")
public @interface InjectClick {
int[] value();
}
如果是onclick事件,可以按以上的寫法,如果是onitemclick,只需要修改@EventBase三要素的value的值。同理可IOC注入Recyclerview、listview的onitemclick事件。
注入代碼:涉及到動態方法代理,即用自定義方法clickMethod(View v),代理了原本的onclick(View v)方法, 需要注意的是代理方法的入參需要與被代理方法的入參一致。
@InjectClick(R.id.btn)
public void clickMethod(View view) {
Toast.makeText(MainActivity.this, "today is 2019-2-13", Toast.LENGTH_LONG).show();
}
public static void injectEvents(Activity activity) {
Class<? extends Activity> clazz = activity.getClass();
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
//拿到方法的所有注解
Annotation[] annotations = method.getAnnotations();
for (Annotation annotation : annotations) {
Class<? extends Annotation> annotationType = annotation.annotationType();
//拿到注解的注解
EventBase eventBaseAnnotation = annotationType.getAnnotation(EventBase.class);
if (eventBaseAnnotation != null) {
String listenerSetter = eventBaseAnnotation.listenerSetter();
Class<?> listenerType = eventBaseAnnotation.listenerType();
String methodName = eventBaseAnnotation.methodName();
try {
ClickInvocationHandler handler = new ClickInvocationHandler(activity);
handler.addMethod(methodName, method);
Object listener = Proxy.newProxyInstance(listenerType.getClassLoader(), new Class<?>[]{listenerType}, handler);
Method value = annotationType.getDeclaredMethod("value");
int[] viewIds = (int[]) value.invoke(annotation, null);
for (int viewid : viewIds) {
View view = activity.findViewById(viewid);
Method setEventListenerMethod = view.getClass().getMethod(listenerSetter, listenerType);
setEventListenerMethod.invoke(view, listener);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
java動態代理相關:Proxy與InvocationHandler
public class ClickInvocationHandler implements InvocationHandler {
private Object target;
private HashMap<String, Method> methodHashMap = new HashMap<>();
public ClickInvocationHandler(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String methodName = method.getName();
if (target != null) {
method = methodHashMap.get(methodName);
if (method != null) {
return method.invoke(target, args);
}
}
return null;
}
public void addMethod(String methodName, Method method) {
methodHashMap.put(methodName, method);
}
}
附上完整的InjectManager代碼,以及BaseActivity代碼
public class InjectManager {
public static void inject(Activity activity) {
injectLayout(activity);
injectViews(activity);
injectEvents(activity);
}
public static void injectLayout(Activity activity) {
Class<? extends Activity> clazz = activity.getClass();
//獲取InjectContentView注解
InjectContentView contentview = clazz.getAnnotation(InjectContentView.class);
if (contentview != null) {
int layout = contentview.value();
try {
//獲取setContentView方法
Method method = clazz.getMethod("setContentView", int.class);
method.setAccessible(true);
method.invoke(activity, layout);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void injectViews(Activity activity) {
Class<? extends Activity> clazz = activity.getClass();
//獲取所有屬性
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
InjectView injectview = field.getAnnotation(InjectView.class);
if (injectview != null) {
try {
int viewId = injectview.value();
Method method = clazz.getMethod("findViewById", int.class);
Object view = method.invoke(activity, viewId);
field.setAccessible(true);
field.set(activity, view);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
public static void injectEvents(Activity activity) {
Class<? extends Activity> clazz = activity.getClass();
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
//拿到方法的所有注解
Annotation[] annotations = method.getAnnotations();
for (Annotation annotation : annotations) {
Class<? extends Annotation> annotationType = annotation.annotationType();
//拿到注解的注解
EventBase eventBaseAnnotation = annotationType.getAnnotation(EventBase.class);
if (eventBaseAnnotation != null) {
String listenerSetter = eventBaseAnnotation.listenerSetter();
Class<?> listenerType = eventBaseAnnotation.listenerType();
String methodName = eventBaseAnnotation.methodName();
try {
ClickInvocationHandler handler = new ClickInvocationHandler(activity);
handler.addMethod(methodName, method);
Object listener = Proxy.newProxyInstance(listenerType.getClassLoader(), new Class<?>[]{listenerType}, handler);
Method value = annotationType.getDeclaredMethod("value");
int[] viewIds = (int[]) value.invoke(annotation, null);
for (int viewid : viewIds) {
View view = activity.findViewById(viewid);
Method setEventListenerMethod = view.getClass().getMethod(listenerSetter, listenerType);
setEventListenerMethod.invoke(view, listener);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
public class BaseActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
InjectManager.inject(this);
}
}
MainActivity代碼:可以看出在activity中,布局文件,view,點擊事件的注入,使代碼更簡潔
@InjectContentView(R.layout.activity_main)
public class MainActivity extends BaseActivity {
@InjectView(R.id.btn)
private Button btn;
@InjectView(R.id.tv)
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@InjectClick(R.id.btn)
public void clickMethod(View view) {
Toast.makeText(MainActivity.this, "today is 2019-2-13", Toast.LENGTH_LONG).show();
}
}