简体   繁体   English

Spring Boot Autowired 服务中的 NullPointerException

[英]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 上找到我的完整项目:

Complete project 完成项目

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.

相关问题 Spring引导Autowired Service和Repository抛出nullpointerexception - Spring boot Autowired Service and Repository throwing nullpointerexception Spring Boot 自动装配服务 java.lang.NullPointerException - Spring Boot autowired service java.lang.NullPointerException Spring Boot - 环境@Autowired抛出NullPointerException - Spring Boot - Environment @Autowired throws NullPointerException Spring 在单元测试中启动 @Autowired 返回 NullPointerException - Spring Boot @Autowired in unit test returns NullPointerException Spring Boot @Autowired Environment 抛出 NullPointerException - Spring Boot @Autowired Environment throws NullPointerException 使用Jersey和Spring进行REST服务的@Autowired属性的NullPointerException - NullPointerException on @Autowired attribute with Jersey and Spring for REST service Spring @configurable NullPointerException,@autowired 服务是 null - Spring @configurable NullPointerException, @autowired service is null 在@Service 中使用 Kotlin 的 Spring Boot @Autowired 始终为 null - Spring Boot @Autowired with Kotlin in @Service is always null spring 启动自动连线服务获取 null - spring boot autowired service getting null 来自 Spring Boot 的 @Autowired 服务在 WAR 中为空 - @Autowired service from Spring Boot is null in WAR
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM