使用@Autowired注解无法注入(使用service报空指针异常)的问题解决,亲测可用!

@Component
public class CommMain{

     @Autowired
     public SysTransRecordCommonService sysTransRecordCommonService;

     public void saveTrans(SysTransRecord sysTransRecord){
      sysTransRecordCommonService.save(sysTransRecord);     
     }
}

 

 

出现无法正常注入的情况,网上找了很多方法,最终以下这种方法亲测可用!

@Component
public class CommMain {
    
      @Autowired
      public SysTransRecordCommonService sysTransRecordCommonService;


      public static CommMain commMain;
       
      @PostConstruct
      public void init(){
	  commMain = this;
	 commMain.sysTransRecordCommonService = this.sysTransRecordCommonService;
      }

    public void saveTrans(SysTransRecord sysTransRecord){   
     commMain.sysTransRecordCommonService.save(sysTransRecord);
} }

 好了,亲测可以正常存表!

 

 

posted @ 2019-04-15 11:19  dark_passion  阅读(2409)  评论(0编辑  收藏  举报