yj_root,lombok常用注解及其使用方法_YJ

目录
@Data、@Value
@Setter、@Getter、lombok.config
@Builder
@Singular和@Builder联合使用
@NoArgsConstructor、@AllArgsConstructor、@RequiredArgsConstructor
@ToStirng
@NotNull
@Accessors(chain = true):使用链式创建
@Synchronized、@SneakyThrows
@Cleanup: 关闭流、连接点
@Log
@EqualsAndHashCode
@UtilityClass
@ExtensionMethod
@FieldDefaults
以以下类为例: public class User {int id;String name;List list;} @Data、@Value@Data注解在类上,将类提供的所有属性都添加get、set方法,并添加equals、canEquals、hashCode、toString方法
@Value注解用于修饰类,相当于是@Data的不可变形式,因为字段都被修饰为private和final,默认的情况下不会生成settter。还有一点更狠的,默认类本身也是final的,不能被继承。
可以看一下这位老哥的帖子:https://blog.csdn.net/weixin_41540822/article/details/86606535
@Setter、@Getter、lombok.config给类添加set、get方法
参考帖子:https://blog.csdn.net/weixin_41540822/article/details/86606245
@Builder使用builder模式创建对象
//创建新的对象User aaa = User.builder().id(1).name("aaa").build();//修改原有对象的属性值;要求实体上添加@Builder(toBuilder=true)aaa = User.toBuilder().id(2).name("bbb).build(); @Singular和@Builder联合使用可以给集合更加方便的添加多条数据
@Singular(value = "list")List list; User aaa = User.builder().id(1).name("aaa").list("aaa").list("djsij").build();参考帖子地址:https://blog.csdn.net/weixin_41540822/article/details/86606562
@NoArgsConstructor、@AllArgsConstructor、@RequiredArgsConstructor可以创造一个无参构造器、或者有参构造器、第三个生成final或者@notnull修饰的无参或者有参构造器
也是这位老哥的帖子:https://blog.csdn.net/weixin_41540822/article/details/86606513
@ToStirng可以添加一个toString方法
@NotNull不能为空,否则抛出空指针异常
@Accessors(chain = true):使用链式创建 //添加注解@Data@Accessors(chain = true)//使用方法User aaa = new User().setId(1).setName("aaa"); @Synchronized、@SneakyThrows@Sychronized 是一个处理线程安全问题的annotation, 他的使用方法和关键字 synchronized比较类似,但是有一些不同点就是,关键字synchronized是锁定当前对象(this指针) , 而@Synchronized则会锁定一个private的常量。如果当前类中没有这个常量,就会自动生成一个
@Synchronizedpublic static void hello(){System.out.println("hello");}@Synchronizedpublic int hello2(){System.out.println("hello");return 1;}@Synchronizedpublic void hello3(){System.out.println("hello");}以下是它生成的方法
private static final Object $LOCK = new Object[0];private final Object $lock = new Object[0];private final Object readLock = new Object();public static void hello() {synchronized($LOCK) {System.out.println("hello");}}public int hello2() {synchronized($lock) {return 1;}}public void hello3() {synchronized(readLock) {System.out.println("hello");}}@SneakyThrows让你的代码拥有try....catch包裹
@SneakyThrowspublic static void throwException() {String str= null;String[] split = str.split(",");System.out.println(split);}实际上
public SneakyThrowsTest() {}public static void throwException() {try {String str = null;String[] split = ((String)str).split(",");System.out.println(split);} catch (Throwable var2) {throw var2;}} @Cleanup: 关闭流、连接点用于处理写入写出流的异常问题,可以让代码简洁
使用前:
public class Cleanup01 {public static void main(String[] args) throws IOException {InputStream in = new FileInputStream(args[0]);try {OutputStream out = new FileOutputStream(args[1]);try {byte[] b = new byte[1000];while (true) {int r = in.read(b);if (r == -1) break;out.write(b, 0, r);}} finally {if (out != null) {out.close();}}} finally {if (in != null) {in.close();}}}}使用后:
public class Cleanup01 {public static void main(String[] args) throws IOException {@Cleanup InputStream in = new FileInputStream(args[0]);@Cleanup OutputStream out = new FileOutputStream(args[1]);byte[] b = new byte[1000];while (true) {int r = in.read(b);if (r == -1) break;out.write(b, 0, r);}}} @Loghttps://blog.csdn.net/weixin_41540822/article/details/86606632
@EqualsAndHashCode重写equals和hashcode方法。
@UtilityClass官方文档是这么说的
创建实用程序类的注释。如果使用注释了一个类,则会@UtilityClass发生以下情况:
它被标记为最终。
如果在其中声明了任何构造函数,则会生成错误。否则,将生成一个私有的无参数构造函数。它抛出一个UnsupportedOperationException。
所有方法,内部类和类中的字段均标记为静态。
@ExtensionMethod设置父类
@FieldDefaults设置属性的使用范围,如private、public等,也可以设置属性是否被final修饰。