解决非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