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;
}