@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public UserService(){
userMapper.test();
}
}
以上代码在执行的时候,构造器可能会抛出空指针异常,调试后会发现UserMapper并没有被成功注入
原因就是因为UserMapper还没加载到
@Service
public class UserService {
private final UserMapper userMapper;
@Autowired
public UserService(UserMapper userMapper){
this.userMapper = userMapper;
this.userMapper.test();
}
}
改为以上方式可以解决这类问题