Android和Java的超輕量級的依賴注入庫:Feather
Feather是用于Android和Java的超輕量級的依賴注入庫(JSR-330)。它的主要目標是為用戶提供易于使用的基本依賴注入功能具有高性能 - 體積小到了極致。和Google的Guice相比:大小不及3%,啟動快8倍,依賴實例化快40%。
How it works
Feather is based on reflection to inject dependencies. No code generating, classpath scanning, proxying or anything costly involved.
Usage - code examples
Create the injector (Feather)
Feather feather = Feather.with();
Typically an application needs a single Feather instance (the JSR-330 Injector).
Instantiating dependencies
Dependencies having an @Inject constructor or a default constructor will be delivered by Feather without the need for any configuration. Eg:
public class A {
@Inject
public A(B b) {
// ...
}
}
public class B {
@Inject
public B(C c, D d) {
// ...
}
}
public class C {}
@Singleton
public class D {
// something expensive or other reasons for being singleton
}Note: supports @Singleton on classes
Getting an instance of A from Feather.
A instance = feather.instance(A.class);
Note: direct use of Feather should typically be used only for bootstrapping an application
Provide additional dependencies to Feather
When a dependency doesn't have a suitable (@Inject annotated or noarg) constructor , needs custom construction, Feather relies on configuration. This is done by configuration modules:
public class MyModule {
@Provides
@Singleton
DataSource ds() {
DataSource dataSource = // instantiate some DataSource
return dataSource;
}
// ... other @Provides methods
}