解決非controller使用,@Autowired或者@Resource注解注入Mapper接口為null的問題


知識點:在service層中注入其它的service接口或者mapper接口都是可以的

      但是在封裝的Utils工具類中或者非controller普通類中使用@Autowired@Resource注解注入Service或者Mapper接口,直接注入會出現問題

參考博客:https://blog.csdn.net/qiulingxin/article/details/78068314

 

解決辦法:

 

@Component
public class ReadExcel {

@Resource
private OrgMapper orgMapper;

//如果僅僅使用以上接口注入不行,mapper.xx為null
這是需要添加以下代碼即可
public static ReadExcel readExcel;

@PostConstruct
public void init(){
readExcel=this;
}
在ReadExcel工具類中,使用mapper接口的方法例子。使用"ReadExcel.orgMapper.xxx"
public Json insertOrg(){
   Json json = new Json();
    List<Org> orgList =readExcel.orgMapper.getOrgByOrgName(org_name);
    return  json;
}


}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM