Spring 中@NonNull、@NonNullApi、@NonNullFields以及@Nullable注解及其示例

【Spring】Spring 中的Null-Safety 1.概述 转载:Spring 中的Null-Safety 之前一直在某些代码中看到过使用@Nullable 标注过的注释,当时也没有在意到底是什么意思,所以这篇文章来谈谈Spring中关于Null的那些事。 在Java中不允许让你使用类型表示其null的安全性,但Spring Framework 现在在org.sprinngframework.lang包提供以下注释,以便声明API和字段的可空性: @Nullable: 用于指定参数、返回值或者字段可以作为null的注释。 @NonNull: 与上. 阅读详情
AI摘要

本文深入探讨了Spring框架中@NonNull、@NonNullApi、@NonNullFields及@Nullable注解的使用,这些注解用于指示代码中对象是否允许为null,是理解和编写高质量Spring应用程序的关键。

    @NonNull,用于注解方法,参数以及变量,指示目标对象不能为null;

    @NonNullApi,包(Package)级别注解,指定参数和方法返回值默认不能为null;

    @NonNullFields,包(Package)级别注解,用于变量不能为null;

    @Nullable,可用于注解方法,参数以及变量,指定目标对象可以weinull,若是与@NonNullApi和@NonNullFields共用时,则会覆盖;

    示例,Spring框架中Spring core其中一个package-info.java源码及其Java项目中的package-info.java的作用

/**
 * Provides basic classes for exception handling and version detection,
 * and other core helpers that are not specific to any part of the framework.
 */
@NonNullApi //指示该包下,参数,方法返回值不能为null
@NonNullFields // 指示该包下,变量不能为null
package org.springframework.core;

import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;

 

知识导航神器:Obsidian Breadcrumbs 插件全攻略 Parent: 插件Child: Breadcrumbs 用法这样,它就能识别出你和其他笔记的关系。 阅读详情

相关推荐

别急着做AI:FDE教你识别真需求,让AI真正落地

人工智能项目在落地过程中,经常出现业务方高喊“我们要做AI”,却说不清具体应用场景的现象。判断一个需求是否值得用AI实现,不能只看技术流行度,而要从问题出现频率、当前替代方案以及可量化业务价值三个维度展开验证。同时需要冷静比较规则引擎与大模型的边界:不是所有语义任务都要靠生成式模型,稳定、可解释且低成本的传统方案往往更具工程性价比。数据闭环也至关重要,缺乏历史样本与反馈回流路径的项目,很难在真实环境中持续优化。针对智能客服、合同审查或企业知识库等典型场景,可借助人机协同与最小代价的用户验证,逐步筛除伪需求,

weixin_42624889的博客 341

Lombok之@NonNull使用

目录一. 为什么要用@NonNull?二. @NonNull如何使用?三. @NonNull源码四. 特别说明 一. 为什么要用@NonNull? 二. @NonNull如何使用? 三. @NonNull源码 四. 特别说明 本文已经收录在Lombok注解系列文章总览中,并继承上文中所提的特别说明。 源码地址:gitee ...

cauchy6317的博客 4万+

C/C++ 计算程序运行的时间

C/C++计算程序执行时间的方法,精度依次增加,推荐度也依次增加 1.#include <time.h> clock_t start, finish; double process_interval ; start=clock(); process(); //要计时的程序 finish=clock(); process_interval =(double)(finish-star...

stu_shanghui的博客 1020

Spring注解@NonNull作用 Spring注解@Nullable作用 Spring NonNull 注解作用 Spring Nullable注解作用

Spring注解@NonNull作用 Spring注解@Nullable作用 Spring NonNull 注解作用 Spring Nullable注解作用 @NonNull 或 @Nullable , 没有参数验证功能,只是一个标记注解,告知调用方:是否允许为null的情况。

HaHa_Sir的博客 9015

Cadence Allegro 17.4学习记录开始01-原理图Capture CIS 17.4

Orcad的学习记录

qq_31444421的博客 1万+

10xGenomics官方可视化工具——Loupe Browser

Loupe Browser是⼀款10xGenomics开发的交互式桌⾯应⽤程序,⽤于探索10x单细胞数据,可以用来找出感兴趣的细胞、找出显著差异表达的基因、鉴定细胞类型、探索细胞亚类和数据共享。

weixin_47195452的博客 3350

@NonNull

是 Spring Framework 提供的一个注解,用于指示某个元素(如字段、方法返回值或参数)不能为。它通常用于提高代码的可读性和可维护性,并帮助工具(如静态分析器)检测潜在的。如果你使用其他注解库(如 Lombok),可以使用 Lombok 提供的。方法,编译器或静态分析工具将会发出警告或错误。以下是一个完整的示例,展示了如何在方法中使用。以下是一个完整的示例,展示了如何在类中使用。虽然不常见,但你也可以在局部变量上使用。在这个示例中,Lombok 的。注解,并在方法中显式地进行。

我这个代码你能看懂吗? 2130

基于STM32+华为云设计的智慧农业灌溉系统(2023升级版)(158)

本项目设计一种基于STM32的智慧农业监测系统。该系统能够检测空气温度、湿度、光强度和土壤湿度等关键指标,并自动调节浇水量,实现作物生长环境的精细化管理。同时,本项目还支持本地LCD屏幕数据显示和通过ESP8266上传数据到华为云物联网平台,同时搭建流媒体服务器实现摄像头远程监控。最重要的是,本项目将利用Qt开发Android手机APP,使用户可以远程查看监测情况,了解农作物生成情况。

1万+

@NonNull系列注解,优雅解决令人发指的判空

空指针在Java中很常见。编码时对其的处理充斥这个整个文件,一个项目下来通常比重占到10%以上。如何优雅解决该问题呢?请看文章介绍

小子宝丁的博客 1万+

使用 MATLAB/Simulink 构建一个 核电站汽轮机与发电机协调控制系统 的仿真模型

在核电机组中,汽轮机-发电机系统是将核能最终转化为电能的关键环节。与火电机组不同,核反应堆的热惯性大、功率调节慢,因此汽轮机的控制策略需与反应堆特性相匹配,实现安全、稳定、高效的能量转换。维持发电机输出功率/频率稳定(响应电网调度)调节汽轮机进汽压力(匹配一回路蒸汽供应)实现“堆-机协调控制”保障机组在启停、变负荷、甩负荷等工况下的安全性本教程将带你使用构建一个核电站汽轮机与发电机协调控制系统汽轮机调速系统(调门控制)建模发电机与电网接口建模(同步机)

xiaoheshang_123的博客 188

【GlobalMapper精品教程】073:像素到点(Pixels-to-Points)从无人机图像轻松生成点云

Global Mapper v19引入的新的像素到点工具使用摄影测量原理,从重叠图像生成高密度点云、正射影像及三维模型。它使LiDAR模块成为已经功能很强大的的必备Global Mapper扩展功能。打开GlobalmapperV25.1中文版,点击【Lidar分析】→【像素到点云】,如下所示:V25之前的版本点击工具条上的【像素到点】工具:打开工具,可以【使用向导选】,或者选择【使用主窗口】,都很简单。

智慧空间实验室 1972

spring的@NonNull注解

spring的@NonNull注解

听海边涛声 1567

57、振荡扫描仪的挠性枢轴:原理、设计与材料选择

本文深入探讨了振荡扫描仪中挠性枢轴的工作原理、设计方法与材料选择。挠性枢轴因其无润滑、高精度、低周期性误差和潜在无限寿命等优势,广泛应用于光学、太空、医学和半导体等领域。文章分析了其在小角度下的优异性能与无法连续旋转等局限性,提供了关键设计公式,并对比了多种挠性材料的性能指标。同时,结合不同应用场景提出设计优化策略,并展望了微型化、智能化和高性能材料等未来发展趋势,为工程实践提供了系统的设计流程与选型建议。

numpy6sculptor的博客 95

codefirst 导航属性为null_我赌你不清楚Spring中关于Null的这些事

之前一直在某些代码中看到过使用@Nullable 标注过的注释,当时也没有在意到底是什么意思,之后忍不住去调查一番,这篇文章来谈谈Spring中关于Null的那些事。在Java中不允许你使用类型表示其null的安全性,但Spring Framework 现在在org.sprinngframework.lang包提供以下注释,以便声明API和字段的可空性:@Nullable: 用于指定参数、返回值或...

weixin_39934257的博客 167

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Spring 中的Null-Safety

之前一直在某些代码中看到过使用@Nullable 标注过的注释,当时也没有在意到底是什么意思,所以这篇文章来谈谈Spring中关于Null的那些事。 在Java中不允许让你使用类型表示其null的安全性,但Spring Framework 现在在org.sprinngframework.lang包提供以下注释,以便声明API和字段的可空性: @Nullable: 用于指定参数、返回值或...

weixin_30731305的博客 480

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Spring null安全注解

1.概述 从Spring 5开始,可以使用 null安全注解来帮助编写更安全的代码。 此功能称为“空安全性”,这是一组注解,其作用类似于监视潜在的空引用的安全措施。 空安全功能不是让摆脱不安全的代码,而是在编译时生成警告。 这样的警告可以防止在运行时发生灾难性的空指针异常(NPE)。 2.@NonNull注解 org.springframework.lang包中 在null安全功能的所有注解中,@NonNull注解是最重要的。 可以在需要对象引用的任何地方使用此注解声明非null约束:字段,方法参数或方

盲目的拾荒者的博客 1207

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

我赌你不清楚Spring中关于Null的这些事

点击上方“Java知音”,选择“置顶公众号”技术文章第一时间送达!推荐阅读(点击即可跳转阅读)1.淘宝服务端高并发分布式架构演进之路2.IntelliJ IDEA 从...

Java知音 1056

Java相关框架注解

1.Spring框架 @Nullable 允许为Null的对象,注解可用在的位置:方法,参数,字段 able可用的 @Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Nonnull(when = When.MAYBE) @TypeQualifierNickname public @interface Nullable

CsbLanca 332

7.Null-safety

一、基本说明 Java不允许用它的类型系统表示空安全性,Spring框架现在在org.springframework.lang包中提供了注解用于声明API和字段的可空性,目前暂不支持泛型类型参数、数组变量的元素参数的可空性 二、注解 @Nullable:用于指示特定参数、返回值或字段可以为null。 @NonNull:用于指示特定参数、返回值或字段不能为null(对于参数/返回值以及分别应用@NonNullApi和@NonNullFields的字段不需要)。 @NonNullApi:包级别的,它将

weixin_36730613的博客 156

spring-others

Null-safety spring在org.springframework.lang包中提供以下注解声明null相关API和字段 @Nullable: 指示参数,返回值,字段可为null. @NonNull:指示参数,返回值,字段不可为null 。 @NonNullApi: 指示参数,返回值不可为null 。 @NonNullFields: 指示字段不可为null . 不支持泛型参数,...

demon7552003的小本本 260

Spring Boot 4.0 Null-safety实战:从根因到编译期拦截NPE

空指针异常(NPE)是Java开发中最常见也最头疼的运行时错误,其根源在于类型系统无法区分变量是否允许为null。传统的防御手段如手动判空、Optional、静态检查工具,往往依赖团队自觉,难以形成统一规范。Spring Boot 4.0将Null-safety提升至框架层面,通过@NonNull、@Nullable与包级@NonNullApi注解,实现“默认非空+显式可空”的约束语义。借助编译期注解处理和IDE提示,开发者能在编码阶段就发现潜在的NPE风险,再配合Assert、ResponseStatus

weixin_42528191的博客 330

Spring 5 core 中的 @NonNull 是个什么鬼?!

说明 在Spring 5的 spring-core jar包中添加了 jsr-305 相关注解。在 Spring 源码中已经被大量使用。如下图: JSR-305介绍 诸如 FindBugs、IntelliJ、Checkstyle 和 PMD 这样的静态分析工具在 Java 开发中得到了广泛应用。这些工具都很强大,但是有一些共同的问题它们都很难解决。在 A...

weixin_34219944的博客 186

从零开始 Spring Boot 33:Null-safety

null安全)实际上是Java这个“古老”语言的历史包袱,很多新的语言(比如go或kotlin)在诞生起就在语言层面提供对null安全的解决方案。实际工作中有相当一部分bug都是“空指针异常”。Spring框架提供一些注解作为null安全这一问题的解决方案,可以通过在Spring框架中使用这些注解来在编码阶段尽早发现一部分“空指针异常”引起的bug。@Nullablenull@NonNullnull这些注解都属于包。

梦幻天空 908

getresourceasstream方法返回空_Java21Spring8Spring Nullsafety 空安全

Java不允许您使用其类型系统来表示null安全性,但是Spring Framework现在在org.springframework.lang包中提供了以下注释,以允许您声明API和字段的为空性:@Nullable:表示特定参数,返回值或字段可以为的注释null。@NonNull:表示不能指定特定参数,返回值或字段的注释null(分别不需要在参数/返回值和字段@NonNu...

weixin_39845347的博客 232

spring的@NonNullApi注解

spring的@NonNullApi注解

听海边涛声 1038
上一篇: Java多线程-实现Callable接口的call()方法,示例
下一篇: Java中 synchronized 修饰符及其示例
小鸡炖蘑菇Plus版
博客等级 码龄10年 10粉丝 27原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值