SpringBoot - Lombok使用详解4(@Data、@Value、@NonNull、@Cleanup)
六、Lombok 注解详解(4)
8,@Data
1 package com.example.demo;
2
3 import lombok.Data;
4
5 @Data
6 public class User {
7 private String name;
8 private Integer age;
9 }
(2)上面的 @Data 等效于如下几个注解结合使用:
1 package com.example.demo;
2
3 import lombok.*;
4
5 @Setter
6 @Getter
7 @ToString
8 @EqualsAndHashCode
9 @NoArgsConstructor
10 public class User {
11 private String name;
12 private Integer age;
13 }
9,@Value
1 // 使用注解
2 @Value
3 public class ValueExample {
4 String name;
5 @Wither(AccessLevel.PACKAGE) @NonFinal int age;
6 double score;
7 protected String[] tags;
8 }
9
10 // 不使用注解
11 public final class ValueExample {
12 private final String name;
13 private int age;
14 private final double score;
15 protected final String[] tags;
16
17 public ValueExample(String name, int age, double score, String[] tags) {
18 this.name = name;
19 this.age = age;
20 this.score = score;
21 this.tags = tags;
22 }
23
24 //下面省略了其它方法
25 //.....
26 }
10,@NonNull
1 package com.example.demo;
2
3 import lombok.NonNull;
4
5 public class NonNullExample {
6 private String name;
7
8 public NonNullExample(@NonNull User user) {
9 this.name = user.getName();
10 }
11 }
(2)上面相当与如下 Java 代码:
1 package com.example.demo;
2
3 public class NonNullExample {
4 private String name;
5
6 public NonNullExample(User user) {
7 if (user == null) {
8 throw new NullPointerException("user");
9 }
10 this.name = user.getName();
11 }
12 }
(3)下面是一个简单的测试样例:
1 User user = null;
2 try {
3 NonNullExample example = new NonNullExample(user);
4 }catch (NullPointerException ex) {
5 return ex.toString();
6 }
11,@Cleanup
1 public class CleanupExample {
2 public static void main(String[] args) throws IOException {
3 @Cleanup InputStream in = new FileInputStream(args[0]);
4 @Cleanup OutputStream out = new FileOutputStream(args[1]);
5 byte[] b = new byte[10000];
6 while (true) {
7 int r = in.read(b);
8 if (r == -1) break;
9 out.write(b, 0, r);
10 }
11 }
12 }
(2)上面相当与如下传统的 Java 代码:
1 public class CleanupExample {
2 public static void main(String[] args) throws IOException {
3 InputStream in = new FileInputStream(args[0]);
4 try {
5 OutputStream out = new FileOutputStream(args[1]);
6 try {
7 byte[] b = new byte[10000];
8 while (true) {
9 int r = in.read(b);
10 if (r == -1) break;
11 out.write(b, 0, r);
12 }
13 } finally {
14 if (out != null) {
15 out.close();
16 }
17 }
18 } finally {
19 if (in != null) {
20 in.close();
21 }
22 }
23 }
24 }
SpringBoot - Lombok使用详解4(@Data、@Value、@NonNull、@Cleanup)的更多相关文章
- 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 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之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的web开发
Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...
随机推荐
- xshell拖拽文件
直接在linux中输入命令 yum install lrzsz 安装完毕后即可拖拽文件.
- jdk 13 添加 jre
问题: 安装 jdk 13 版本后发现没有 jre . 解决方法: 1.进入 jdk 安装目录(如:D:\Program\Java\jdk-13.0.2\) 2.在 jdk 安装目录打开命令行,输入以 ...
- IIS部署HTTPS站点
常用的IIS大体有二个版本: IIS8和IIS7,分别有不同的配置方法如下: IIS8.5以上版本 1).新建一个站点,切记尽量不要与旧http协议站点共用一个站点,容易冲突 2).先将https证书 ...
- springsecurity maven 打包后,404错误。maven 打包后,加载内置的xml文件
404错误,解决的办法,主要是pom文件 <build> <resources> <resource> <directory>src/main/reso ...
- AVL tree rotate
AVL tree single rotate /** * Rotate binary tree node with left child. * For AVL trees, this is a sin ...
- 使用layui时遇到的问题以及解决文章链接
1.斜线表头效果 2.表格嵌套使用 3.layui数据表格跨行自动合并 4.layui表格数据变更的处理方式 5.layer弹窗动态添加KindEditor编辑器 6.layer弹出层自动调节位置 7 ...
- vue data functions should return an object
报错: 原因:data里没写return{}
- MySQL系列-详解mysql数据类型
MySQL数据类型 (1)数值类型 1.整数型 2.浮点型 3.定点型 (2)日期时间类型 (3)字符串类型 MySQL字段属性 1.空\不为空值:NULL.NOT NULL 2.主键:primary ...
- 模态框拖拽案例分析--元素偏移量 offset 系列
弹出框,我们也称为模态框. 模态框拖拽案例分析: (1)点击弹出层, 会弹出模态框, 并且显示灰色半透明的遮挡层. (2)点击关闭按钮,可以关闭模态框,并且同时关闭灰色半透明遮挡层. (3)鼠标放到模 ...
- Win10系统删除文件需提供管理员权限-- 解决方案
解决方案1:选中[文件]-[属性]-[安全]-[高级]-选中当前用户[编辑]权限 若还是不行,则试试方案2解决方案2:更改[所有者]--[高级]--[立即查找] 选中[everyone]--[确定] ...