Container中关联的变量

package com.opensymphony.xwork2.inject;

/**
 * Dependency mapping key. Uniquely identified by the required type and name.
 *
 * @author crazybob@google.com (Bob Lee)
 */
class Key<T> {

  final Class<T> type;
  final String name;
  final int hashCode;
  // 注意此处private
  private Key(Class<T> type, String name) {
    if (type == null) {
      throw new NullPointerException("Type is null.");
    }
    if (name == null) {
      throw new NullPointerException("Name is null.");
    }

    this.type = type;
    this.name = name;

    hashCode = type.hashCode() * 31 + name.hashCode();
  }

  Class<T> getType() {
    return type;
  }

  String getName() {
    return name;
  }

  public int hashCode() {
    return hashCode;
  }

  public boolean equals(Object o) {
    if (!(o instanceof Key)) {
      return false;
    }
    if (o == this) {
      return true;
    }
    Key other = (Key) o;
    return name.equals(other.name) && type.equals(other.type);
  }

  public String toString() {
    return "[type=" + type.getName() + ", name='" + name + "']";
  }
  // 注意此处
  static <T> Key<T> newInstance(Class<T> type, String name) {
    return new Key<T>(type, name);
  }
}

 // InternalFactory<T>  定义

package com.opensymphony.xwork2.inject;

import java.io.Serializable;

/**
 * Creates objects which will be injected.
 *
 * @author crazybob@google.com (Bob Lee)
 */
// 接口定义
interface InternalFactory<T> extends Serializable {

  /**
   * Creates an object to be injected.
   *
   * @param context of this injection
   * @return instance to be injected
   */
  T create(InternalContext context);// 注意参数
}

 // InternalContext定义

package com.opensymphony.xwork2.inject;

import java.util.HashMap;
import java.util.Map;

/**
 * Internal context. Used to coordinate injections and support circular
 * dependencies.
 *
 * @author crazybob@google.com (Bob Lee)
 */
class InternalContext {

  final ContainerImpl container;// 注意final
  final Map<Object, ConstructionContext<?>> constructionContexts =
      new HashMap<Object, ConstructionContext<?>>();
  Scope.Strategy scopeStrategy;//
  ExternalContext<?> externalContext;// 冠梁ExternalContext<?>
  
//构造函数定义
  InternalContext(ContainerImpl container) {
    this.container = container;
  }

  public Container getContainer() {
    return container;
  }
//为何这样子定义呢?
  ContainerImpl getContainerImpl() {
    return container;
  }

  Scope.Strategy getScopeStrategy() {
    if (scopeStrategy == null) {
      scopeStrategy = (Scope.Strategy) container.localScopeStrategy.get();

      if (scopeStrategy == null) {
        throw new IllegalStateException("Scope strategy not set. "
            + "Please call Container.setScopeStrategy().");
      }
    }

    return scopeStrategy;
  }

  @SuppressWarnings("unchecked")
  <T> ConstructionContext<T> getConstructionContext(Object key) {
    ConstructionContext<T> constructionContext =
        (ConstructionContext<T>) constructionContexts.get(key);
    if (constructionContext == null) {
      constructionContext = new ConstructionContext<T>();
      constructionContexts.put(key, constructionContext);
    }
    return constructionContext;
  }
//第四个参数 的get set
  @SuppressWarnings("unchecked")
  <T> ExternalContext<T> getExternalContext() {
    return (ExternalContext<T>) externalContext;
  }

  void setExternalContext(ExternalContext<?> externalContext) {
    this.externalContext = externalContext;
  }
}

 //ExtralContext定义

package com.opensymphony.xwork2.inject;

import java.lang.reflect.Member;
import java.util.LinkedHashMap;

/**
 * An immutable snapshot of the current context which is safe to
 * expose to client code.
 *
 * @author crazybob@google.com (Bob Lee)
 */
class ExternalContext<T> implements Context {

  final Member member;//注意此处final
  final Key<T> key;
  final ContainerImpl container;
//此处是public
  public ExternalContext(Member member, Key<T> key, ContainerImpl container) {
    this.member = member;
    this.key = key;
    this.container = container;
  }

  public Class<T> getType() {
    return key.getType();
  }

  public Scope.Strategy getScopeStrategy() {
    return (Scope.Strategy) container.localScopeStrategy.get();
  }

  public Container getContainer() {
    return container;
  }

  public Member getMember() {
    return member;
  }

  public String getName() {
    return key.getName();
  }

  public String toString() {
    return "Context" + new LinkedHashMap<String, Object>() {{
      put("member", member);
      put("type", getType());
      put("name", getName());
      put("container", container);
    }}.toString();
  }
//此处为调用构造函数得到实例
  static <T> ExternalContext<T> newInstance(Member member, Key<T> key,
      ContainerImpl container) {
    return new ExternalContext<T>(member, key, container);
  }
}

 //ConstructContext<T> 在Jdk动态代理中有列出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值