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

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

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

