Tag: dagger 2

Dagger 2 – 使用@Named无法正常注入多个相同类型的对象

我的模块: @Module public class TcpManagerModule { private ITcpConnection eventsTcpConnection; private ITcpConnection commandsTcpConnection; public TcpManagerModule(Context context) { eventsTcpConnection = new EventsTcpConnection(context); commandsTcpConnection = new CommandsTcpConnection(context); } @Provides @Named(“events”) public ITcpConnection provideEventsTcpConnection() { return eventsTcpConnection; } @Provides @Named(“commands”) public ITcpConnection provideCommandsTcpConnection() { return commandsTcpConnection; } } 零件: @Component(modules = TcpManagerModule.class) public interface TcpManagerComponent { void inject(ITcpManager tcpManager); […]

Dagger 2活动注射不起作用

我正在尝试新的匕首2,这是我第一次实施它,但我不能让它工作。 我想我得到了这个概念,我理解这里的例子 我尝试复制相同的结构只是为我的例子稍作修改。 这是扩展Graph的AppComponent,我定义了我想要的类。 @ApplicationScope @Component(modules = {AppModule.class, DataModule.class}) public interface EFAppComponent extends EFGraph { /** * An initializer that creates the graph from an application. */ public final static class Initializer { private Initializer() { } // No instances. public static EFAppComponent init(EFApp app) { return Dagger_EFAppComponent.builder() .appModule(new AppModule(app)) .build(); } } } public […]

Android Dagger 2 Dependency没有被注入

我正在尝试将Dagger 2用于我的应用程序,但我遇到了有关实体存储库的一些问题,而且我还没有弄明白我错过了什么。 这是我的应用程序组件: @Singleton @Component( modules = { AndroidModule.class, RepositoryModule.class } ) public interface ApplicationComponent { void inject(AndroidApplication app); IDependenceyRepository dependencyRepository(); } 我的模块: @Module public class RepositoryModule { @Provides @Singleton IDependenceyRepository provideDependendencyRepository(Context context) { return new DependencyRepository(context); } } @Module public class AndroidModule { private final AndroidApplication app; public AndroidModule(AndroidApplication app) { this.app = app; […]

匕首2:如何注入Map <Class ,Provider >

在Dagger 2中,是否可以注入Map<Class, Provider> Map<Class, Provider> Map<Class, Provider> ? 假设,我有几个扩展Foo的类 class Bar extends Foo { @Inject Bar() {} } class Baz extends Foo { @Inject Baz() {} } 现在我想通过声明来创建一个FooFactory class FooFactory { @Inject FooFactory(Map<Class, Provider> providers) {} } 我能用最少的配置在Dagger 2中做到这一点吗? 我读过有关Multibinding但我无法使用它。

Android TestCase中的Dagger 2dependency injection

我已经构建了一个示例应用程序(是的,它实际上只是一个示例,没有多大意义,但有助于理解Dagger 2中的Android清洁架构和dependency injection)。 我的代码可以在github上找到 。(过时。看到这篇文章)示例应用程序只是让你在EditText输入一个名字,如果按下按钮,你会看到一条消息“Hello YourName” 我有三个不同的组件: ApplicationComponent , ActivityComponent和FragmentComponent 。 FragmentComponent包含三个模块: ActivityModule FragmentModule InteractorModule InteractorModule提供了一个MainInteractor 。 @Module public class InteractorModule { @Provides @PerFragment MainInteractor provideMainInteractor () { return new MainInteractor(); } } 在我的Activity-UnitTest中,我想伪造这个MainInteractor 。 这个Interactor只有一个方法public Person createPerson(String name) ,它可以创建一个Person对象。 FakeMainInteractor具有相同的方法,但始终创建​​一个名为“Fake Person”的Person对象,与您传递的参数FakeMainInteractor 。 public class FakeMainInteractor { public Person createPerson(final String name) { return new […]

Dagger2自定义@Qualifier用法

假设我正在制造一辆汽车,我有几个不同实施的刹车豆 class Car { @Inject Car(@BrakeType(value=”abs”)Brake frontBrake, @BrakeType(value=”nonabs”)Brake rearBrake) { } } @Qualifier @Retention(RetentionPolicy.RUNTIME) public @interface BrakeType { String value(); } interface Brake {} @BrakeType(value=”abs”) class AbsBrakeImpl implements Brake { @Inject AbsBrakeImpl() {} } @BrakeType(value=”nonabs”) class BrakeImpl implements Brake { @Inject BrakeImpl() {} } 为什么我的CarModule必须为特定的制动类型定义@Provides? 自定义注释类型@BrakeType不应该足以确定要注入哪个impl? 或者这需要使用reflection,dagger2不使用? @Module public class CarModule { @Provides @BrakeType(“abs”) public […]

在Android中“注入一切”是一种不好的做法吗?

关于dependency injection的研究我发现一些方法建议注入所有东西,而另一些方法则说没有必要这样做 。 在我目前的项目中,关于dependency injection的经验法则是“ 如果该类是由我创建的,那么我可以将其注入 ”。 换句话说,只有像SimpleDateFormat , ArrayList , HashMap这样的类才是我项目中的新function。 我做这种方法的意图是,一旦在Activity调用Injector.getApplicationComponent().inject(this) ,我就可以@Inject任何类。 基本上我的所有类都有一个带@Inject的非args构造函数。 我主要使用DI,因为我认为一旦new运算符由Dagger生成的类专门使用,它将改善性能和内存使用。 但我读了Dagger 1开发人员的post ,说DI对性能没有影响,用法基本上是为了减少样板。 第一个问题是: Dagger 2在Android应用程序中没有任何性能优势? 我的项目运行没有问题,我认为“注入一切”的方法有助于更好地组织,尽管存在一些缺点。 使用此方法的一个示例是以下类: public class TimelineEntryAdapter { @Inject Provider mTwitterProvider; @Inject Provider mInstagramProvider; @Inject Provider mFacebookProvider; @Inject TimelineEntryComparator mComparator; @Inject public TimelineEntryAdapter() { } 第二个问题是: 在Android中注入所有东西是不好的做法? 如果第二个问题的答案是“否”,那么有一种更好的方法来处理非args构造函数来创建类? 因为当我用@Inject注释创建一个非args构造函数并且一个类需要一些参数来处理时,我必须使用setters : public class SavelArtist { private […]

摆脱Dagger 2警告“生成一个MembersInjector”

鉴于以下课程 abstract class AbstractClass { @Inject SomeDependency someDependency; } class SomeClass extends AbstractClass { @Inject AnotherDependency anotherDepenency; public void onCreate() { component = // Get component instance somehow component.inject(this); } } 在Dagger 2中,当将依赖项注入到从包含依赖项的抽象基类扩展的类中时,Dagger显示了Generating a MembersInjector for AbstractClass. Prefer to run the dagger processor over that class instead.类型的警告Generating a MembersInjector for AbstractClass. Prefer to run the […]

如何使用Dagger2实例化具有动态成员的对象?

我有一个在构造函数中获取参数的类: public class Dependency{ Dependency(int number1, int number2, int number3, DependencyListener listener){} } 每个依赖类需要路径不同的参数以实例化依赖项。 我应该如何定义模块和其他类可以正确启动它的组件,同时传输不同的值并将“this”作为侦听器传递? 另外,在这种情况下我应该如何使用@Inject方法? 编辑1 @ jeff-bowman我在考虑使用以下方法: @Module public class DependencyModule { int first; int second; int third; DependencyListener listener; public DependencyModule(int first, int second, int third, DependencyListener listener) { this.first = first; this.second = second; this.third = third; this.listener = listener } […]

多个实例中的Dagger 2单身人士

我刚刚测试了Dagger 2,我对单例注释有一些奇怪的行为。 我创建了一些测试代码来显示我的问题。 我的模块: @Module public class App { @Provides @Singleton ThingA provideThingA(){ return new ConcreteThingA(); } } 在singleton中我想要的东西的界面: public interface ThingA { void showMyId(); } 执行: public class ConcreteThingA implements ThingA { @Override public void showMyId() { System.out.println(this); } } 执行Dagger的代码: public void doStuff() { ThingA thingA=DaggerThingAComponent.create().provideThingA(); ThingA thingB=DaggerThingAComponent.create().provideThingA(); System.out.println(“Hello”); } 这是一个屏幕截图,显示当我要求它两次时,我没有得到相同的实例。 我错过了什么基本的东西? ThingA只是一个愚蠢的名字,在我的实际应用程序中,我希望在我的服务上有这种单例行为。