3
我有一個Context類,它是一個在運行時逐漸填充的鍵值對。使用Guice來注入運行時生成的值
我想創建需要從上下文的一些值對象的實例。
例如:
public interface Task
{
void execute();
}
public interface UiService
{
void moveToHomePage();
}
public class UiServiceImpl implements UiService
{
public UiService(@ContexParam("username") String username, @ContexParam("username") String password)
{
login(username, password);
}
public void navigateToHomePage() {}
private void login(String username, String password)
{
//do login
}
}
public class GetUserDetailsTask implements Task
{
private ContextService context;
@Inject
public GetUserDetailsTask(ContextService context)
{
this.context = context;
}
public void execute()
{
Console c = System.console();
String username = c.readLine("Please enter your username: ");
String password = c.readLine("Please enter your password: ");
context.add("username", username);
context.add("password", password);
}
}
public class UseUiServiceTask implements Task
{
private UiService ui;
@Inject
public UseUiServiceTask(UiService uiService)
public void execute()
{
ui.moveToHomePage();
}
}
我希望能夠創建使用吉斯的UseUiServiceTask的實例。 我該如何做到這一點?
您是否嘗試過與供應商? –
如果我得到它正確的提供者不適用於我的情況。你能詳細說明嗎? – Ikaso
我在想像http://stackoverflow.com/a/15493413/4462333 這樣的東西如果它不鍛鍊,我可能會誤解你的問題。你能編輯它來添加更多與你的問題有關的代碼/信息嗎? –