Lombok 将命名提供程序注入 arg 构造函数

问题描述 投票:0回答:1

使用 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
    // ...
}

我尝试过使用各种注释和构造函数模式,但似乎无法找出在调用者代码中注入正确客户端的正确咒语。

java dependency-injection guice lombok
1个回答
0
投票

这不是 Lombok 支持的功能。 Here是他们的

@*ArgsConstructor
注释的完整文档,并且根本没有提到参数上的注释。

您必须手动编写此构造函数。

© www.soinside.com 2019 - 2024. All rights reserved.