Gui*_*lty 0 java spring lombok spring-boot spring-webflux
我有一个带有private final WebClient webClient. 而其余的私有最终字段可以使用 Lombok 提供的 @RequiredArgsConstructor 进行初始化。Spring-Webflux WebClient 对象似乎只能像这样初始化:this.webClient = WebClientBuilder.build()因此 lombok 生成的构造函数不起作用。
有没有解决的办法?
示例代码:
@RequiredArgsConstructor
@Controller
public class SomeController {
private final SomeService someService;
private final SomeConfig someConfig;
private final SomeOtherConfig someOtherConfig;
private final WebClient webClient;
// etc.
}
Run Code Online (Sandbox Code Playgroud)
这不是因为龙目岛。当依赖项存在于应用程序上下文中时,Spring 会注入依赖项。仅当实例使用 Spring 构造型注释、通过@Bean注释创建或属于自动配置的一部分时,Spring 才会创建实例。该Webclient实例需要存在于要注入的应用程序上下文中。
要注入,WebClient您可以在配置类中创建与下面相同的内容。
@Configuration
public class Config {
@Bean
public WebClient webClient(){
return WebClient.builder().build();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1532 次 |
| 最近记录: |