bean/mapper始终为空

bean/mapper始终为空

Posted by Autuan.Yu on October 11, 2022

If the plan doesn’t work,change the plan , but never the goal.

错误描述

SpringBoot Mapper注入为空

异常代码情况

@Service
public class DemoServiceImpl implements DemoService {
    @Autowired
    private DemoMapper demoMapper;


    public DemoServiceImpl(){
        // !important  此处 demoMapper 为空
        demoMapper.doingSomething();
    }

}

解决方法

如果在构造方法中执行了需要bean 的操作,需要通过属性注入的方式注入bean.

如下:

@Service
public class DemoServiceImpl implements DemoService {
    
    private DemoMapper demoMapper;


    public DemoServiceImpl(@Autowired DemoMapper demoMapper){
        this.demoMapper = demoMapper;
        // 正常执行
        demoMapper.doingSomething();
    }

}

原因

构造器的优先级要比属性注入优先级高,执行构造器方法时,demoMapper尚没有注入,故为空。