`
seadragonnj
  • 浏览: 57237 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

深入研究xwork(二)

阅读更多
下面说一下create(..)方法干些什么东东(我说的create(..)是这个哦new ContainerBuilder().factory(IPerson.class,Person.class).factory(IUserPerson.class,UserPerson.class).create(true))
java 代码
  1. final ContainerImpl container = new ContainerImpl(
  2. new HashMap<key>, InternalFactory>(factories)); </key>
  3. if (loadSingletons) {
  4. container.callInContext(new ContainerImpl.ContextualCallable<void>() { </void>
  5. public Void call(InternalContext context) {
  6. for (InternalFactory factory : singletonFactories) {
  7. factory.create(context);
  8. }
  9. return null;
  10. }
  11. });
  12. }
首先它创建一个ContainerImpl对象,这个对象的创建过程会做很多很多的事情,一些需要依赖注入的东西会在这里完成还是前面说的,它并没有完成真正的依赖注入(对象都没有创建当然没法注入了),它只是完成依赖注入所需要的一些信息,当然它不只完成这些,我现在还有点迷糊,等过两天再好好整理这一块!!
java 代码
  1. container.injectStatics(staticInjections);
这段代码是进行static field 和static method的注入,众所周知static是和对象无关的,所以可以在没有实例化对象前进行注入

java 代码
  1. IUserPerson person=container.getInstance(IUserPerson.class);
这行代码就是实例化一个对象,这个过程会完成所有的真的依赖注入工作

  1. container.getInstance(..)
这个方法中会调用如下一段代码
  1. ExternalContext previous = context.getExternalContext();
  2. Key<t> key = Key.newInstance(type, name); </t>
  3. context.setExternalContext(ExternalContext.newInstance(null, key, this));
  4. try {
  5. InternalFactory o = getFactory(key);
  6. if (o != null) {
  7. return getFactory(key).create(context);
  8. } else {
  9. return null;
  10. }
  11. } finally {
  12. context.setExternalContext(previous);
  13. }
在这里通过Key.newInstance(type, name);创建一个Key,这个Key的type和name和调用factory(..)时放到factories中的Key相对应,所以通过InternalFactory o = getFactory(key);这个方法就能得到前面在factory(..)中创建的InternalFactory对象,进而可以调用它的create(..)方法(个人觉得这个设计的很精妙!),我们再来重新看一下这个方法的实现
  1. public T create(InternalContext context) {  
  2.   if (constructor == null) {  
  3.     this.constructor =  
  4.         context.getContainerImpl().getConstructor(implementation);  
  5.   }  
  6.   return (T) constructor.construct(context, type);  
  7. }  
这段代码通过调用
constructor.construct(context, type);来创建一个对象,并进行依赖注入,
 
  1. Object construct(InternalContext context, Class<? super T> expectedType) {  
  2.    ConstructionContext<T> constructionContext =  
  3.        context.getConstructionContext(this);  
  4.   
  5.    // We have a circular reference between constructors. Return a proxy.  
  6.    if (constructionContext.isConstructing()) {  
  7.      // TODO (crazybob): if we can't proxy this object, can we proxy the  
  8.      // other object?  
  9.      return constructionContext.createProxy(expectedType);  
  10.    }  
  11.   
  12.    // If we're re-entering this factory while injecting fields or methods,  
  13.    // return the same instance. This prevents infinite loops.  
  14.    T t = constructionContext.getCurrentReference();  
  15.    if (t != null) {  
  16.      return t;  
  17.    }  
  18.   
  19.    try {  
  20.      // First time through...  
  21.      constructionContext.startConstruction();  
  22.      try {  
  23.        Object[] parameters =  
  24.            getParameters(constructor, context, parameterInjectors);  
  25.        t = constructor.newInstance(parameters);  
  26.        constructionContext.setProxyDelegates(t);  
  27.      } finally {  
  28.        constructionContext.finishConstruction();  
  29.      }  
  30.   
  31.      // Store reference. If an injector re-enters this factory, they'll  
  32.      // get the same reference.  
  33.      constructionContext.setCurrentReference(t);  
  34.   
  35.      // Inject fields and methods.  
  36.      for (Injector injector : injectors) {  
  37.        injector.inject(context, t);  
  38.      }  
  39.   
  40.      return t;  
  41.    } catch (InstantiationException e) {  
  42.      throw new RuntimeException(e);  
  43.    } catch (IllegalAccessException e) {  
  44.      throw new RuntimeException(e);  
  45.    } catch (InvocationTargetException e) {  
  46.      throw new RuntimeException(e);  
  47.    } finally {  
  48.      constructionContext.removeCurrentReference();  
  49.    }  
  50.  } 
这就是xwork进行依赖注入的过程,写的比较粗浅(不过第一次写原创,还是鼓励一下),
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics