[英]NullPointerException in Spring Boot Autowired service
I am creating a Spring Boot application and I got a NullPointerException in an @Autowired bean.我正在创建一个 Spring Boot 应用程序,并且在 @Autowired bean 中得到了一个 NullPointerException。
I have checked the annotations and I have marked my services interfaces and classes as @Service.我检查了注释,并将我的服务接口和类标记为@Service。
I have also created a Java configuration file to specify the base packages for component scanning but the problem persists.我还创建了一个 Java 配置文件来指定用于组件扫描的基本包,但问题仍然存在。
I am presenting my code so far:到目前为止,我正在展示我的代码:
package gr.parisk85.dnd35cg.helper.skill;
@Service
public class BarbarianSkillStrategy implements SkillStrategy {
private static final double MAX_RANKS = 4;
private static final double RANK_COST = 1;
@Autowired
private DnDClassService dnDClassService;
@Override
public List<CharacterSkillDTO> select(List<DnDSkill> dndSkills) {
List<CharacterSkillDTO> characterSkillDTOList = dndSkills.stream()
.map(skill -> CharacterSkillMapper.map(skill))
.collect(Collectors.toList());
Optional<DnDClass> dnDClass = dnDClassService.findDnDClassByClassName(Constants.BARBARIAN);
characterSkillDTOList
.stream()
.filter(skill -> dnDClass.get().getClassSkills().contains(skill))
.forEach(skill -> {
skill.setMaxRanks(MAX_RANKS);
skill.setRankCost(RANK_COST);
});
return characterSkillDTOList;
}
}
The exception is thrown in line:异常在行中抛出:
Optional<DnDClass> dnDClass = dnDClassService.findDnDClassByClassName(Constants.BARBARIAN);
The debugger shows that dnDClassService is null.调试器显示 dnDClassService 为空。
Above is the SkillStrategyInterface also marked as @Service:上面是 SkillStrategyInterface 也标记为@Service:
package gr.parisk85.dnd35cg.helper.skill;
@Service
public interface SkillStrategy {
List<CharacterSkillDTO> select(List<DnDSkill> dndSkills);
}
The DnDClassService interface: DnDClassService 接口:
package gr.parisk85.dnd35cg.service;
@Service
public interface DnDClassService {
Optional<DnDClass> findDnDClassByClassName(String name);
}
And the Impl class:和 Impl 类:
package gr.parisk85.dnd35cg.service.impl;
@Service
public class DnDClassServiceImpl implements DnDClassService {
@Autowired
private DnDClassRepository dnDClassRepository;
@Override
public Optional<DnDClass> findDnDClassByClassName(String name) {
return dnDClassRepository.findDnDClassByClassName(name);
}
}
I also wrote a configuration class in case I messed up with the component scanning:我还写了一个配置类,以防我搞砸了组件扫描:
package gr.parisk85.dnd35cg.config;
@Configuration
@ComponentScan(basePackages = {
"gr.parisk85.dnd35cg.helper.skill",
"gr.parisk85.dnd35cg.helper",
"gr.parisk85.dnd35cg.service",
"gr.parisk85.dnd35cg.service.impl"
})
public class ApplicationConfig {
}
You can find my complete project on my github:你可以在我的 github 上找到我的完整项目:
Thanks in advance for your help.在此先感谢您的帮助。
The class SkillStrategyFactory
is the problem:类
SkillStrategyFactory
是问题:
public class SkillStrategyFactory {
private static final Map<String, SkillStrategy> skillStrategyMap;
static {
skillStrategyMap = new HashMap<>();
skillStrategyMap.put(Constants.BARBARIAN, new BarbarianSkillStrategy());
}
public static SkillStrategy getStrategy(DnDClass dndClass) {
return skillStrategyMap.get(dndClass.getClassName());
}
}
Here, you create the BarbarianSkillStrategy
yourself using the default constructor.在这里,您使用默认构造函数自己创建
BarbarianSkillStrategy
。 You need to let Spring do that for you for Autowiring
to work.您需要让 Spring 为您执行此操作,以便
Autowiring
工作。 So you could inject it directly or transform the factory to a Spring service and inject every implementation of a SkillStrategy in it.因此,您可以直接注入它或将工厂转换为 Spring 服务并在其中注入 SkillStrategy 的每个实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.