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类型.

猜你喜欢

转载自www.cnblogs.com/lishuaiqi/p/10416609.html