ContainerBuilder中的public Container create(boolean loadSingletons)
① final ContainerImpl container = new ContainerImpl(
new HashMap<Key<?>, InternalFactory<?>>(factories));
this.factories = factories;
ContainerImpl的构造方法中,用factories的Key部分构件一个新Map
for (Key<?> key : factories.keySet()) {
Set<String> names = map.get(key.getType());
if (names == null) {
names = new HashSet<String>();
map.put(key.getType(), names);
}
names.add(key.getName());
}
for (Entry<Class<?>,Set<String>> entry : map.entrySet()) {
entry.setValue(Collections.unmodifiableSet(entry.getValue()));
}
使Map的Value只读,即一些default、struts之类的值
Returns an unmodifiable view of the specified set. This method allows modules to provide users with "read-only" access to internal sets. Query operations on the returned set "read through" to the specified set, and attempts to modify the returned set, whether direct or via its iterator, result in an UnsupportedOperationException.
this.factoryNamesByType = Collections.unmodifiableMap(map) 不可修改
{type=class com.opensymphony.xwork2.ObjectFactory, name=default, implementation=class com.opensymphony.xwork2.ObjectFactory, scope=SINGLETON}
② container.callInContext(new ContainerImpl.ContextualCallable<Void>() {
public Void call(InternalContext context) {
for (InternalFactory<?> factory : singletonFactories) {
factory.create(context);
}
return null;
}
})
callInContext 创建了一个InternalContext
匿名ContextualCallable的实例调用call(创建的InternalContext)
对单例工厂中的每个工厂,factory.create(context)
① final ContainerImpl container = new ContainerImpl(
new HashMap<Key<?>, InternalFactory<?>>(factories));
this.factories = factories;
ContainerImpl的构造方法中,用factories的Key部分构件一个新Map
for (Key<?> key : factories.keySet()) {
Set<String> names = map.get(key.getType());
if (names == null) {
names = new HashSet<String>();
map.put(key.getType(), names);
}
names.add(key.getName());
}
for (Entry<Class<?>,Set<String>> entry : map.entrySet()) {
entry.setValue(Collections.unmodifiableSet(entry.getValue()));
}
使Map的Value只读,即一些default、struts之类的值
Returns an unmodifiable view of the specified set. This method allows modules to provide users with "read-only" access to internal sets. Query operations on the returned set "read through" to the specified set, and attempts to modify the returned set, whether direct or via its iterator, result in an UnsupportedOperationException.
this.factoryNamesByType = Collections.unmodifiableMap(map) 不可修改
{type=class com.opensymphony.xwork2.ObjectFactory, name=default, implementation=class com.opensymphony.xwork2.ObjectFactory, scope=SINGLETON}
② container.callInContext(new ContainerImpl.ContextualCallable<Void>() {
public Void call(InternalContext context) {
for (InternalFactory<?> factory : singletonFactories) {
factory.create(context);
}
return null;
}
})
callInContext 创建了一个InternalContext
匿名ContextualCallable的实例调用call(创建的InternalContext)
对单例工厂中的每个工厂,factory.create(context)
本文解析了ContainerBuilder中单例加载的过程,包括通过factories创建ContainerImpl对象,并利用ContextualCallable接口创建上下文环境,最终实现单例工厂中各组件的初始化。
1257

被折叠的 条评论
为什么被折叠?



