最终变量未使用 @RequiredArgsConstructor 注解进行注入。

huangapple go评论69阅读模式
英文:

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&lt;Person&gt; getPersonsByFirstName(String firstname) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List&lt;Person&gt; getPersonsByLastName(String lastname) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List&lt;Person&gt; 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.

huangapple
  • 本文由 发表于 2020年9月21日 05:24:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63983815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定