Google Tiger
原文地址https://github.com/google/tiger
Description 描述
No description or website provided.
没有提供描述或者网站
DISCLAIMER 免责声明
DISCLAIMER: This is not an official Google product. Google's production dependency injection frameworks are Dagger and Guice.
免责声明:这不是Google官方项目.Google的项目依赖Dagger和Guice注入框架。
Tiger - The fastest java dependency injection framework
Tiger-最快速的java依赖注入框架
Acknowledge 致谢
Tiger is highly inspired by Dagger, Kudos to those great guys.
Tiger灵感来自于Dagger,Kudos大神的启发
Challenge 挑战
If you find a faster DI framework, let me know. I will drop the title as long as it is proved.
如果你发现了更快的DI框架,请告诉我。只要证明了更快,我将去掉标题。
Why Tiger? 为什么用Tiger?
It is the fastest! Not faster, but the fastest! I have tried it on a big project with ~200 modules.
他是最快的!没有最快,只有更快!我尝试了拥有两百个模块的大项目。
While it takes hundreds of milliseconds to instantiate Dagger components, on the same hardware,
it only takes a few milliseconds to instantiate Tiger injectors.
在相同硬件的情况下,实例化Dagger组件需要几百毫秒,而实例化Tigger组件只需要几毫秒。
Minimal amount of code to write therefore easy to maintain, if you need to maintain it at all.
如果你需要维护他们,你可以使用最少代码量写入,使其跟容易维护。
You don't need to write components like in Dagger.
你不需要跟Dagger一样写组件。
You don't need to split a module into several modules one for each scope that used by bindings provided by the module.
你不需要为了使用某个模块提供的绑定而去把一个模块分隔成多个模块,
You will feel it is some easy to change the scope of a binding.
你会觉得很容易的去改变绑定的范围
Just change it. Way to go, isn’t it?
想要改变它,就应该这么做,不是吗?
Build up your knowledge 积累你的知识
If you are here, you must already be familiar with DI(Dependency Injection) and its advantage.
如果你读到这里,你必须熟悉DI(依赖注入)及其优势。
Otherwise wiki will be your friend. DI has been evolving for long time in the different form.
不然wiki将会成为你的朋友。DI已在不同的结构不断的发展了很长一段时间。
But the concept is not really changed much. This document will not repeat these concepts.
但是他的概念没有改变多少。这个文档不会重复这些概念。
If you find some concept not explained, google it. Also Guice probably has explained those concept very well.
如果你找到一些概念没有解释,请GOOGLE。也许Guice可以更好的解释这些概念。
Integration 集成
Tiger is an annotation process.
Tiger是一个注释进程。
Therefore just build the jar and use it the way that annotation processors are supposed to be used.
因此在编译Jar的时候注释需要使用到它的方法。
All environment should work.
所有环境
The sample uses gradle.
例子使用gradle编译
How? 怎么样?
Before diving into details, it will be helpful, very helpful, to understand the intended usage of tiger.
在了解到细节之前,它会对你有益,非常的有帮助。让你去明白Tiger的用途。
Tiger is designed to let the machine do as much as possible and let human do as little as possible.
Tiger是设计的让机器干更多的活,让人尽可能的少干活。
It requires minimal information from the developer.
他只需要从开发者获取很少的信息。
From these information, scoped injectors are generated.
从这些信息里面就可以产生范围的注入器。
Application can instances these injectors and use them to inject classes.
应用程序可以示例化这些注入器并且使用它们注入到类。
To achieve this, tiger has distilled the information needed to generate injectors.
为了实现这些,Tiger需要过滤这些信息来产生注入器。
Here are they, with related annotation.
下面是它们和相关的注释
Scopes使用范围
Usually application has at least one scope, singleton.
通常应用程序有一个作用域,单例。
Even if there is no scoped binding, it is harmless to have a singleton scope.
即使它没有作用域绑定,它只是一个单例的纯净作用域。
Therefore, tiger requires there always be a scope tree.
因此,Tiger需要有一些作用域的树。
The root is usually singleton, but not necessary.
根一般是单例,但是不是必须的。
Details will be shown later in the sample.
在例子里面可以看到详细的解释
Tiger generates one injector class for each scope.
Tiger为每个作用域生成一个注入器。
@tiger.ScopeDependency
It specifies the dependencies between scopes.
他指定了作用域之间的依赖
All the dependency information form a scope tree.
所有的依赖信息形成一个作用树
@tiger.Module
It provides binding information through @Provides annotated methods with optional scope.
他通过@Provides注释方法可选作用域提供绑定信息。
Now(2016/08/10) we just reuse dagger.Module.
现在(2016/08/10) 我们只是重用了Dagger.Module.
In future, dagger.Module will be copied into tiger.Module so that tiger does not need to depend on dagger.
未来,将把Dagger.Module将拷贝到Tiger.Module,使其不需要依赖Dagger.
@javax.inject.Inject on ctor
It provides binding information with optional scope.
它通过可选作用域提供绑定信息
@tiger.MembersInjector with mandatory scope
It specifies interfaces which includes class that has fields or methods to be injected.
它指定类的字段或者方法需要注入的接口
Injectors will implement all these interfaces.
注入器需要实现这些接口
@javax.inject.Inject on fields, methods
It specifies the injection points from which injector fans out when injecting an object.
它指定在向对象注入时,注入器的注入点。
@tiger.PackageForGenerated
The package for the generated injectors.
用于产生注入器的包。
@tiger.ScopedComponentNames
It specify the names of the injectors.
它提供了注入器的名字
@tiger.GenerationTriggerAnnotation
This the annotation that triggers the generation processor.
这是产生处理器的触发器的注释
@Module, @Inject and @MembersInjector are naturally scattered around the code.
For the others, i.e., @ScopeDependency, @PackageForGenerated, @ScopedComponentNames and @GenerationTriggerAnnotation, we suggest to put them into a dedicated java file as the central configuration information for the app.
关于其他的,既@ScopeDependency, @PackageForGenerated, @ScopedComponentNames 和@GenerationTriggerAnnotation,我们建议把它们放在一个专门的java文件里面作为应用程序的配置信息
Here is the depicted code the sample(with some modification)
这里是一些代码端的例子(和一些改变)
@GenerationTriggerAnnotation
@PackageForGenerated("sample")
public class Scopes {
@ScopedComponentNames(scope = Singleton.class, name = "ApplicationInjector" )
public static class ForApplication {}
@ScopedComponentNames(scope = sample.ActivityScoped.class, name = "ActivityInjector" )
@ScopeDependency(scope = sample.ActivityScoped.class, parent = Singleton.class)
public static class ForActivityScoped {}
}
Here is how ApplicationInjector is instantiated and used.
这里是怎么样去实例化和使用ApplicationInjector
ApplicationInjector applicationInjector = new ApplicationInjector.Builder().build();
PseudoApplication application = new PseudoApplication();
applicationInjector.injectPseudoApplication(application);
Here is how ActivityInjector is instantiated and used.
ActivityInjector activityInjector = new ActivityInjector.Builder()
.applicationInjector(applicationInjector)
.build();
activityInjector.injectPseudoActivity(this);
The injectors guarantee that scoped bindings will be instantiated at most once within a scope.
这个注入器保证了作用域类的绑定被实例化,一个作用域内最多一个
The application needs to create related injectors for scope objects, e.g., in android, a context scoped injector for each Activity, a singleton scoped injector for the Application.
应用程序需要为作用域对象创建相关的注入器,比如:在android里面,每个Activity的context一个作用域注入器,在Application里面需要一个单例的作用域注入器。
Tip 贴士
Inspecting the generate code will help you. If you want more, there is source code. Enjoy injection!
检查生成的代码有助于你.如果你想要更多,这里有源代码。享受注入吧
Group 组
fastesttiger@gmail.com
该转载为了只是为了学习技术和英语所用,请勿用于其他用途