Struts2源码浅析-Container

Container容器接口定义

public interface Container extends Serializable {

  /**
   * Default dependency name.
   */
  String DEFAULT_NAME = "default";

  /**
   * 对当前对象 执行依赖注入
   * 引用了@Inject注解的 方法 字段 构造器 参数 
   * @param o 
   */
  void inject(Object o);

  /**
   * 创建实例 然后再依赖注入
   */
  <T> T inject(Class<T> implementation);

  /**
   * 返回容器中 类型 和名称匹配的bean
   */
  <T> T getInstance(Class<T> type, String name);

  /**
   * 返回容器中 类型匹配的bean  可能是多个
   */
  <T> T getInstance(Class<T> type);
  
  /**
   * 返回容器中 类型匹配的 bean名称 可能是多个 
   */
  Set<String> getInstanceNames(Class<?> type);

  /**
   * Sets the scope strategy for the current thread.
   */
  void setScopeStrategy(Scope.Strategy scopeStrategy);

  /**
   * Removes the scope strategy for the current thread.
   */
  void removeScopeStrategy();
}

容器的实现类

class ContainerImpl implements Container {
}

容器的创建 是由ContainerBuilder create方法完成

	public Container create(boolean loadSingletons) {
		ensureNotCreated();
		created = true;
		//factories 保存了 bean的定义  常量的定义
		//ContainerImpl factories  factoryNamesByType
		final ContainerImpl container = new ContainerImpl(new HashMap<Key<?>, InternalFactory<?>>(factories));
		if (loadSingletons) {
			container.callInContext(new ContainerImpl.ContextualCallable<Void>() {
				public Void call(InternalContext context) {
					for (InternalFactory<?> factory : singletonFactories) {
						factory.create(context);
					}
					return null;
				}
			});
		}
		// xml配置 bean节点 属性static 为true  默认为false
		//com.opensymphony.xwork2.config.providers.XmlConfigurationProvider#register() line 210
		container.injectStatics(staticInjections);
		return container;
	} 
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值