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