一、简介
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());
}
}
...
}
- 大小: 8.9 KB
- 大小: 6.8 KB
分享到:
相关推荐
赠送源代码:guice-assistedinject-3.0-sources.jar; 赠送Maven依赖信息文件:guice-assistedinject-3.0.pom; 包含翻译后的API文档:guice-assistedinject-3.0-javadoc-API文档-中文(简体)-英语-对照版.zip; ...
赠送源代码:guice-multibindings-3.0-sources.jar; 赠送Maven依赖信息文件:guice-multibindings-3.0.pom; 包含翻译后的API文档:guice-multibindings-3.0-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven...
赠送源代码:guice-multibindings-3.0-sources.jar; 赠送Maven依赖信息文件:guice-multibindings-3.0.pom; 包含翻译后的API文档:guice-multibindings-3.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:...
赠送源代码:guice-assistedinject-3.0-sources.jar; 赠送Maven依赖信息文件:guice-assistedinject-3.0.pom; 包含翻译后的API文档:guice-assistedinject-3.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:...
赠送源代码:guice-servlet-4.0-sources.jar; 赠送Maven依赖信息文件:guice-servlet-4.0.pom; 包含翻译后的API文档:guice-servlet-4.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:...
赠送源代码:guice-servlet-4.0-sources.jar; 赠送Maven依赖信息文件:guice-servlet-4.0.pom; 包含翻译后的API文档:guice-servlet-4.0-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:...
赠送源代码:guice-4.0-sources.jar; 赠送Maven依赖信息文件:guice-4.0.pom; 包含翻译后的API文档:guice-4.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:com.google.inject:guice:4.0; 标签:google、...
赠送源代码:guice-4.0-sources.jar; 赠送Maven依赖信息文件:guice-4.0.pom; 包含翻译后的API文档:guice-4.0-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:com.google.inject:guice:4.0; 标签:...
赠送源代码:guice-3.0-sources.jar; 赠送Maven依赖信息文件:guice-3.0.pom; 包含翻译后的API文档:guice-3.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:com.google.inject:guice:3.0; 标签:google、...
赠送源代码:guice-3.0-sources.jar; 赠送Maven依赖信息文件:guice-3.0.pom; 包含翻译后的API文档:guice-3.0-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:com.google.inject:guice:3.0; 标签:...
guice-bridge-jit-injector 让 Guice 与 Jersey-HK2 顺利工作 如果在Jersey 2中使用 Guice,您几乎肯定需要将 Guice 组件注入 Jersey 组件。 与常规 Guice 不同,注入 Jersey 的任何 Guice 组件都需要在 Guice 配置...
赠送源代码:shiro-guice-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-guice-1.4.0.pom; 包含翻译后的API文档:shiro-guice-1.4.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache.shiro:shiro-...
赠送源代码:jersey-guice-1.19-sources.jar; 赠送Maven依赖信息文件:jersey-guice-1.19.pom; 包含翻译后的API文档:jersey-guice-1.19-javadoc-API文档-中文(简体)版.zip; Maven坐标:...
gaelyk-guice-plugin-0.1-sources.jar
atmosphere-guice-chat-1.0.16-sources.jar
atmosphere-guice-chat-1.0.14-sources.jar
atmosphere-guice-chat-1.0.13-sources.jar
atmosphere-guice-chat-1.0.6-sources.jar
atmosphere-guice-chat-1.0.5-sources.jar
atmosphere-guice-chat-1.0.4-sources.jar