Android和Java的超轻量级的依赖注入库:Feather

pcbbe 9年前

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  }

项目主页:http://www.open-open.com/lib/view/home/1443350511830