Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android的IoC(九)—— 可空注入实现补充(0或n) #73

Open
soapgu opened this issue Aug 23, 2021 · 0 comments
Open

Android的IoC(九)—— 可空注入实现补充(0或n) #73

soapgu opened this issue Aug 23, 2021 · 0 comments
Labels
Demo Demo IoC New feature or request 安卓 安卓

Comments

@soapgu
Copy link
Owner

soapgu commented Aug 23, 2021

  • 前言

Android的IoC(八)—— 可空注入实现里面
留下了下面的遗憾
现在@BindsOptionalOf只支持(0-1),暂时对(0-n)还没有直接的解决方案。

翻了很多都没翻到解决方案。最后只能搁置。
偶然机会,这个问题终于有了解。
真是踏破铁鞋无觅处,得来全不费工夫

  • Key Point:@Multibinds

You can declare that a multibound set or map is bound by adding an abstract @Multibinds-annotated method to a module that returns the set or map you want to declare.

You do not have to use @Multibinds for sets or maps that have at least one @IntoSet, @ElementsIntoSet, or @Intomap binding, but you do have to declare them if they may be empty.

迫不及待要实现一下

public interface INullableItem {
    /**
     * 获取对应名称
     * @return name
     */
    String getName();
}

@Module
@InstallIn(ActivityComponent.class)
public abstract class BindModule {
    @BindsOptionalOf
    public abstract ILog bindILog();

    /**
     * 声明可空集合
     * @return 空集合对象
     */
    @Multibinds
    public abstract Set<INullableItem> setNullableItems();
}

声明部分,只要定义好@Multibinds标记即可

@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {

    @Inject
    Set<INullableItem> nullableItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if( nullableItems != null && !nullableItems.isEmpty() ){
            nullableItems.forEach( item -> Logger.i( "export INullableItem name:%s", item.getName() ) );
        }
        else{
            Logger.i( "this collection is empty!" );
        }
   }
}

调用部分,和平时用的集合导入一样用法,Optional是不需要了

运行结果

2021-08-23 22:36:09.003 3589-3589/com.soapgu.hellohilt I/HelloHilt: ┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────
2021-08-23 22:36:09.003 3589-3589/com.soapgu.hellohilt I/HelloHilt: │ Thread: main
2021-08-23 22:36:09.003 3589-3589/com.soapgu.hellohilt I/HelloHilt: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
2021-08-23 22:36:09.003 3589-3589/com.soapgu.hellohilt I/HelloHilt: │ Activity.performCreate  (Activity.java:6679)
2021-08-23 22:36:09.003 3589-3589/com.soapgu.hellohilt I/HelloHilt: │    MainActivity.onCreate  (MainActivity.java:68)
2021-08-23 22:36:09.003 3589-3589/com.soapgu.hellohilt I/HelloHilt: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
2021-08-23 22:36:09.003 3589-3589/com.soapgu.hellohilt I/HelloHilt: │ this collection is empty!
2021-08-23 22:36:09.003 3589-3589/com.soapgu.hellohilt I/HelloHilt: └────────────────────────────────────────────────────────────────────────────────────────────────────────────────

符合预期,那么0部分已经是实现了

n也要实现下,接下来导入一些对象看看

@Module
@InstallIn(ActivityComponent.class)
public class SimpleModule {
    @IntoSet
    @Provides
    public INullableItem provideFirst(){
        return () -> "---first nullable item---";
    }

    @IntoSet
    @Provides
    public INullableItem provideSecond(){
        return () -> "---second nullable item---";
    }
}

这部分的用法和一般的@IntoSet用法一样

运行结果

2021-08-23 22:47:00.313 3947-3947/com.soapgu.hellohilt I/HelloHilt: ┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────
2021-08-23 22:47:00.314 3947-3947/com.soapgu.hellohilt I/HelloHilt: │ Thread: main
2021-08-23 22:47:00.314 3947-3947/com.soapgu.hellohilt I/HelloHilt: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
2021-08-23 22:47:00.314 3947-3947/com.soapgu.hellohilt I/HelloHilt: │ -$$Lambda$MainActivity$WmS_dJtN855cLjTu7Q9bQYAYoGo.accept  (lambda:-1)
2021-08-23 22:47:00.314 3947-3947/com.soapgu.hellohilt I/HelloHilt: │    MainActivity.lambda$onCreate$0  (MainActivity.java:65)
2021-08-23 22:47:00.314 3947-3947/com.soapgu.hellohilt I/HelloHilt: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
2021-08-23 22:47:00.314 3947-3947/com.soapgu.hellohilt I/HelloHilt: │ export INullableItem name:---first nullable item---
2021-08-23 22:47:00.314 3947-3947/com.soapgu.hellohilt I/HelloHilt: └────────────────────────────────────────────────────────────────────────────────────────────────────────────────
2021-08-23 22:47:00.314 3947-3947/com.soapgu.hellohilt I/HelloHilt: ┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────
2021-08-23 22:47:00.314 3947-3947/com.soapgu.hellohilt I/HelloHilt: │ Thread: main
2021-08-23 22:47:00.314 3947-3947/com.soapgu.hellohilt I/HelloHilt: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
2021-08-23 22:47:00.314 3947-3947/com.soapgu.hellohilt I/HelloHilt: │ -$$Lambda$MainActivity$WmS_dJtN855cLjTu7Q9bQYAYoGo.accept  (lambda:-1)
2021-08-23 22:47:00.314 3947-3947/com.soapgu.hellohilt I/HelloHilt: │    MainActivity.lambda$onCreate$0  (MainActivity.java:65)
2021-08-23 22:47:00.314 3947-3947/com.soapgu.hellohilt I/HelloHilt: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
2021-08-23 22:47:00.314 3947-3947/com.soapgu.hellohilt I/HelloHilt: │ export INullableItem name:---second nullable item---
2021-08-23 22:47:00.314 3947-3947/com.soapgu.hellohilt I/HelloHilt: └────────────────────────────────────────────────────────────────────────────────────────────────────────────────

非常完美收工

  • 结论

@Multibinds可以完美实现[0-n]的多重绑定逻辑,完善了IoC的特性。
其实相关资料就在multibindings这个章节里面,我以前只是看了上半部分,@IntoSet @Intomap @stringkey看完基本上收工了。后面的部分没耐性看完。
是的又犯下了“灯下黑”的错误
图片

Demo 仓库

参考链接

@soapgu soapgu added Demo Demo IoC New feature or request 安卓 安卓 labels Aug 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Demo Demo IoC New feature or request 安卓 安卓
Projects
None yet
Development

No branches or pull requests

1 participant