你是否常编一些小工具,做一些小东西?
在你扩展维护的时候,是不是觉得实例传递越来越复杂?
你经常改变想法,但小东西不停重构开始耽误你越来越多的时间?
你想到了用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