In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
The editor will share with you the sample analysis of Null in Spring. I hope you will get something after reading this article. Let's discuss it together.
I've seen comments marked with @ Nullable in some code before, and I didn't care what it meant, so I couldn't help but look into it. This article is about Null in Spring.
You are not allowed to use types to indicate the security of their null in Java, but Spring Framework now provides the following comments in the org.sprinngframework.lang package to declare the nullability of API and fields:
Nullable: used to specify parameters, return values, or fields that can be used as comments for null.
@ NonNull: contrary to the above comment, it indicates that the specified parameter, return value, or field is not allowed to be null. (parameters / return values and fields applicable to @ NonNullApi and @ NonNullFields are not required)
NonNullApi: comments at the package level declare non-null as parameters and return values.
@ NonNullFields: comment declaration fields at the package level are not empty by default
Spring Framework itself takes advantage of the above annotations, but they can also be used in any Spring-based Java project to declare empty security api and empty security fields. The nullability of generics and array elements is not yet supported, but they should also be supported in later releases.
Spring Null-Safety appears in Spring5, making it easier for us to write empty and safe code, which is called null-safety,null-safety not to escape unsafe code, but to generate warnings at compile time. Such warnings prevent catastrophic null pointer exceptions (NPE) at run time.
@ NonNull
The @ NonNull annotation is the most important of all annotations in null-safety, and we can use this annotation to declare a non-null constraint anywhere we expect an object reference: a field, a method parameter, or a method return value.
Let's take a look at an example.
Public class Student {private String name; public String getName () {return name;} public void setName (String name) {if (null! = null & & name.isEmpty ()) {name = null;} this.name = name;}}
The above code is valid for name checking, but there is a flaw. If name is set to null, then when we use name, it ends with NullPointerException.
Use @ NonNull
Spring's null-safety feature allows idea or eclipse to report this potential threat, for example, if we add @ NonNull to the attribute with IDEA.
Strange, there is no change, ah, do not see any potential security tips ah, that is because you did not set up in idea.
Set up security check
If you are not prompted, you can set up a security check in the following ways:
If it doesn't work, add the jar package where @ NonNull and @ Nullable are located on the right side of configuration annotations, as follows:
Add it and type ✅ to see the following effect.
Now fullName has been annotated by @ NonNull to add the compiler's ability to check for null values!
To test it, you can remove the @ NonNull annotation, and then place your mouse over the fullName, and you won't have this prompt.
@ NonNullFields
The @ NonNull annotation can help you ensure null-safety. However, if this comment decorates all the fields directly, it will pollute the entire code base.
Spring provides another annotation that is not allowed to be null-@ NonNullFields. This annotation is suitable for use at the package level, notifying our development tools of all fields in the annotation package. Null is not allowed by default.
Create a new Parent class and create a class named package-info.java under the package to which it belongs. Instead of creating a Java class, you create a file named package-info.java, as follows
Package-info.java
@ NonNullFields package com.nullsafety.demo.pojo; import org.springframework.lang.NonNullFields
Create a new Parent.java class
Public class Parent {private String son; private String age; private String name; public void setSon (String son) {if (son! = null & & son.isEmpty ()) {son = null;} this.son = son;} public void setAge (String age) {if (age! = null & & age.isEmpty ()) {age = null;} this.age = age } public void setName (String name) {if (name! = null & & name.isEmpty ()) {name = null;} this.name = name;}}
@ NonNullFields in package-info.java can work on all the properties in the Parent class. If you mouse over any attribute, you will be prompted to check at compile time.
@ Nullable
The @ NonNullFields annotation is usually better than @ NonNull because it helps reduce templates. However, sometimes we want to exempt certain fields from the non-null constraints specified at the package level, so we use the @ Nullable annotation
Person.java,Person.java and pack-info.java are in the same package.
Public class Person {@ NonNull private String fullName; @ Nullable private String nickName; public String getNickName () {return nickName;} public void setNickName (String nickName) {if (nickName! = null & & nickName.isEmpty ()) {nickName = null;} this.nickName = nickName;} public String getFullName () {return fullName } public void setFullName (String fullName) {if (fullName! = null & & fullName.isEmpty ()) {fullName = null;} this.fullName = fullName;}}
In this case, we use the @ Nullable annotation to override the semantics of @ NonNullFields on the field.
@ NonNullApi
The @ NonNullFields annotation applies only to the fields indicated by its name. If we want to have the same effect on the parameters of the method and the return value, we need @ NonNullApi.
Add @ NonNullApi and @ NonNullFields to configure annotations and select NonNullApi
Like @ NonNullFields, we need to define @ NonNullApi in package-info.java
Package-info.java
@ NonNullApi @ NonNullFields package com.nullsafety.demo.pojo; import org.springframework.lang.NonNullApi; import org.springframework.lang.NonNullFields
The effect of adding the following comments is as follows: you can accept the compile-time prompt when you return the value.
After reading this article, I believe you have some understanding of "sample Analysis of Null in Spring". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.