英文:
Final variable not injected with @RequiredArgsConstuctor annotation
问题
我遇到了一个错误,错误信息是“未初始化的空字段 'personRepo'”。
我不明白为什么在我使用了 @RequiredArgsConstructor 注解标记了 @Service 类之后,lombok 没有注入我的 final 变量 'personRepo'。
@RequiredArgsConstructor 表示它会注入使用 @NonNull 注解标记的 final 字段和变量,所以我不明白为什么会出现这个问题。
package nr.learning.service;
import java.util.List;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import nr.learning.domain.Person;
import nr.learning.repository.PersonRepo;
@Service
@RequiredArgsConstructor
public class PersonServiceImpl implements PersonService {
private final PersonRepo personRepo;
@Override
public List<Person> getPersonsByFirstName(String firstname) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<Person> getPersonsByLastName(String lastname) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<Person> getPersonsByFavoriteSoccerTeam(String team) {
// TODO Auto-generated method stub
return null;
}
}
感谢您提供的所有答案和提示!
英文:
I get an error saying that "blank field 'personRepo' is not initialized"
I do not understand why lombok is not injecting my final variable 'personRepo' when I annotated the @Service class with @RequiredArgsConstructor.
@RequiredArgsConstructor says that it injects final fields and variables that are annotated with @NonNull, so I do not understand why.
package nr.learning.service;
import java.util.List;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import nr.learning.domain.Person;
import nr.learning.repository.PersonRepo;
@Service
@RequiredArgsConstructor
public class PersonServiceImpl implements PersonService {
private final PersonRepo personRepo;
@Override
public List<Person> getPersonsByFirstName(String firstname) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<Person> getPersonsByLastName(String lastname) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<Person> getPersonsByFavoriteSoccerTeam(String team) {
// TODO Auto-generated method stub
return null;
}
}
All answers and tips are appreciated!
答案1
得分: 0
我通过从官网手动下载 Lombok,运行 JAR 文件,并重新启动 Eclipse IDE 来解决了这个问题。
英文:
I fixed the issue by downloading Lombok manually from their site, run the JAR file, and restart Eclipse IDE.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论