Java中@Accessors注解的具體使用
前言
關(guān)于該注解的學(xué)習(xí),主要來源項(xiàng)目中涉及,對此進(jìn)行查漏補(bǔ)缺
@Accessors 注解通常用于簡化實(shí)體類的代碼,使其更加簡潔和易讀。
1. 概念
@Accessors 是 Lombok(一種Java庫)提供的注解之一,用于自動生成 getter 和 setter 方法,并可以配置一些屬性。
以下是關(guān)于 @Accessors 注解的詳細(xì)解釋
常用屬性:
- fluent:如果設(shè)置為 true,生成的 getter 方法會移除 get 前綴,setter 方法移除 set 前綴。
- chain:如果設(shè)置為 true,生成的 setter 方法會返回當(dāng)前對象,支持方法鏈調(diào)用。
- 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 方法支持方法鏈調(diào)用,并移除了 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. 屬性
默認(rèn)fluent、chain 都是false
對于false,其設(shè)定的值跟往常差不多!
舉例如下:(主要為了區(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默認(rèn)就是false,當(dāng)修改為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值,因?yàn)閕d為int類型
System.out.println(user1.id(123)); // 這個返回的對象值是類
System.out.println(user1.id()); // 再次看看id的屬性為,123
System.out.println(user1.age()); // 查看其age屬性,發(fā)現(xiàn)為0
}
}
截圖如下:

對應(yīng)的屬性有如下:
- 返回屬性值
- 返回對象
可以通過得到對象再去檢查其他的屬性:

2.2 chain屬性
chain的區(qū)別在于可以鏈?zhǔn)皆O(shè)定值!
代碼如下:
@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,則屬性值應(yīng)該為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
}
}
截圖如下:

到此這篇關(guān)于Java中@Accessors注解的具體使用的文章就介紹到這了,更多相關(guān)Java @Accessors注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Maven的porfile與SpringBoot的profile結(jié)合使用案例詳解
這篇文章主要介紹了Maven的porfile與SpringBoot的profile結(jié)合使用,通過maven的profile功能,在打包的時候,通過-P指定maven激活某個pofile,這個profile里面配置了一個參數(shù)activatedProperties,不同的profile里面的這個參數(shù)的值不同,需要的朋友可以參考下吧2021-12-12
Java中輕量級http開發(fā)庫Unirest使用及實(shí)用技巧
Unirest for Java 是一個輕量級、易于使用的 HTTP 客戶端庫,旨在簡化 Java 應(yīng)用程序中的 HTTP 請求發(fā)送和響應(yīng)處理,本文詳細(xì)介紹它的主要特性、基本用法以及一些實(shí)用技巧,感興趣的朋友跟隨小編一起看看吧2025-10-10
Java LinkedList的實(shí)現(xiàn)原理圖文詳解
今天小編就為大家分享一篇關(guān)于Java LinkedList的實(shí)現(xiàn)原理圖文詳解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01
SpringBoot集成內(nèi)存數(shù)據(jù)庫hsqldb的實(shí)踐
hsqldb只需要添加對應(yīng)的依賴,然后在配置文件進(jìn)行配置。不需要安裝一個數(shù)據(jù)庫,本文就來介紹一下具體使用,感興趣的可以了解一下2021-09-09
Java實(shí)現(xiàn)字符串反轉(zhuǎn)的常用方法小結(jié)
在Java中,你可以使用多種方法來反轉(zhuǎn)字符串,這篇文章主要為大家整理了幾種常見的反轉(zhuǎn)字符串的方法,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
java創(chuàng)建多級目錄文件的實(shí)例講解
下面小編就為大家分享一篇java創(chuàng)建多級目錄文件的實(shí)例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
查看JDK安裝路徑,一臺電腦安裝多個版本的JDK并切換使用方式
文章介紹了如何查看JDK安裝路徑以及如何在一臺電腦上安裝和切換多個版本的JDK(JDK?8、JDK?11、JDK?17),內(nèi)容涵蓋了通過命令行查看JDK路徑的方法,以及如何下載、安裝和配置多個JDK版本,并通過修改環(huán)境變量來切換JDK版本2025-12-12

