中文:存取器
用于:配置getter和setter方法的生成结果
参考:@Accessors官方文档
参考:总结——》【Java】
一、Accessors源码
package lombok.experimental;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* A container for settings for the generation of getters and setters.
* <p>
* Complete documentation is found at <a href="https://projectlombok.org/features/experimental/Accessors">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 {};
}
二、Accessors属性
使用@Accessors时,fluent默认为false,chain默认false,prefix默认{}
在属性上增加注解,在类上的注解就会自动忽略=
import lombok.Data;
import lombok.experimental.Accessors;
/**
* @author Created by xiaoxian
* @version v1.0.0
*/
@Data
@Accessors
public class TestUser {
Long testUerId;
String testUserName;
}
1、fluent
如果fluent=true,那么chain默认为真。
@Accessors(fluent = true)
:
- 属性:testUerId
- getter:testUserId()
- setter:testUserId(T newValue):返回当前对象的实例
2、chain
set方法返回的是对象的实例,可以直接再使用set方法或者直接调用函数
@Accessors(chain = true)
- 属性:testUerId
- getter:getTestUserId()
- setter:setTestUserId(T newValue):返回当前对象的实例
3、prefix:默认{}
遵守驼峰命名,忽视指定前缀(匹配有前缀+大写字母这样的匹配)
@Accessors(prefix = "test")
- 属性:testUerId
- getter:getUserId()
- setter:setUserId(T newValue)