Dagger 2:没有@Inject构造函数或@ Provide-annotated方法不能提供

时间:2017-11-10 13:24:33

标签: android dagger-2

我在我的示例应用程序中尝试了dagger2,在尝试构建我的代码后显示此异常。

代码:

    @Singleton


  @Component(modules = {UserModule.class,BackendServiceModule.class})
    public interface AppComponent {

        BackendServiceModule provideBackendService();

        // allow to inject into our Main class
        // method name not important
        void inject(MainActivity mainActivity);
    }

    public class AppGlobal extends Application
    {
        AppComponent appComponent;
        public static AppGlobal global;

        @Override
        public void onCreate() {
            super.onCreate();
            global = this;
            initAppComponent();

        }

        private void initAppComponent() {

        }
        public AppComponent getDataComponent() {
            return appComponent;
        }
    }

    public class BackendService {


        @Inject
        public User user;

        private String serverUrl;

        @Inject
        public BackendService(@Named("serverUrl") String serverUrl) {
            this.serverUrl = serverUrl;
        }

        public boolean callServer() {
            if (user !=null && serverUrl!=null && serverUrl.length()>0) {
                System.out.println("User: " + user + " ServerUrl: "  + serverUrl);
                return true;
            }
            return false;
        }
    }
    @Module
    public class BackendServiceModule
    {

        @Provides
        @Singleton
        BackendService provideBackendService(@Named("serverUrl") String serverUrl) {
            return new BackendService(serverUrl);
        }

        @Provides
        @Named("serverUrl")
        String provideServerUrl() {
            return "http://www.vogella.com";
        }

        @Provides
        @Named("anotherUrl")
        String provideAnotherUrl() {
            return "http://www.google.com";
        }
    }

    public class MainActivity extends AppCompatActivity {

        @Inject
        BackendService service;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            AppGlobal.global.getDataComponent().inject(this);
            TextView result = (TextView) findViewById(R.id.resulttxt);
            result.setText(service.callServer()+"");
        }
    }
    public class User {

        private String firstName;
        private String lastName;

        public User(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }

        @Override
        public String toString() {
            return "User [firstName=" + firstName + ", lastName=" + lastName + "]";
        }
    @Module
    public class UserModule
    {
        @Singleton
        @Provides
        User providesUser()
        {
            return new User("syam","jjjj");
        }

    }

异常:       错误:(16,26)错误:如果没有@Inject构造函数或@ Provide-annotated方法,则无法提供com.example.intrio.daggersample2.BackendServiceModule。          com.example.intrio.daggersample2.BackendServiceModule在。提供          com.example.intrio.daggersample2.AppComponent.provideBackendService()

2 个答案:

答案 0 :(得分:1)

AppComponent中,您有行BackendServiceModule provideBackendService();,因此您尝试注入整个模块而不是单个依赖项。可能这不是你想要实现的目标。

将其更改为BackendService provideBackendService();并从@Module课程中删除BackendService注释。

答案 1 :(得分:0)

您已交换了BackendService@Module:模块注释为@Singleton @Component(modules = {UserModule.class,BackendServiceModule.class}) // <-- Modules public interface AppComponent { BackendService provideBackendService(); // <-- Getter ("provision method") // allow to inject into our Main class // method name not important void inject(MainActivity mainActivity); // <-- Members injection method } 并用于绑定/配置Dagger图,然后用于组合您的对象通过在组件上放置一个getter(&#34;提供方法&#34;)来实现。

public AlumnoModel FromDomainToModel_Alumnos(Alumnos alumno)
{
    Mapper.Initialize(cfg =>
    {
        cfg.CreateMap<Turnos, TurnoModel>()
        .ForMember(d => d.Id, s => s.MapFrom(src => src.Id))
        .ForMember(d => d.Descripcion, s => s.MapFrom(src => src.Descripcion));

        cfg.CreateMap<Carreras, CarreraModel>()
        .ForMember(d => d.Id, s => s.MapFrom(src => src.Id))
        .ForMember(d => d.Descripcion, s => s.MapFrom(src => src.Descripcion));

        cfg.CreateMap<AlumnoModel, Alumnos>()
        .ForMember(d => d.Nombre, s => s.MapFrom(src => src.Nombre))
        .ForMember(d => d.Apellido, s => s.MapFrom(src => src.Apellido))
        .ForMember(d => d.Legajo, s => s.MapFrom(src => src.Legajo))
        .ForMember(d => d.Dni, s => s.MapFrom(src => src.Dni))
        .ForMember(d => d.Carrera, s => s.MapFrom(src => src.Carrera.Id))
        .ForMember(d => d.Turno, s => s.MapFrom(src => src.Turno.Id))

        .ForMember(d => d.Turnos, s => s.MapFrom(src => src.Turno))
        .ForMember(d => d.Carreras, s => s.MapFrom(src => src.Carrera));
    });

    return Mapper.Map<AlumnoModel>(alumno);
}

有关详细信息,请参阅documentation for @Component