详解Java中@Accessors注解(全)
创始人
2024-11-05 00:36:32
0

目录

  • 前言
  • 1. 概念
  • 2. 属性
    • 2.1 fluent属性
    • 2.2 chain属性
    • 2.3 prefix属性

前言

关于该注解的学习,主要来源项目中涉及,对此进行查漏补缺

@Accessors 注解通常用于简化实体类的代码,使其更加简洁和易读。

1. 概念

@Accessors 是 Lombok(一种Java库)提供的注解之一,用于自动生成 getter 和 setter 方法,并可以配置一些属性。

以下是关于 @Accessors 注解的详细解释

常用属性:

  • fluent:如果设置为 true,生成的 getter 方法会移除 get 前缀,setter 方法移除 set 前缀。
  • chain:如果设置为 true,生成的 setter 方法会返回当前对象,支持方法链调用。
  • prefix:为生成的 getter 和 setter 方法添加指定前缀。

类似如下例子:

import lombok.AccessLevel; import lombok.Setter; import lombok.ToString; import lombok.experimental.Accessors;  @ToString @Accessors(chain = true, fluent = true) public class Example {     @Setter(AccessLevel.PROTECTED)     private String name;      private int age; } 

在上面的例子中,@Accessors 注解配置了 chain = true 和 fluent = true,表示生成的 setter 方法支持方法链调用,并移除了 get 和 set 前缀。

通过源码也可看出其配置的属性:

/**  * A container for settings for the generation of getters and setters.  * 

* Complete documentation is found at the project lombok features page for @Accessors. *

* Using this annotation does nothing by itself; an annotation that makes lombok generate getters and setters, * such as {@link lombok.Setter} or {@link lombok.Data} is also required. */ @Target({ElementType.TYPE, ElementType.FIELD}) @Retention(RetentionPolicy.SOURCE) public @interface Accessors { /** * If true, accessors will be named after the field and not include a {@code get} or {@code set} * prefix. If true and {@code chain} is omitted, {@code chain} defaults to {@code true}. * default: false * * @return Whether or not to make fluent methods (named {@code fieldName()}, not for example {@code setFieldName}). */ boolean fluent() default false; /** * If true, setters return {@code this} instead of {@code void}. * default: false, unless {@code fluent=true}, then default: true * * @return Whether or not setters should return themselves (chaining) or {@code void} (no chaining). */ boolean chain() default false; /** * If present, only fields with any of the stated prefixes are given the getter/setter treatment. * Note that a prefix only counts if the next character is NOT a lowercase character or the last * letter of the prefix is not a letter (for instance an underscore). If multiple fields * all turn into the same name when the prefix is stripped, an error will be generated. * * @return If you are in the habit of prefixing your fields (for example, you name them {@code fFieldName}, specify such prefixes here). */ String[] prefix() default {}; }

2. 属性

默认fluent、chain 都是false
对于false,其设定的值跟往常差不多!

举例如下:(主要为了区分fluent、chain以及prefix三个属性)

@Data //@AllArgsConstructor //@NoArgsConstructor @TableName("test_user1") @Accessors(chain = false,fluent = false) public class User1 {     @TableId(value = "id", type = IdType.AUTO)     private int xxId;     private String yyUserName;     private int zzAge;     // 其他字段...      public static void main(String[] args) {         User1 user1 = new User1();          user1.setXxId(123);         user1.setYyUserName("manong");         user1.setZzAge(123);          System.out.println(user1); // User1(xxId=123, yyUserName=manong, zzAge=123)         System.out.println(user1.getZzAge()); // 123      } } 

截图如下:

在这里插入图片描述

2.1 fluent属性

为了方便测试,原先fluent默认就是false,当修改为true的时候:

@Data @TableName("test_user1") @Accessors(fluent = true) public class User1 {     @TableId(value = "id", type = IdType.AUTO)     private int id;     private String username;     private int age;     // 其他字段...      public static void main(String[] args) {         User1 user1 = new User1();          System.out.println(user1.id()); // 这个返回的值是int值,因为id为int类型         System.out.println(user1.id(123)); // 这个返回的对象值是类          System.out.println(user1.id()); // 再次看看id的属性为,123         System.out.println(user1.age()); // 查看其age属性,发现为0        } } 

截图如下:

在这里插入图片描述

对应的属性有如下:

  • 返回属性值
  • 返回对象

可以通过得到对象再去检查其他的属性:

在这里插入图片描述

2.2 chain属性

chain的区别在于可以链式设定值!

代码如下:

@Data @TableName("test_user1") @Accessors(chain = true) public class User1 {     @TableId(value = "id", type = IdType.AUTO)     private int id;     private String username;     private int age;     // 其他字段...      public static void main(String[] args) {         User1 user1 = new User1();  //        System.out.println(user1.setId(123)); // 返回对象         user1.setAge(123).setUsername("manong");         System.out.println(user1); // User1(id=0, username=manong, age=123)         System.out.println(user1.getAge()); // 123           User1 user2 = new User1().setAge(333).setUsername("yanjiuseng");         System.out.println(user2); // User1(id=0, username=yanjiuseng, age=333)       } } 

截图如下:

在这里插入图片描述

2.3 prefix属性

注意属性中的前缀后要开头大写!此处的前缀必须为string类型
比如id属性,为了加一个前缀xx,则属性值应该为xxId,如果为xxid代码会错!

代码如下:

@Data @TableName("test_user1") @Accessors(prefix = {"xx","yy","zz"}) public class User1 {     @TableId(value = "id", type = IdType.AUTO)     private int xxId;     private String yyUserName;     private int zzAge;     // 其他字段...      public static void main(String[] args) {         User1 user1 = new User1();          user1.setId(123);         user1.setUserName("manong");         user1.setAge(123);          System.out.println(user1); // User1(xxId=123, yyUserName=manong, zzAge=123)         System.out.println(user1.getAge()); // 123      } } 

截图如下:

在这里插入图片描述

相关内容

热门资讯

近期!hhpoker有后台操控... 近期!hhpoker有后台操控吗,开心泉州小程序辅助哪里查看,法子教程(有人有挂)1、起透看视 开心...
据报道!wepoker有辅助工... 据报道!wepoker有辅助工具吗,方片十三张脚本,烘培教程(有挂详细)1、全新机制【方片十三张脚本...
2026版教学!newpoke... 2026版教学!newpoker怎么安装脚本,微信随意玩辅助器,模块教程(有挂总结)1、完成微信随意...
2026版辅助挂!约局吧辅助器... 2026版辅助挂!约局吧辅助器,老k游戏辅助器,技法教程(有挂详细)1、任何老k游戏辅助器透视是真的...
日前!wpk透视辅助靠谱吗,家... 日前!wpk透视辅助靠谱吗,家乡大二有没有挂,手筋教程(有挂方式)家乡大二有没有挂破解侠是真的助透视...
据了解!pokernow辅助控... 据了解!pokernow辅助控制,sohoo竞技联盟辅助,资料教程(有挂辅助)1、sohoo竞技联盟...
据悉!德州局怎么透视,微信小程... 据悉!德州局怎么透视,微信小程序辅助器免费下载,讲义教程(有挂秘籍)微信小程序辅助器免费下载辅助器是...
2026版规律!wpk真的有透... 2026版规律!wpk真的有透视嘛,透视盒子,方式教程(有挂方法)1、透视盒子破解器简单,透视盒子机...
近日!购买的wpk辅助在哪里下... 近日!购买的wpk辅助在哪里下载,微信开发辅助工具,步骤教程(讲解有挂)1、微信开发辅助工具免费脚本...
黑科技辅助挂!aapoker辅... 黑科技辅助挂!aapoker辅助器怎么用,微友三代辅助,方案教程(有挂详细)1、实时微友三代辅助透视...