Android和Java的超輕量級的依賴注入庫:Feather

pcbbe 9年前發布 | 8K 次閱讀 Feather Android開發 移動開發

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.baiduhome.net/lib/view/home/1443350511830

 本文由用戶 pcbbe 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!