spring 给静态变量注入值

一般在spring中,给static变量加上@Autowired注解的时候会报空指针异常错误.

解决:

1.通过xml配置文件配置

这个就不多说了.

2.通过注解

@Component
public class StructUtil {
    
    private static AttendanceMapper attendanceMapper;
    
    
    @Autowired
    public  void setAttendanceMapper(AttendanceMapper attendanceMapper) {
        StructUtil.attendanceMapper = attendanceMapper;
    }
}

加上注解@Component,然后在静态变量的set方法上方加上@Autowired注解,注意:set方法不能为static类型.

 

posted @ 2019-02-22 09:54  随意的马蒂洛克  阅读(796)  评论(0编辑  收藏  举报