@Component下mapper为空的解决办法

问题:工作中,出现一个问题。在一个工具类中(非Controller、非Service),@Autowired注解注入Service或者mapper,service, mapper为null?

 

解决步骤: 借鉴了网上其他大佬的解决办法,记录一下

第一步:工具类上面加@Component注解

第二步: 静态初使化当前类

第三步:工具类中写一个init方法,并在方法上面加上注解 @PostConstruct  @PostConstruct

 

第四步: mapper.方法" 就可以了

 

posted @ 2022-03-17 17:05  逗号Comma  阅读(1192)  评论(0)    收藏  举报