SpringBoot - Lombok使用详解2(@Setter、@Getter、@ToString、@EqualsAndHashCode)
四、Lombok 注解详解(2)
1,@Setter 和 @Getter
- 如果用在属性上:则只为该属性提供 setter 和 getter 方法
- 如果是用在类上:则为这个类所有属性供 setter 和 getter方法
package com.example.demo; import lombok.Getter;
import lombok.Setter; @Getter
@Setter
public class User {
private String name;
private Integer age;
}
(3)然后我们就可以直接使用它的 setter 和 getter 方法了:
User user = new User();
user.setName("hangge");
user.setAge(123);
user.getName();
user.getAge();
2,@Getter(lazy=true)
1 // 使用注解
2 public class GetterLazyExample {
3 @Getter(lazy=true) private final double[] cached = expensive();
4
5 private double[] expensive() {
6 double[] result = new double[1000000];
7 for (int i = 0; i < result.length; i++) {
8 result[i] = Math.asin(i);
9 }
10 return result;
11 }
12 }
13
14 // 不使用注解
15 public class GetterLazyExample {
16 private final java.util.concurrent.AtomicReference<java.lang.Object> cached =
17 new java.util.concurrent.AtomicReference<java.lang.Object>();
18
19 public double[] getCached() {
20 java.lang.Object value = this.cached.get();
21 if (value == null) {
22 synchronized(this.cached) {
23 value = this.cached.get();
24 if (value == null) {
25 final double[] actualValue = expensive();
26 value = actualValue == null ? this.cached : actualValue;
27 this.cached.set(value);
28 }
29 }
30 }
31 return (double[])(value == this.cached ? null : value);
32 }
33
34 private double[] expensive() {
35 double[] result = new double[1000000];
36 for (int i = 0; i < result.length; i++) {
37 result[i] = Math.asin(i);
38 }
39 return result;
40 }
41 }
3,@ToString
(1)@ToString 注解在类上, 为类提供 toString() 方法:
- 默认情况下,它会按顺序(以逗号分隔)打印这个类名称以及每个字段。
- 可以这样设置不包含哪些字段:@ToString(exclude = "id") 或者 @ToString(exclude = {"id","name"})
1 package com.example.demo;
2
3 import lombok.Getter;
4 import lombok.Setter;
5 import lombok.ToString;
6
7 @Getter
8 @Setter
9 @ToString
10 public class User {
11 private String name;
12 private Integer age;
13 }
(2)下面是一个简单的测试样例:
1 User user = new User();
2 user.setName("hangge");
3 user.setAge(123);
4 System.out.println(user.toString());

4,@EqualsAndHashCode
- 默认情况下,它将使用所有非静态,非 transient 字段。
- 可以通过在可选的 exclude 参数中来排除更多字段。
- 也可以通过在 parameter 参数中命名它们来准确指定希望使用哪些字段。
1 package com.amos.lombok;
2
3 import lombok.EqualsAndHashCode;
4
5 @EqualsAndHashCode
6 public class EqualsAndHashCodeExample {
7
8 private transient int transientVar = 10;
9 private String name;
10 private double score;
11
12 /**
13 * 不包含该字段
14 */
15 @EqualsAndHashCode.Exclude
16 private Shape shape = new Square(5, 10);
17 private String[] tags;
18
19 /**
20 * 不包含该字段
21 */
22 @EqualsAndHashCode.Exclude
23 private int id;
24
25 public String getName() {
26 return this.name;
27 }
28
29 @EqualsAndHashCode(callSuper = true)
30 public static class Square extends Shape {
31 private final int width, height;
32
33 public Square(int width, int height) {
34 this.width = width;
35 this.height = height;
36 }
37 }
38
39 public static class Shape {
40 }
41 }
(2)上面编译后会变成如下代码:
1 package com.amos.lombok;
2
3 import java.util.Arrays;
4
5 public class EqualsAndHashCodeExample {
6 private transient int transientVar = 10;
7 private String name;
8 private double score;
9 private EqualsAndHashCodeExample.Shape shape = new EqualsAndHashCodeExample.Square(5, 10);
10 private String[] tags;
11 private int id;
12
13 public EqualsAndHashCodeExample() {
14 }
15
16 public String getName() {
17 return this.name;
18 }
19
20 public boolean equals(final Object o) {
21 if (o == this) {
22 return true;
23 } else if (!(o instanceof EqualsAndHashCodeExample)) {
24 return false;
25 } else {
26 EqualsAndHashCodeExample other = (EqualsAndHashCodeExample)o;
27 if (!other.canEqual(this)) {
28 return false;
29 } else {
30 label31: {
31 Object this$name = this.getName();
32 Object other$name = other.getName();
33 if (this$name == null) {
34 if (other$name == null) {
35 break label31;
36 }
37 } else if (this$name.equals(other$name)) {
38 break label31;
39 }
40
41 return false;
42 }
43
44 if (Double.compare(this.score, other.score) != 0) {
45 return false;
46 } else {
47 return Arrays.deepEquals(this.tags, other.tags);
48 }
49 }
50 }
51 }
52
53 protected boolean canEqual(final Object other) {
54 return other instanceof EqualsAndHashCodeExample;
55 }
56
57 public int hashCode() {
58 int PRIME = true;
59 int result = 1;
60 Object $name = this.getName();
61 int result = result * 59 + ($name == null ? 43 : $name.hashCode());
62 long $score = Double.doubleToLongBits(this.score);
63 result = result * 59 + (int)($score >>> 32 ^ $score);
64 result = result * 59 + Arrays.deepHashCode(this.tags);
65 return result;
66 }
67
68 public static class Shape {
69 public Shape() {
70 }
71 }
72
73 public static class Square extends EqualsAndHashCodeExample.Shape {
74 private final int width;
75 private final int height;
76
77 public Square(int width, int height) {
78 this.width = width;
79 this.height = height;
80 }
81
82 public boolean equals(final Object o) {
83 if (o == this) {
84 return true;
85 } else if (!(o instanceof EqualsAndHashCodeExample.Square)) {
86 return false;
87 } else {
88 EqualsAndHashCodeExample.Square other = (EqualsAndHashCodeExample.Square)o;
89 if (!other.canEqual(this)) {
90 return false;
91 } else if (!super.equals(o)) {
92 return false;
93 } else if (this.width != other.width) {
94 return false;
95 } else {
96 return this.height == other.height;
97 }
98 }
99 }
100
101 protected boolean canEqual(final Object other) {
102 return other instanceof EqualsAndHashCodeExample.Square;
103 }
104
105 public int hashCode() {
106 int PRIME = true;
107 int result = super.hashCode();
108 result = result * 59 + this.width;
109 result = result * 59 + this.height;
110 return result;
111 }
112 }
113 }
SpringBoot - Lombok使用详解2(@Setter、@Getter、@ToString、@EqualsAndHashCode)的更多相关文章
- Lombok使用详解(转)
本文转自https://blog.csdn.net/u010695794/article/details/70441432 2017年04月22日 15:17:00 阅读数:10394 Lombok使 ...
- Lombok 使用详解,简化Java编程
前言 在 Java 应用程序中存在许多重复相似的.生成之后几乎不对其做更改的代码,但是我们还不得不花费很多精力编写它们来满足 Java 的编译需求 比如,在 Java 应用程序开发中,我们几乎要为所有 ...
- Springboot mini - Solon详解(六)- Solon的校验框架使用、定制与扩展
Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...
- SpringBoot之DispatcherServlet详解及源码解析
在使用SpringBoot之后,我们表面上已经无法直接看到DispatcherServlet的使用了.本篇文章,带大家从最初DispatcherServlet的使用开始到SpringBoot源码中Di ...
- SpringBoot Profile使用详解及配置源码解析
在实践的过程中我们经常会遇到不同的环境需要不同配置文件的情况,如果每换一个环境重新修改配置文件或重新打包一次会比较麻烦,Spring Boot为此提供了Profile配置来解决此问题. Profile ...
- Spring全家桶——SpringBoot之AOP详解
Spring全家桶--SpringBoot之AOP详解 面向方面编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象编程(OOP). OOP中模块化的关键单元是类,而在AOP中,模块化单元是方 ...
- Springboot mini - Solon详解(四)- Solon的事务传播机制
Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...
- Springboot mini - Solon详解(二)- Solon的核心
Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...
- Springboot mini - Solon详解(三)- Solon的web开发
Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...
- Springboot mini - Solon详解(五)- Solon扩展机制之Solon Plugin
Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...
随机推荐
- flutter Color和colors
"Color(颜色)"是我们在设置应用程序界面样式时最常用到的属性. Flutter 中颜色设置的方式有很多种,其中最常用的有下列几种: Color c1 = Color(0xFF ...
- NDK 减少 so 库体积方法总结
. 使用 strip 使用 NDK toolchain 可以把调试的 C++ 符号表(Symbol Table)中数据删除,我们一般我们打成 APK 会自动帮我们做这个工作,当然也可以手动设置: 手动 ...
- java okio 找不到的问题
问题描述: okio 找不到的问题 解决办法: 下载 jar_files.zip 在idea-File-Project Structure- Project Settings - Libraries ...
- Linux下hadoop和spark的基础环境配置准备
我使用了4台虚拟机centos7来搭建环境, 2个主结点(一般是一个,但为了体验zookeeper,万一其中一个master挂掉呢,另外一个会自动启动接管), 2个从结点 注意:下面的配置主要以主节点 ...
- mybatis-关联查询3-自关联查询
一对多的方式处理 查询指定栏目的所有子孙栏目 查询指定栏目及其所有子孙栏目 多对 一的方式处理
- 全国分乡镇第六次人口普查数据shp数据库省市区县街道
全国分乡镇第六次人口普查数据shp数据库省市区县街道 ==名称:全国第六次人口普查任意十个乡镇的乡镇界地图==数据说明:精确到乡镇级别.==数据:面数据,属性列表(乡镇名.乡镇代码,第六次人口普查分乡 ...
- WC2023 游记
不是很会写游记,随便写写吧. 一些附件 讲课资料合集(压缩后 \(\rm 31MB\))太大了,可以去 U 群下载. 由于后面很多乐子,我把相关内容打包成 zip 上传上来了. 乐子合集下载链接.(这 ...
- Mysql数据库基础第三章:DML语言
Mysql数据库基础系列 软件下载地址 提取码:7v7u 数据下载地址 提取码:e6p9 mysql数据库基础第一章:(一)数据库基本概念 mysql数据库基础第一章:(二)mysql环境搭建 mys ...
- JS数组的交集与差集
有两个数组arr1,arr2 实现arr2中去除arr1相同的元素 e.g arr1=[1,2,3] arr2=[2,3,4] ===> result = [4] 实现 获取两个数组(arr1, ...
- 2022-04-19内部群每日三题-清辉PMP
1.一个项目的完工预算(BAC)为100,000美元.目前的项目报告显示该项目符合进度和预算.一位团队成员对相关工作包进行自下而上估算,表明剩余工作将比原计划少用10,000美元,并且仍能按进度计划交 ...