原文地址https://github.com/google/tiger
Description 描述
No description or website provided.
沒有提供描述或者網(wǎng)站
DISCLAIMER 免責(zé)聲明
DISCLAIMER: This is not an official Google product. Google's production dependency injection frameworks are Dagger and Guice.
免責(zé)聲明:這不是Google官方項(xiàng)目.Google的項(xiàng)目依賴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大神的啟發(fā)
Challenge 挑戰(zhàn)
If you find a faster DI framework, let me know. I will drop the title as long as it is proved.
如果你發(fā)現(xiàn)了更快的DI框架,請(qǐng)告訴我茉唉。只要證明了更快勤讽,我將去掉標(biāo)題电爹。
Why Tiger? 為什么用Tiger?
It is the fastest! Not faster, but the fastest! I have tried it on a big project with ~200 modules.
他是最快的!沒有最快,只有更快!我嘗試了擁有兩百個(gè)模塊的大項(xiàng)目。
While it takes hundreds of milliseconds to instantiate Dagger components, on the same hardware,
it only takes a few milliseconds to instantiate Tiger injectors.
在相同硬件的情況下,實(shí)例化Dagger組件需要幾百毫秒桐腌,而實(shí)例化Tigger組件只需要幾毫秒。
Minimal amount of code to write therefore easy to maintain, if you need to maintain it at all.
如果你需要維護(hù)他們苟径,你可以使用最少代碼量寫入案站,使其跟容易維護(hù)。
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.
你不需要為了使用某個(gè)模塊提供的綁定而去把一個(gè)模塊分隔成多個(gè)模塊蟆盐,
You will feel it is some easy to change the scope of a binding.
你會(huì)覺得很容易的去改變綁定的范圍
Just change it. Way to go, isn’t it?
想要改變它,就應(yīng)該這么做遭殉,不是嗎石挂?
Build up your knowledge 積累你的知識(shí)
If you are here, you must already be familiar with DI(Dependency Injection) and its advantage.
如果你讀到這里,你必須熟悉DI(依賴注入)及其優(yōu)勢险污。
Otherwise wiki will be your friend. DI has been evolving for long time in the different form.
不然wiki將會(huì)成為你的朋友痹愚。DI已在不同的結(jié)構(gòu)不斷的發(fā)展了很長一段時(shí)間。
But the concept is not really changed much. This document will not repeat these concepts.
但是他的概念沒有改變多少蛔糯。這個(gè)文檔不會(huì)重復(fù)這些概念拯腮。
If you find some concept not explained, google it. Also Guice probably has explained those concept very well.
如果你找到一些概念沒有解釋,請(qǐng)GOOGLE蚁飒。也許Guice可以更好的解釋這些概念疾瓮。
Integration 集成
Tiger is an annotation process.
Tiger是一個(gè)注釋進(jìn)程。
Therefore just build the jar and use it the way that annotation processors are supposed to be used.
因此在編譯Jar的時(shí)候注釋需要使用到它的方法飒箭。
All environment should work.
所有環(huán)境
The sample uses gradle.
例子使用gradle編譯
How? 怎么樣?
Before diving into details, it will be helpful, very helpful, to understand the intended usage of tiger.
在了解到細(xì)節(jié)之前蜒灰,它會(huì)對(duì)你有益弦蹂,非常的有幫助。讓你去明白Tiger的用途强窖。
Tiger is designed to let the machine do as much as possible and let human do as little as possible.
Tiger是設(shè)計(jì)的讓機(jī)器干更多的活凸椿,讓人盡可能的少干活。
It requires minimal information from the developer.
他只需要從開發(fā)者獲取很少的信息翅溺。
From these information, scoped injectors are generated.
從這些信息里面就可以產(chǎn)生范圍的注入器脑漫。
Application can instances these injectors and use them to inject classes.
應(yīng)用程序可以示例化這些注入器并且使用它們注入到類。
To achieve this, tiger has distilled the information needed to generate injectors.
為了實(shí)現(xiàn)這些咙崎,Tiger需要過濾這些信息來產(chǎn)生注入器优幸。
Here are they, with related annotation.
下面是它們和相關(guān)的注釋
Scopes使用范圍
Usually application has at least one scope, singleton.
通常應(yīng)用程序有一個(gè)作用域,單例褪猛。
Even if there is no scoped binding, it is harmless to have a singleton scope.
即使它沒有作用域綁定网杆,它只是一個(gè)單例的純凈作用域。
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.
在例子里面可以看到詳細(xì)的解釋
Tiger generates one injector class for each scope.
Tiger為每個(gè)作用域生成一個(gè)注入器昼浦。
@tiger.ScopeDependency
It specifies the dependencies between scopes.
他指定了作用域之間的依賴
All the dependency information form a scope tree.
所有的依賴信息形成一個(gè)作用樹
@tiger.Module
It provides binding information through @Provides annotated methods with optional scope.
他通過@Provides注釋方法可選作用域提供綁定信息馍资。
Now(2016/08/10) we just reuse dagger.Module.
現(xiàn)在(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.
注入器需要實(shí)現(xiàn)這些接口
@javax.inject.Inject on fields, methods
It specifies the injection points from which injector fans out when injecting an object.
它指定在向?qū)ο笞⑷霑r(shí)关噪,注入器的注入點(diǎn)鸟蟹。
@tiger.PackageForGenerated
The package for the generated injectors.
用于產(chǎn)生注入器的包。
@tiger.ScopedComponentNames
It specify the names of the injectors.
它提供了注入器的名字
@tiger.GenerationTriggerAnnotation
This the annotation that triggers the generation processor.
這是產(chǎn)生處理器的觸發(fā)器的注釋
@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.
關(guān)于其他的色洞,既@ScopeDependency, @PackageForGenerated, @ScopedComponentNames 和@GenerationTriggerAnnotation戏锹,我們建議把它們放在一個(gè)專門的java文件里面作為應(yīng)用程序的配置信息
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.
這里是怎么樣去實(shí)例化和使用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.
這個(gè)注入器保證了作用域類的綁定被實(shí)例化,一個(gè)作用域內(nèi)最多一個(gè)
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.
應(yīng)用程序需要為作用域?qū)ο髣?chuàng)建相關(guān)的注入器火诸,比如:在android里面锦针,每個(gè)Activity的context一個(gè)作用域注入器,在Application里面需要一個(gè)單例的作用域注入器置蜀。
Tip 貼士
Inspecting the generate code will help you. If you want more, there is source code. Enjoy injection!
檢查生成的代碼有助于你.如果你想要更多奈搜,這里有源代碼。享受注入吧
Group 組
fastesttiger@gmail.com
該轉(zhuǎn)載為了只是為了學(xué)習(xí)技術(shù)和英語所用盯荤,請(qǐng)勿用于其他用途