你是否常編一些小工具,做一些小東西?
在你擴展維護的時候,是不是覺得實例傳遞越來越復雜?
你經常改變想法,但小東西不停重構開始耽誤你越來越多的時間?
你想到了用spring,但又不想對於一個簡單的小東西,引入一個相對小重的框架,讓你的小程序加載時都需要卡一下?
哈,試一試用下simpleFactory,是否可以解決你的煩惱,只有一個類哦,copy一下,就可以幫助你實現依賴注入;
simpleFactory是我在做小實驗時,為了解決上面的問題,自己寫的編碼小工具,至少自己用起來很哈皮
SF代碼:
- package com.honeysuckle.webx.context;
- import Java.lang.reflect.Constructor;
- import Java.lang.reflect.Method;
- import Java.util.HashMap;
- import Java.util.LinkedList;
- import Java.util.Map;
- import Java.util.Queue;
- /**
- * 類SF.Java的實現描述:SimpleBeanFactory no one simpler than this one
- *
- * @author yanghang 2010-9-16 下午04:43:05
- */
- public class SF {
- private final Map<Class<?>, Object> context = new HashMap<Class<?>, Object>();
- /**
- * 向容器中放入某個實例
- * @param clazz 該實例的類型,可以是接口
- * @param object 該實例
- */
- public void put(Class<?> clazz, Object object) {
- this.context.put(clazz, object);
- }
- /**
- * 獲取某個實例(單例) <br/>
- * 如果容器中不存在,會嘗試初使化: <br/>
- * 1.調用構造函數,args為可能的構造函數的參數 <br/>
- * 2.會對該實例的set方法進行注入 <br/>
- * 3.最後會調用init()方法 <br/>
- */
- public Object get(Class<?> clazz, Object... args) {
- Object object = this.context.get(clazz);
- if (object == null) {
- try {
- object = this.instanceClazz(clazz, args);
- this.instanceSetMethod(object);
- this.instanceInitMethod(object);
- this.put(clazz, object);
- } catch (Exception e) {
- return null;
- }
- }
- return object;
- }
- //獲取某個實例的多例,每次都會重新產生該實例
- public Object getMulti(Class<?> clazz, Object... args) {
- Object object;
- try {
- object = this.instanceClazz(clazz, args);
- this.instanceSetMethod(object);
- this.instanceInitMethod(object);
- } catch (Exception e) {
- return null;
- }
- return object;
- }
- //初使化該類,調用構造方法
- private Object instanceClazz(Class<?> clazz, Object[] args) throws Exception {
- Object object = null;
- Class<?>[] clazzes = new Class[args.length];
- for (int i = 0; i < args.length; i++) {
- clazzes[i] = args[i].getClass();
- }
- Constructor<?>[] cons = clazz.getConstructors();
- for (Constructor<?> con : cons) {
- if (con.getParameterTypes().length != args.length) continue;
- object = con.newInstance(args);
- }
- return object;
- }
- //根據set方法,進行注入
- private void instanceSetMethod(Object object) {
- Method[] methods = object.getClass().getDeclaredMethods();
- Queue<Method> queue = new LinkedList<Method>();
- for (Method method : methods) {
- queue.add(method);
- }
- for (int i = queue.size(); queue.size() > 0; i = queue.size()) {
- Method method = queue.poll();
- if (method.getName().indexOf("set") != 0) continue;
- Class<?>[] clazzes = method.getParameterTypes();
- if (clazzes.length != 1) continue;
- Class<?> pclazz = clazzes[0];
- try {
- Object pObject = this.get(pclazz);
- method.invoke(object, new Object[] { pObject });
- } catch (Exception e) {
- continue;
- }
- if (i == queue.size()) break;
- }
- }
- // 初使化該類,調用init方法
- private void instanceInitMethod(Object object) {
- try {
- Method method = object.getClass().getMethod("init", new Class<?>[] {});
- method.invoke(object, new Object[] {});
- } catch (Exception e) {
- return;
- }
- }
- }
用法示例
有類Bean1
- public class Bean1 {
- private String hello;
- public void init() {
- this.hello = "hello kitty";
- }
- public void show() {
- System.out.println(this.hello);
- }
- }
執行 TestSF
- public class TestSF {
- private Bean1 bean;
- public void setBean(Bean1 bean) {
- this.bean = bean;
- }
- public void test() {
- this.bean.show();
- }
- public static void main(String[] args) {
- SF sf = new SF();
- TestSF test = (TestSF) sf.get(TestSF.class);
- test.test();
- }
- }
輸出:
- hello kitty