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 ...
随机推荐
- -bash: nslookup: 未找到命令;centos7 安装nslookup
一.安装服务 [root@localhost ~]# yum -y install bind-utils 二.查看 [root@localhost ~]# nslookup
- 斐波那契数python实现迭代循环两种方法
#递归方法 def fibona(n): if n == 0: return 0 elif n==1: return 1 else: return fibona(n - 1) + fibona(n - ...
- esxi虚拟机定时创建快照
1.vim-cmd vmsvc/getallvms 列出所有虚拟机信息 2.获取需要备份的虚拟机的Vmid 3.执行快照 vim-cmd vmsvc/snapshot.create Vmid $( ...
- 记一次mysql5.7保存Emoji表情
1.错误:SQLException; SQL state [HY000]; error code [1366]; Incorrect string value: '\xF0\x9F\x90\x96 \ ...
- centos7 双网卡同网段双网关配置
需求: #1.服务器为双网卡: #2.网卡1为互联网 172.16.137.99/24/254 #3.网卡2为旅游专网 172.16.137.97/24/1 #4.互联网路由器为172.16.137. ...
- C++实现链式表示多项式加法运算
#include<iostream>#include<cstdlib>using namespace std;#define MAXSIZE 100#define OK 1#d ...
- Python 获取IP地址
import socket def get_host_ip(): """ 查询本机ip地址 :return: """ try: s=sock ...
- vue的双向绑定规则
vue提供了v-model双向绑定指令,用来辅助在不操作DOM的前提下,快速读取表单的数据 <!DOCTYPE html> <html lang="en"> ...
- js - class 操作
js - class 操作 // 添加 function addClass(dom, classNameString = '') { if (!dom.className.length) dom.cl ...
- Qt中资源文件qrc中的路径访问
首先先看一下我们的qrc文件目录结构: 在文件系统中的目录结构是这样的: 请务必注意这边的前缀(按照网友推荐,大部分项目前缀都是只写一个"/"): 接下来进入正题,我们来分 ...