了解Android开发的Dagger2

了解Android开发的Dagger2,android,dependency-injection,dagger-2,Android,Dependency Injection,Dagger 2,这是我的代码,我是根据互联网上的一些旧教程编写的。Dagger2的主站点上应该有一些例子,我发现很难理解如何实现这一切 要运行这样一个简单的应用程序真的需要做很多工作。我有两个问题: 我需要在每个类中调用DaggerLoggerComponent吗?我想得到一些组件,比如我的Logger类 另外,如何使记录器类的作用域成为单例?现在,每次单击按钮都会创建一个新的记录器实例 可能我不理解一些基本概念,我以前只在Spring中使用过依赖注入,所有这些对我来说都很奇怪 public class Mai

这是我的代码,我是根据互联网上的一些旧教程编写的。Dagger2的主站点上应该有一些例子,我发现很难理解如何实现这一切

要运行这样一个简单的应用程序真的需要做很多工作。我有两个问题:

我需要在每个类中调用DaggerLoggerComponent吗?我想得到一些组件,比如我的Logger类

另外,如何使记录器类的作用域成为单例?现在,每次单击按钮都会创建一个新的记录器实例

可能我不理解一些基本概念,我以前只在Spring中使用过依赖注入,所有这些对我来说都很奇怪

public class MainActivity extends AppCompatActivity {

    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init(){
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoggerComponent component = DaggerLoggerComponent.builder().loggerModule(new LoggerModule()).build();
                component.getLogger().log("Hello!",MainActivity.this);
            }
        });
    }

}


public class Logger {

    private static int i = 0;

    public Logger(){
        i++;
    }

    public static int getI() {
        return i;
    }

    public void log(String text, Context context){
        Toast.makeText(context,text+" "+i,Toast.LENGTH_SHORT).show();
    }
}


@Singleton
@Component(modules={LoggerModule.class})
public interface LoggerComponent {

    Logger getLogger();

}


@Module
public class LoggerModule {
    @Provides
    @Singleton
    Logger provideLogger(){
        return new Logger();
    }
}
我需要在每个类中调用DaggerLoggerComponent吗?我想得到一些组件,比如我的Logger类

是,适用于系统创建的所有类,如应用程序、活动和服务。但是对于你自己的课程,你不需要这些。只需使用@inject注释构造函数,dagger就会提供依赖项

另外,如何使记录器类的作用域成为单例?赖特 现在,每次单击按钮都会创建一个新的记录器实例

您的singleton设置是正确的。但您必须在创建活动(onCreate)后初始化组件一次,以便让dagger注入所有字段。如果不需要立即使用Logger对象,也可以利用惰性注入特性

    @Inject
    Logger logger;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LoggerComponent component = DaggerLoggerComponent.builder().loggerModule(new LoggerModule()).build();

        component.inject(this);

        init();
    }
然后,您可以访问对象,而无需从组件获取引用:

private void init(){
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logger.log("Hello!",MainActivity.this);
            }
        });
    }
总之: 您必须在所有使用字段注入的类中初始化组件

更新: 要进行实际的注入,必须在组件中声明inject()方法,dagger将自动实现它。此方法将负责提供任何带有@Inject注释的对象

我需要在每个类中调用DaggerLoggerComponent吗?我想得到一些组件,比如我的Logger类

是,适用于系统创建的所有类,如应用程序、活动和服务。但是对于你自己的课程,你不需要这些。只需使用@inject注释构造函数,dagger就会提供依赖项

另外,如何使记录器类的作用域成为单例?赖特 现在,每次单击按钮都会创建一个新的记录器实例

您的singleton设置是正确的。但您必须在创建活动(onCreate)后初始化组件一次,以便让dagger注入所有字段。如果不需要立即使用Logger对象,也可以利用惰性注入特性

    @Inject
    Logger logger;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LoggerComponent component = DaggerLoggerComponent.builder().loggerModule(new LoggerModule()).build();

        component.inject(this);

        init();
    }
然后,您可以访问对象,而无需从组件获取引用:

private void init(){
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logger.log("Hello!",MainActivity.this);
            }
        });
    }
总之: 您必须在所有使用字段注入的类中初始化组件

更新: 要进行实际的注入,必须在组件中声明inject()方法,dagger将自动实现它。此方法将负责提供任何带有@Inject注释的对象

我需要在每个类中调用DaggerLoggerComponent吗?我想得到一些组件,比如我的Logger类

是,适用于系统创建的所有类,如应用程序、活动和服务。但是对于你自己的课程,你不需要这些。只需使用@inject注释构造函数,dagger就会提供依赖项

另外,如何使记录器类的作用域成为单例?赖特 现在,每次单击按钮都会创建一个新的记录器实例

您的singleton设置是正确的。但您必须在创建活动(onCreate)后初始化组件一次,以便让dagger注入所有字段。如果不需要立即使用Logger对象,也可以利用惰性注入特性

    @Inject
    Logger logger;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LoggerComponent component = DaggerLoggerComponent.builder().loggerModule(new LoggerModule()).build();

        component.inject(this);

        init();
    }
然后,您可以访问对象,而无需从组件获取引用:

private void init(){
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logger.log("Hello!",MainActivity.this);
            }
        });
    }
总之: 您必须在所有使用字段注入的类中初始化组件

更新: 要进行实际的注入,必须在组件中声明inject()方法,dagger将自动实现它。此方法将负责提供任何带有@Inject注释的对象

我需要在每个类中调用DaggerLoggerComponent吗?我想得到一些组件,比如我的Logger类

是,适用于系统创建的所有类,如应用程序、活动和服务。但是对于你自己的课程,你不需要这些。只需使用@inject注释构造函数,dagger就会提供依赖项

另外,如何使记录器类的作用域成为单例?赖特 现在,每次单击按钮都会创建一个新的记录器实例

您的singleton设置是正确的。但您必须在创建活动(onCreate)后初始化组件一次,以便让dagger注入所有字段。如果不需要立即使用Logger对象,也可以利用惰性注入特性

    @Inject
    Logger logger;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LoggerComponent component = DaggerLoggerComponent.builder().loggerModule(new LoggerModule()).build();

        component.inject(this);

        init();
    }
然后,您可以访问对象,而无需从组件获取引用:

private void init(){
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logger.log("Hello!",MainActivity.this);
            }
        });
    }
总之: 您必须在所有使用字段注入的类中初始化组件

更新: 要进行实际的注入,必须在组件中声明inject()方法,dagger将自动实现它。此方法将负责提供任何带有@Inject注释的对象。

答案是

public class MainActivity extends AppCompatActivity {
    @OnClick(R.id.button) //ButterKnife
    public void onClickButton() {
        logger.log("Hello!");
    }

    @Inject
    Logger logger;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Injector.INSTANCE.getApplicationComponent().inject(this);
        ButterKnife.bind(this);
    }

    @Override
    protected void onDestroy() {
        ButterKnife.unbind(this);
        super.onDestroy();
    }
}

public class Logger {
    private static int i = 0;

    private CustomApplication customApplication;

    public Logger(CustomApplication application) {
        this.customApplication = application;
        i++;
    }

    public static int getI() {
        return i;
    }

    public void log(String text){
        Toast.makeText(customApplication, text + " " + i,Toast.LENGTH_SHORT).show();
    }
}


public interface LoggerComponent {
    Logger logger();
}

@Module
public class ApplicationModule {
    private CustomApplication customApplication;

    public ApplicationModule(CustomApplication customApplication) {
        this.customApplication = customApplication;
    }

    @Provides
    public CustomApplication customApplication() {
        return customApplication;
    }
}

@Module
public class LoggerModule {
    @Provides
    @Singleton
    Logger provideLogger(){
        return new Logger();
    }
}


@Singleton
@Component(modules={LoggerModule.class, ApplicationModule.class})
public interface ApplicationComponent extends LoggerComponent {
    CustomApplication customApplication();

    void inject(MainActivity mainActivity);
}

public class CustomApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Injector.INSTANCE.initializeApplicationComponent(this);
    }
}

public enum Injector {
    INSTANCE;

    private ApplicationComponent applicationComponent;

    public ApplicationComponent getApplicationComponent() {
        return applicationComponent;
    }

    void initializeApplicationComponent(CustomApplication customApplication) {
        this.applicationComponent = DaggerApplicationComponent.builder()
            .applicationModule(new ApplicationModule(customApplication))
            .build();
    }
}

编辑:这是从我们正在制作的应用程序中改装东西的实际代码:

public interface RecordingService {    
    ScheduledRecordsXML getScheduledRecords(long userId)
            throws ServerErrorException;
}

public class RecordingServiceImpl
        implements RecordingService {

    private static final String TAG = RecordingServiceImpl.class.getSimpleName();

    private RetrofitRecordingService retrofitRecordingService;

    public RecordingServiceImpl(RetrofitRecordingService retrofitRecordingService) {
        this.retrofitRecordingService = retrofitRecordingService;
    }

    @Override
    public ScheduledRecordsXML getScheduledRecords(long userId)
            throws ServerErrorException {
        try {
            return retrofitRecordingService.getScheduledPrograms(String.valueOf(userId));
        } catch(RetrofitError retrofitError) {
            Log.e(TAG, "Error occurred in downloading XML file.", retrofitError);
            throw new ServerErrorException(retrofitError);
        }
    }
}

@Module
public class NetworkClientModule {
    @Provides
    @Singleton
    public OkHttpClient okHttpClient() {
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.interceptors().add(new HeaderInterceptor());
        return okHttpClient;
    }
}

@Module(includes = {NetworkClientModule.class})
public class ServiceModule {
    @Provides
    @Singleton
    public RecordingService recordingService(OkHttpClient okHttpClient, Persister persister, AppConfig appConfig) {
        return new RecordingServiceImpl(
                new RestAdapter.Builder().setEndpoint(appConfig.getServerEndpoint())
                        .setConverter(new SimpleXMLConverter(persister))
                        .setClient(new OkClient(okHttpClient))
                        .setLogLevel(RestAdapter.LogLevel.NONE)
                        .build()
                        .create(RetrofitRecordingService.class));
    }

    //...
}

public interface RetrofitRecordingService {
    @GET("/getScheduledPrograms")
    ScheduledRecordsXML getScheduledPrograms(@Query("UserID") String userId);
}

public interface ServiceComponent {
    RecordingService RecordingService();

    //...
}

public interface AppDomainComponent
        extends InteractorComponent, ServiceComponent, ManagerComponent, ParserComponent {
}

@Singleton
@Component(modules = {
        //...
        InteractorModule.class,
        ManagerModule.class,
        ServiceModule.class,
        ParserModule.class
    //...
})
public interface ApplicationComponent
        extends AppContextComponent, AppDataComponent, AppDomainComponent, AppUtilsComponent, AppPresentationComponent {
    void inject(DashboardActivity dashboardActivity);
    //...
}
答案是

public class MainActivity extends AppCompatActivity {
    @OnClick(R.id.button) //ButterKnife
    public void onClickButton() {
        logger.log("Hello!");
    }

    @Inject
    Logger logger;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Injector.INSTANCE.getApplicationComponent().inject(this);
        ButterKnife.bind(this);
    }

    @Override
    protected void onDestroy() {
        ButterKnife.unbind(this);
        super.onDestroy();
    }
}

public class Logger {
    private static int i = 0;

    private CustomApplication customApplication;

    public Logger(CustomApplication application) {
        this.customApplication = application;
        i++;
    }

    public static int getI() {
        return i;
    }

    public void log(String text){
        Toast.makeText(customApplication, text + " " + i,Toast.LENGTH_SHORT).show();
    }
}


public interface LoggerComponent {
    Logger logger();
}

@Module
public class ApplicationModule {
    private CustomApplication customApplication;

    public ApplicationModule(CustomApplication customApplication) {
        this.customApplication = customApplication;
    }

    @Provides
    public CustomApplication customApplication() {
        return customApplication;
    }
}

@Module
public class LoggerModule {
    @Provides
    @Singleton
    Logger provideLogger(){
        return new Logger();
    }
}


@Singleton
@Component(modules={LoggerModule.class, ApplicationModule.class})
public interface ApplicationComponent extends LoggerComponent {
    CustomApplication customApplication();

    void inject(MainActivity mainActivity);
}

public class CustomApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Injector.INSTANCE.initializeApplicationComponent(this);
    }
}

public enum Injector {
    INSTANCE;

    private ApplicationComponent applicationComponent;

    public ApplicationComponent getApplicationComponent() {
        return applicationComponent;
    }

    void initializeApplicationComponent(CustomApplication customApplication) {
        this.applicationComponent = DaggerApplicationComponent.builder()
            .applicationModule(new ApplicationModule(customApplication))
            .build();
    }
}

编辑:这是从我们正在制作的应用程序中改装东西的实际代码:

public interface RecordingService {    
    ScheduledRecordsXML getScheduledRecords(long userId)
            throws ServerErrorException;
}

public class RecordingServiceImpl
        implements RecordingService {

    private static final String TAG = RecordingServiceImpl.class.getSimpleName();

    private RetrofitRecordingService retrofitRecordingService;

    public RecordingServiceImpl(RetrofitRecordingService retrofitRecordingService) {
        this.retrofitRecordingService = retrofitRecordingService;
    }

    @Override
    public ScheduledRecordsXML getScheduledRecords(long userId)
            throws ServerErrorException {
        try {
            return retrofitRecordingService.getScheduledPrograms(String.valueOf(userId));
        } catch(RetrofitError retrofitError) {
            Log.e(TAG, "Error occurred in downloading XML file.", retrofitError);
            throw new ServerErrorException(retrofitError);
        }
    }
}

@Module
public class NetworkClientModule {
    @Provides
    @Singleton
    public OkHttpClient okHttpClient() {
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.interceptors().add(new HeaderInterceptor());
        return okHttpClient;
    }
}

@Module(includes = {NetworkClientModule.class})
public class ServiceModule {
    @Provides
    @Singleton
    public RecordingService recordingService(OkHttpClient okHttpClient, Persister persister, AppConfig appConfig) {
        return new RecordingServiceImpl(
                new RestAdapter.Builder().setEndpoint(appConfig.getServerEndpoint())
                        .setConverter(new SimpleXMLConverter(persister))
                        .setClient(new OkClient(okHttpClient))
                        .setLogLevel(RestAdapter.LogLevel.NONE)
                        .build()
                        .create(RetrofitRecordingService.class));
    }

    //...
}

public interface RetrofitRecordingService {
    @GET("/getScheduledPrograms")
    ScheduledRecordsXML getScheduledPrograms(@Query("UserID") String userId);
}

public interface ServiceComponent {
    RecordingService RecordingService();

    //...
}

public interface AppDomainComponent
        extends InteractorComponent, ServiceComponent, ManagerComponent, ParserComponent {
}

@Singleton
@Component(modules = {
        //...
        InteractorModule.class,
        ManagerModule.class,
        ServiceModule.class,
        ParserModule.class
    //...
})
public interface ApplicationComponent
        extends AppContextComponent, AppDataComponent, AppDomainComponent, AppUtilsComponent, AppPresentationComponent {
    void inject(DashboardActivity dashboardActivity);
    //...
}
答案是

public class MainActivity extends AppCompatActivity {
    @OnClick(R.id.button) //ButterKnife
    public void onClickButton() {
        logger.log("Hello!");
    }

    @Inject
    Logger logger;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Injector.INSTANCE.getApplicationComponent().inject(this);
        ButterKnife.bind(this);
    }

    @Override
    protected void onDestroy() {
        ButterKnife.unbind(this);
        super.onDestroy();
    }
}

public class Logger {
    private static int i = 0;

    private CustomApplication customApplication;

    public Logger(CustomApplication application) {
        this.customApplication = application;
        i++;
    }

    public static int getI() {
        return i;
    }

    public void log(String text){
        Toast.makeText(customApplication, text + " " + i,Toast.LENGTH_SHORT).show();
    }
}


public interface LoggerComponent {
    Logger logger();
}

@Module
public class ApplicationModule {
    private CustomApplication customApplication;

    public ApplicationModule(CustomApplication customApplication) {
        this.customApplication = customApplication;
    }

    @Provides
    public CustomApplication customApplication() {
        return customApplication;
    }
}

@Module
public class LoggerModule {
    @Provides
    @Singleton
    Logger provideLogger(){
        return new Logger();
    }
}


@Singleton
@Component(modules={LoggerModule.class, ApplicationModule.class})
public interface ApplicationComponent extends LoggerComponent {
    CustomApplication customApplication();

    void inject(MainActivity mainActivity);
}

public class CustomApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Injector.INSTANCE.initializeApplicationComponent(this);
    }
}

public enum Injector {
    INSTANCE;

    private ApplicationComponent applicationComponent;

    public ApplicationComponent getApplicationComponent() {
        return applicationComponent;
    }

    void initializeApplicationComponent(CustomApplication customApplication) {
        this.applicationComponent = DaggerApplicationComponent.builder()
            .applicationModule(new ApplicationModule(customApplication))
            .build();
    }
}

编辑:这是从我们正在制作的应用程序中改装东西的实际代码:

public interface RecordingService {    
    ScheduledRecordsXML getScheduledRecords(long userId)
            throws ServerErrorException;
}

public class RecordingServiceImpl
        implements RecordingService {

    private static final String TAG = RecordingServiceImpl.class.getSimpleName();

    private RetrofitRecordingService retrofitRecordingService;

    public RecordingServiceImpl(RetrofitRecordingService retrofitRecordingService) {
        this.retrofitRecordingService = retrofitRecordingService;
    }

    @Override
    public ScheduledRecordsXML getScheduledRecords(long userId)
            throws ServerErrorException {
        try {
            return retrofitRecordingService.getScheduledPrograms(String.valueOf(userId));
        } catch(RetrofitError retrofitError) {
            Log.e(TAG, "Error occurred in downloading XML file.", retrofitError);
            throw new ServerErrorException(retrofitError);
        }
    }
}

@Module
public class NetworkClientModule {
    @Provides
    @Singleton
    public OkHttpClient okHttpClient() {
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.interceptors().add(new HeaderInterceptor());
        return okHttpClient;
    }
}

@Module(includes = {NetworkClientModule.class})
public class ServiceModule {
    @Provides
    @Singleton
    public RecordingService recordingService(OkHttpClient okHttpClient, Persister persister, AppConfig appConfig) {
        return new RecordingServiceImpl(
                new RestAdapter.Builder().setEndpoint(appConfig.getServerEndpoint())
                        .setConverter(new SimpleXMLConverter(persister))
                        .setClient(new OkClient(okHttpClient))
                        .setLogLevel(RestAdapter.LogLevel.NONE)
                        .build()
                        .create(RetrofitRecordingService.class));
    }

    //...
}

public interface RetrofitRecordingService {
    @GET("/getScheduledPrograms")
    ScheduledRecordsXML getScheduledPrograms(@Query("UserID") String userId);
}

public interface ServiceComponent {
    RecordingService RecordingService();

    //...
}

public interface AppDomainComponent
        extends InteractorComponent, ServiceComponent, ManagerComponent, ParserComponent {
}

@Singleton
@Component(modules = {
        //...
        InteractorModule.class,
        ManagerModule.class,
        ServiceModule.class,
        ParserModule.class
    //...
})
public interface ApplicationComponent
        extends AppContextComponent, AppDataComponent, AppDomainComponent, AppUtilsComponent, AppPresentationComponent {
    void inject(DashboardActivity dashboardActivity);
    //...
}
答案是

public class MainActivity extends AppCompatActivity {
    @OnClick(R.id.button) //ButterKnife
    public void onClickButton() {
        logger.log("Hello!");
    }

    @Inject
    Logger logger;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Injector.INSTANCE.getApplicationComponent().inject(this);
        ButterKnife.bind(this);
    }

    @Override
    protected void onDestroy() {
        ButterKnife.unbind(this);
        super.onDestroy();
    }
}

public class Logger {
    private static int i = 0;

    private CustomApplication customApplication;

    public Logger(CustomApplication application) {
        this.customApplication = application;
        i++;
    }

    public static int getI() {
        return i;
    }

    public void log(String text){
        Toast.makeText(customApplication, text + " " + i,Toast.LENGTH_SHORT).show();
    }
}


public interface LoggerComponent {
    Logger logger();
}

@Module
public class ApplicationModule {
    private CustomApplication customApplication;

    public ApplicationModule(CustomApplication customApplication) {
        this.customApplication = customApplication;
    }

    @Provides
    public CustomApplication customApplication() {
        return customApplication;
    }
}

@Module
public class LoggerModule {
    @Provides
    @Singleton
    Logger provideLogger(){
        return new Logger();
    }
}


@Singleton
@Component(modules={LoggerModule.class, ApplicationModule.class})
public interface ApplicationComponent extends LoggerComponent {
    CustomApplication customApplication();

    void inject(MainActivity mainActivity);
}

public class CustomApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Injector.INSTANCE.initializeApplicationComponent(this);
    }
}

public enum Injector {
    INSTANCE;

    private ApplicationComponent applicationComponent;

    public ApplicationComponent getApplicationComponent() {
        return applicationComponent;
    }

    void initializeApplicationComponent(CustomApplication customApplication) {
        this.applicationComponent = DaggerApplicationComponent.builder()
            .applicationModule(new ApplicationModule(customApplication))
            .build();
    }
}

编辑:这是从我们正在制作的应用程序中改装东西的实际代码:

public interface RecordingService {    
    ScheduledRecordsXML getScheduledRecords(long userId)
            throws ServerErrorException;
}

public class RecordingServiceImpl
        implements RecordingService {

    private static final String TAG = RecordingServiceImpl.class.getSimpleName();

    private RetrofitRecordingService retrofitRecordingService;

    public RecordingServiceImpl(RetrofitRecordingService retrofitRecordingService) {
        this.retrofitRecordingService = retrofitRecordingService;
    }

    @Override
    public ScheduledRecordsXML getScheduledRecords(long userId)
            throws ServerErrorException {
        try {
            return retrofitRecordingService.getScheduledPrograms(String.valueOf(userId));
        } catch(RetrofitError retrofitError) {
            Log.e(TAG, "Error occurred in downloading XML file.", retrofitError);
            throw new ServerErrorException(retrofitError);
        }
    }
}

@Module
public class NetworkClientModule {
    @Provides
    @Singleton
    public OkHttpClient okHttpClient() {
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.interceptors().add(new HeaderInterceptor());
        return okHttpClient;
    }
}

@Module(includes = {NetworkClientModule.class})
public class ServiceModule {
    @Provides
    @Singleton
    public RecordingService recordingService(OkHttpClient okHttpClient, Persister persister, AppConfig appConfig) {
        return new RecordingServiceImpl(
                new RestAdapter.Builder().setEndpoint(appConfig.getServerEndpoint())
                        .setConverter(new SimpleXMLConverter(persister))
                        .setClient(new OkClient(okHttpClient))
                        .setLogLevel(RestAdapter.LogLevel.NONE)
                        .build()
                        .create(RetrofitRecordingService.class));
    }

    //...
}

public interface RetrofitRecordingService {
    @GET("/getScheduledPrograms")
    ScheduledRecordsXML getScheduledPrograms(@Query("UserID") String userId);
}

public interface ServiceComponent {
    RecordingService RecordingService();

    //...
}

public interface AppDomainComponent
        extends InteractorComponent, ServiceComponent, ManagerComponent, ParserComponent {
}

@Singleton
@Component(modules = {
        //...
        InteractorModule.class,
        ManagerModule.class,
        ServiceModule.class,
        ParserModule.class
    //...
})
public interface ApplicationComponent
        extends AppContextComponent, AppDataComponent, AppDomainComponent, AppUtilsComponent, AppPresentationComponent {
    void inject(DashboardActivity dashboardActivity);
    //...
}

但有可能以某种方式用dagger包装系统创建的类吗?我在某个地方见过它,比如扩展应用程序类等等,但我找不到一个好的例子。做