@RequiredArgsConstructor(onConstructor = @__(@Autowired))解析出错
时间: 2023-08-26 07:14:31 浏览: 39
这是一个 Lombok 注解,用于自动生成一个带有 final 标识的成员变量,并在构造函数中进行初始化。其中的 onConstructor 属性表示在哪个构造函数上添加该注解。@__ 表示空注解,这里用于表示构造函数不需要任何参数。@Autowired 表示使用 Spring 的依赖注入机制进行对象的注入。如果出现解析错误,可能是因为您没有正确导入相应的 Lombok 和 Spring 相关依赖,或者没有开启 Lombok 的注解处理器。建议检查一下相关配置和依赖是否正确。
相关问题
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequiredArgsConstructor(onConstructor = @__(@Autowired)) 是使用 Lombok 注解的一种方式,用于生成一个带有构造函数的类。在这个注解中,@Autowired 是一个 Spring Framework 的注解,用于自动注入依赖。和引用的描述,这种写法在使用 @RequiredArgsConstructor 注解时是错误的。正确的写法是使用 @RequiredArgsConstructor(onConstructor_ = {@Autowired})。这样的写法可以确保在生成的构造函数中自动注入所需的依赖。
因此,如果你想使用 @RequiredArgsConstructor 注解来生成带有自动注入依赖的构造函数,应该使用如下的写法:
@RequiredArgsConstructor(onConstructor_ = {@Autowired})<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [Spring Boot 使用 RequiredArgsConstructor 参数 onConstructor 报错 找不到符号onConstructor_ __解决方法](https://blog.csdn.net/Lance_welcome/article/details/123232468)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [@RequiredArgsConstructor(onConstructor =@_(@Autowired))](https://blog.csdn.net/waitu88/article/details/112474954)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
RequiredArgsConstructor(onConstructor = @__(@Autowired)):
`@RequiredArgsConstructor(onConstructor = @__(@Autowired))` 是一个Lombok注解,它可以为类的非静态字段生成构造函数,并在构造函数中自动注入依赖。这个注解会为带有 `@NonNull` 注解的字段生成参数,并将它们作为构造函数的参数。它的作用是简化构造函数的编写,避免手动添加大量的 `@Autowired` 注解和参数。
举个例子,如果有一个类如下所示:
```java
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MyClass {
private final MyDependency myDependency;
private final AnotherDependency anotherDependency;
private final YetAnotherDependency yetAnotherDependency;
// ...
}
```
在这个例子中,`MyClass` 类有三个非静态字段 `myDependency`、`anotherDependency` 和 `yetAnotherDependency`。使用 `@RequiredArgsConstructor(onConstructor = @__(@Autowired))` 注解后,Lombok会为这些字段生成一个带有自动注入的构造函数,如下所示:
```java
public MyClass(MyDependency myDependency, AnotherDependency anotherDependency, YetAnotherDependency yetAnotherDependency) {
this.myDependency = myDependency;
this.anotherDependency = anotherDependency;
this.yetAnotherDependency = yetAnotherDependency;
}
```
这样,在使用这些依赖时,就不需要手动添加 `@Autowired` 注解了。