一、简介
Guice的运行可以分为三个阶段:install阶段、create injector阶段和运行时阶段。install阶段主要完成加载Module中定义的绑定关系;create injector阶段通过分析install中获得的绑定信息创建一个Injector实例;运行时阶段根据绑定信息响应用户请求。
在install过程结束之后,Module中自定义的绑定信息都由BindingBuilderImpl来表示存储在BinderImpl的bindingBuilders属性中。在接下来的create injector过程中,Guice将会分析每一个BindingBuilderImpl实例,为其创建一个对应的BindingImpl实例并存储到InjectorImpl对象的运行时数据结构Map中去。本篇将只分析绑定一个接口到具体实现类的情况,不包含AOP部分。
二、create injector大致过程
一个绑定接口实现的create injector大致过程如下:
2.1、create injector过程启动
create injector过程的入口是BinderImpl类上的createInjector方法,它实现了整个创建过程的轮廓:
class BinderImpl implements Binder {
Injector createInjector() throws CreationException {
stopwatch.resetAndLog(logger, "Configuration");
Map<Key<?>, BindingImpl<?>> bindings = new HashMap<Key<?>, BindingImpl<?>>();
injector = new InjectorImpl(
proxyFactoryBuilder.create(), bindings, scopes);
...
// 分析bindingBuilders数组,创建BindingImpl
createBindings(preloaders);
...
// 建立索引
injector.index();
...
// 激发FactoryProxy等上的监听器,使其获得实现的InternalFactory
for (CreationListener creationListener : creationListeners) {
creationListener.notify(injector);
}
...
return injector;
}
...
}
2.2、createBindings过程
createBindings过程分析BinderImpl中bindingBuilders属性里每一个BindingBuilderImpl实例中的信息,创建一个BindingImpl对象存入InjectorImpl的Map结构中。
class BinderImpl implements Binder {
private void createBindings(List<ContextualCallable<Void>> preloaders) {
for (BindingBuilderImpl<?> builder : bindingBuilders) {
// 从BindingBuilderImpl得到Key和BindingImpl,并加入injector
createBinding(builder, preloaders);
}
}
// 从BindingBuilderImpl得到Key和BindingImpl,并加入injector
private <T> void createBinding(BindingBuilderImpl<T> builder,
List<ContextualCallable<Void>> preloaders) {
final Key<T> key = builder.getKey();
final InternalFactory<? extends T> factory
= builder.getInternalFactory(injector);
// 生成BindingImpl
BindingImpl<?> binding
= BindingImpl.newInstance(injector, key, builder.getSource(), factory);
// 将Key->BindingImpl对加入injector
putBinding(binding);
....
}
...
}
2.3、InjectorImpl的getInternalFactory过程
getInternalFactory(..)方法会为每个传入的Key返回一个对应的InternalFactory实现,通过该接口可以创建一个所需对象的实例:
class InjectorImpl implements Injector {
<T> InternalFactory<? extends T> getInternalFactory(
final Member member, Key<T> key) {
BindingImpl<T> binding = getBinding(key);
if (binding != null) {
return binding.getInternalFactory();
}
Class<? super T> rawType = key.getTypeLiteral().getRawType();
...
// 对于to原语中直接绑定一个class的,将产生隐式绑定
return (InternalFactory<? extends T>) getImplicitBinding(member, rawType,
null);
}
...
}
getImplicitBinding(..)方法用于得到一个默认绑定,它常常是一个实现接口的类:
class InjectorImpl implements Injector {
<T> InternalFactory<? extends T> getImplicitBinding(Member member,
final Class<T> type, Scope scope) {
...
synchronized (implicitBindings) {
@SuppressWarnings("unchecked")
InternalFactory<T> factory =
(InternalFactory<T>) implicitBindings.get(type);
if (factory != null) {
return factory;
}
// Create the factory. ImplicitBinding可进行依赖注入
ImplicitBinding<T> implicitBinding = new ImplicitBinding<T>(type);
// 挂载scope
...
implicitBindings.put(type, scoped);
//...
ConstructorInjector<T> constructor = getConstructor(type);
implicitBinding.setConstructorInjector(constructor);
//...
return scoped;
}
}
..
}
2.4、InternalFactory实现
InternalFactory实现类的类层次如下:
其中默认绑定ImplicitBinding的实现如下:
class InjectorImpl implements Injector {
static class ImplicitBinding<T> implements InternalFactory<T> {
final Class<T> implementation;
ConstructorInjector<T> constructorInjector;
ImplicitBinding(Class<T> implementation) {
this.implementation = implementation;
}
void setConstructorInjector(
ConstructorInjector<T> constructorInjector) {
this.constructorInjector = constructorInjector;
}
// 生成对象,进行依赖注入
public T get(InternalContext context) {
return (T) constructorInjector.construct(context,
context.getExpectedType());
}
}
...
}