1@Service2@RequiredArgsConstructor3public class UserServiceImpl implements UserService {4 private final UserRepository userRepository;5 private final RedisUtil redisUtil;6}
8.3 Fail-fast 原则
关键组件初始化时立即校验配置,不等到运行时:
1@Component2public class JwtUtil {3 public JwtUtil(@Value("${jwt.secret-key}") String secretKey, ...) {4 if (secretKey == null || secretKey.isBlank()) {5 throw new IllegalArgumentException("JWT secret-key 未配置");6 }7 this.key = Keys.hmacShaKeyFor(secretKey.getBytes(StandardCharsets.UTF_8));8 }9}