Java中@Accessors注解的具體使用
前言
關于該注解的學習,主要來源項目中涉及,對此進行查漏補缺
@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. * <p> * Complete documentation is found at <a rel="external nofollow" >the project lombok features page for @Accessors</a>. * <p> * 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}. * <strong>default: false</strong> * * @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}. * <strong>default: false</strong>, unless {@code fluent=true}, then <strong>default: true</strong> * * @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,其設定的值跟往常差不多!
舉例如下:(主要為了區(qū)分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屬性,發(fā)現(xiàn)為0 } }
截圖如下:
對應的屬性有如下:
- 返回屬性值
- 返回對象
可以通過得到對象再去檢查其他的屬性:
2.2 chain屬性
chain的區(qū)別在于可以鏈式設定值!
代碼如下:
@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 } }
截圖如下:
到此這篇關于Java中@Accessors注解的具體使用的文章就介紹到這了,更多相關Java @Accessors注解內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java中java.lang.ClassCastException異常原因及解決方法
大家好,本篇文章主要講的是Java中java.lang.ClassCastException異常原因及解決方法,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01Java try-catch-finally異常處理機制詳解
這篇文章主要介紹了Java try-catch-finally異常處理機制詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-08-08淺析Java中print、printf、println的區(qū)別
以下是對Java中print、printf、println的區(qū)別進行了詳細的分析介紹,需要的朋友可以過來參考下2013-08-08