使用 Lombok 和 Guice,我想从两个不同的调用代码实例注入两个不同的命名提供程序。
当前设置为:
@RequiredArgsConstructor(onConstructor = @__(@Inject))
class CallerA {
Strategy strategy;
public void run() {
// Do some things, and then
strategy.runMe();
}
}
@RequiredArgsConstructor(onConstructor = @__(@Inject))
class Strategy {
Client client; // Client is an interface, I am adding a second implementation now
public void runMe() {
// Lots of logic that needs to run independent of client, and then
client.doThings();
}
}
class ClientA implements Client {
@Builder
ClientA() {...}
}
然后在模块中:
@Provides
@Named("ClientA") // I just added this name, it wasn't named before, previously there was only one implementation and so only one thing to inject = no need for Named
public Client providesClientA()
我想要的是将一个命名的 ClientB 添加到模块中,并在运行策略代码之前有一个 CallerB 将 ClientB 注入到其策略中。
@Provides
@Named("ClientB") // I just added this name, it wasn't named before
public Client providesClientB()
@SomeLombok/GuiceMagicAnnotation
class CallerB {
Strategy strategy; // But build/inject Strategy with ClientB named Provides
// ...
}
我尝试过使用各种注释和构造函数模式,但似乎无法找出在调用者代码中注入正确客户端的正确咒语。