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(八)—— 可空注入实现 #53

Open
soapgu opened this issue Jun 9, 2021 · 0 comments
Open

Android的IoC(八)—— 可空注入实现 #53

soapgu opened this issue Jun 9, 2021 · 0 comments
Labels
IoC New feature or request 安卓 安卓

Comments

@soapgu
Copy link
Owner

soapgu commented Jun 9, 2021

  • 前言

  • 通过构造函数的@Inject标记或者@provides标记可以实现单个对象的注入(1)

  • 通过@Intomap及@IntoSet标记可以实现多个对象的批量注入(1-n)

  • 怎么实现可空对象的注入那,就是我获取的注入对象,可能提供了,可能也没提供。这是一个动态值,多用了插件组件模式上的。(0-1)?

目前如果声明一个@Inject标记,不设置导出的话
会有如下报错cannot be provided without an @Inject constructor or an @Provides-annotated method,怎么实现可空导入对象那?

  • Optional bindings

经过一番探索,总算在dagger的手册里面找到我想要的特性
@BindsOptionalOf其实是和@BINDS对应的。
一直以来用了@provides就没有使用Binds的必要了
Sample代码

public interface AnalyticsService {
  void analyticsMethods();
}

// Constructor-injected, because Hilt needs to know how to
// provide instances of AnalyticsServiceImpl, too.
public class AnalyticsServiceImpl implements AnalyticsService {
  ...
  @Inject
  AnalyticsServiceImpl(...) {
    ...
  }
}

@Module
@InstallIn(ActivityComponent.class)
public abstract class AnalyticsModule {

  @Binds
  public abstract AnalyticsService bindAnalyticsService(
    AnalyticsServiceImpl analyticsServiceImpl
  );
}

@BINDS标记的抽象函数需要指定具体类的实现。所以其实用@provides更加容易理解。

  • 代码实现

首先需要定义抽象类,并声明抽象函数

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

加上@BindsOptionalOf,声明ILog 类型就是需要注入的类型。如需像@BINDS指定输出

接下来是调用部分

@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {
    @Inject
    Provider<Optional<ILog>> log;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    Optional<ILog> logSet = log.get();
        if(  logSet.isPresent() ) {
            Logger.i("Print from log:%s", logSet.get().toString());
        }
        else {
            Logger.i( "log is not export" );
        }
   }
}

使用部分一定要使用泛型对象Optional

运行结果如下

2021-06-09 14:00:29.314 2555-2555/com.soapgu.hellohilt I/HelloHilt: ┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────
2021-06-09 14:00:29.314 2555-2555/com.soapgu.hellohilt I/HelloHilt: │ Thread: main
2021-06-09 14:00:29.314 2555-2555/com.soapgu.hellohilt I/HelloHilt: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
2021-06-09 14:00:29.314 2555-2555/com.soapgu.hellohilt I/HelloHilt: │ Activity.performCreate  (Activity.java:6709)
2021-06-09 14:00:29.314 2555-2555/com.soapgu.hellohilt I/HelloHilt: │    MainActivity.onCreate  (MainActivity.java:58)
2021-06-09 14:00:29.314 2555-2555/com.soapgu.hellohilt I/HelloHilt: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
2021-06-09 14:00:29.314 2555-2555/com.soapgu.hellohilt I/HelloHilt: │ log is not export
2021-06-09 14:00:29.314 2555-2555/com.soapgu.hellohilt I/HelloHilt: └────────────────────────────────────────────────────────────────────────────────────────────────────────────────

看来程序已经接受空Inject了

好了,接下来我们再提供下输出

@Module
@InstallIn(ActivityComponent.class)
public class SimpleModule {
   
    @Provides
    public ILog provideILog(Lazy<SimpleExport> simpleExport ){
        return new ConsoleLog(simpleExport);
    }
}

我在另外的模块实现ILog 的导出
运行日志如下

2021-06-09 15:37:52.776 2708-2708/com.soapgu.hellohilt I/HelloHilt: ┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────
2021-06-09 15:37:52.776 2708-2708/com.soapgu.hellohilt I/HelloHilt: │ Thread: main
2021-06-09 15:37:52.777 2708-2708/com.soapgu.hellohilt I/HelloHilt: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
2021-06-09 15:37:52.777 2708-2708/com.soapgu.hellohilt I/HelloHilt: │ Activity.performCreate  (Activity.java:6709)
2021-06-09 15:37:52.777 2708-2708/com.soapgu.hellohilt I/HelloHilt: │    MainActivity.onCreate  (MainActivity.java:55)
2021-06-09 15:37:52.778 2708-2708/com.soapgu.hellohilt I/HelloHilt: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
2021-06-09 15:37:52.778 2708-2708/com.soapgu.hellohilt I/HelloHilt: │ Print from log:com.soapgu.hellohilt.ConsoleLog@8b01931
2021-06-09 15:37:52.778 2708-2708/com.soapgu.hellohilt I/HelloHilt: └────────────────────────────────────────────────────────────────────────────────────────────────────────────────

完美

  • 遗憾

现在@BindsOptionalOf只支持(0-1),暂时对(0-n)还没有直接的解决方案

  • 链接

仓库

参考

@soapgu soapgu added IoC New feature or request 安卓 安卓 labels Jun 9, 2021
@soapgu soapgu changed the title Android的IOC(七)—— 可空注入实现 Android的IOC(八)—— 可空注入实现 Jun 11, 2021
@soapgu soapgu changed the title Android的IOC(八)—— 可空注入实现 Android的IoC(八)—— 可空注入实现 Jul 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IoC New feature or request 安卓 安卓
Projects
None yet
Development

No branches or pull requests

1 participant