龙目岛和@Autowired
Lombok and @Autowired
如果我将 Lombok 连接到项目,如何使用 @autowired 注释注入 bean?
这些链接上的答案似乎不稳定(支持?):
Spring support in IDEA with Lombok: Is "Navigate to autowired dependencies" supported?
我总是使用 @RequiredArgsContstructor
并且它会生成自动装配的构造函数。在这种情况下,不需要 @Autowired
注释。
从 Spring 4.3 开始,如果 class 只定义一个构造函数,Spring 将理解使用该构造函数而无需添加 @Autowired
或任何其他构造函数注释。只需确保您有一个合适的构造函数。
如@vszholobov 所述,您可以使用 @RequiredArgsConstructor
或 @AllArgsConstructor
;通常最好使您的依赖项 final
,因此除非您有类似默认值 Clock
的东西,否则它们基本上是等效的。 (这也适用于其他创建构造函数的样式,例如 @TupleConstructor(defaults = false)
for Groovy。)
在旧版本的 Spring 中,您需要 Lombok 用 @Autowired
注释构造函数,例如@AllArgsConstructor(onConstructor = @__(@Autowired))
.
如果我将 Lombok 连接到项目,如何使用 @autowired 注释注入 bean?
这些链接上的答案似乎不稳定(支持?):
Spring support in IDEA with Lombok: Is "Navigate to autowired dependencies" supported?
我总是使用 @RequiredArgsContstructor
并且它会生成自动装配的构造函数。在这种情况下,不需要 @Autowired
注释。
从 Spring 4.3 开始,如果 class 只定义一个构造函数,Spring 将理解使用该构造函数而无需添加 @Autowired
或任何其他构造函数注释。只需确保您有一个合适的构造函数。
如@vszholobov 所述,您可以使用 @RequiredArgsConstructor
或 @AllArgsConstructor
;通常最好使您的依赖项 final
,因此除非您有类似默认值 Clock
的东西,否则它们基本上是等效的。 (这也适用于其他创建构造函数的样式,例如 @TupleConstructor(defaults = false)
for Groovy。)
在旧版本的 Spring 中,您需要 Lombok 用 @Autowired
注释构造函数,例如@AllArgsConstructor(onConstructor = @__(@Autowired))
.