gpt4 book ai didi

java - 字段 @Inject 在 Dagger2 中不起作用

转载 作者:行者123 更新时间:2023-12-02 11:16:50 25 4
gpt4 key购买 nike

我是 Dagger 的 CDI 新手。我有以下结构。问题是当我在 Main 类中获取 backendService 时,仅获取直接 backendService,但底层 User 依赖项仍为空。这个设置有什么问题吗?

我的组件类

import javax.inject.Singleton;
import dagger.Component;

@Singleton
@Component(modules = {UserModule.class, BackEndServiceModule.class})
public interface MyComponent {

User user();

BackendService backendService();

void inject(Main main);
void injectIntoBackendService(BackendService backendService);
}

类(class)用户:

import javax.inject.Inject;

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 + "]";
}
}

用户模块类

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;

@Module
public class UserModule {
@Provides @Singleton User providesUser() {
return new User("Lars", "Vogel");
}
}

后端服务

import javax.inject.Inject;
import javax.inject.Named;

public class BackendService {

@Inject public User user;

private String serverUrl;

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

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

后端服务模块

import javax.inject.Named;
import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;

@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";
}
}

主要

import javax.inject.Inject;

public class Main {

@Inject public BackendService backendService;

public void callService() {
boolean callServer = backendService.callServer();

if (callServer) {
System.out.println("Server call was successful. ");
} else {
System.out.println("Server call failed. ");
}
}

public static void main(String[] args) {
Main m = new Main();
MyComponent component = DaggerMyComponent.builder().build();
component.inject(m);
m.callService();
}
}

最佳答案

请查看字段注入(inject)与构造函数注入(inject)。您尝试部分使用两者,但最终都没有使用。

@Singleton
@Component(modules = {UserModule.class, BackEndServiceModule.class})
public interface MyComponent {

User user();

BackendService backendService();

void inject(Main main);

// you don't need that if you create the object!
// void injectIntoBackendService(BackendService backendService);
}

如有疑问,请使用构造函数注入(inject)。顾名思义..在构造函数中定义依赖项并删除任何其他 @Inject .

@Singleton // add singleton here, use constructor injection!
public class BackendService {

public User user;

private String serverUrl;

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

当您不需要任何进一步的设置时,您不需要任何 @Provides带注释的方法,所以摆脱 provideBackendService(@Named("serverUrl") String serverUrl)请。我们在上面的实现类上指定了范围。

我还写了一篇关于 few basic things 的文章使用 Dagger 时请记住。

关于java - 字段 @Inject 在 Dagger2 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50207553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com