Spring cannot inject a value directly into a static variable because the static variable is not part of the object, it belongs to the class, that is, when the class is loaded with bytecode, the variable is initialized, that is, the variable is allocated memory, which causes spring to ignore the static variable. So this is the wrong way to do this, it is not injected, when the use of the variable will result in a null pointer error:
@Autowiredprivate static StudentMapper studentMapper;
Spring Dependency Injection is dependent on the set method, where static variables do not belong to the object and belong to the class only. The workaround is to add a non-static set method, as follows:
private static StudentMapper studentMapper;public StudentMapper getStudentMapper() { return studentMapper;}@Autowiredpublic void setStudentMapper(StudentMapper studentMapper) { this.studentMapper = studentMapper;}
spring-how to inject values into static variables