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


}

 

posted @ 2018-08-02 14:52  shuaiflying  阅读(14651)  评论(1编辑  收藏  举报