lombok学习笔记
首先想知道详细的关系,请去阅读官网文档,一下均是个人化的总结,仅供参考。
@getter和@setter:
注在类上就是为所有属性添加get和set方法,注在属性上就是为某一个属性添加get和set方法,同时get方法对于boolean值改为is,这是Boolean值本身get的规则。
@ToString(callSuper = false,exclude = {},of={},includeFieldNames = true):
注在类上,等于自动生成toString方法,callSuper是是否输出父类的,exlude是排除不需要输出的属性,of是仅包含哪些属性,includeFieldNames是手否包含属性的名字。以上四个个是常用属性和默认值。
@EqualsAndHashCode(callSuper = false,exclude = {},of={}):
注在类上,自动生成equals和hashCode方法,同样有上面这三个常用属性。
@Data(staticConstructor = "of"):
注在类上,等效于同时添加@Getter、@Setter、@ToString、@EqualsAndHashCode这四个注释,同时如果过不带参数,还会生成一个无参构造,如果带参数staticConstructor="of"那么会生成一个私有的无参构造,同时还有一个方法名为of的静态构造函数。最后, @Data生成的这些东西会被框架中其他颗粒度更小的注解覆盖。
@Cleanup:
注在流一类的参数上,然后之后使用该参数的一系列代码均会被try finally包裹,并且会在finally中执行该参数的close方法,或者可以添加value参数来指定参数名的方法。
@Synchronized:
上锁,同步功能,等同于给某个对象自动添加一个多对象,具体使用不太理解
@SneakyThrows(value = Exception.class):
包裹异常使用的,具体使用还不太了解
基础介绍之外的内容:
@nonnull:
注在属性上则为其相应的构造函数和set方法添加null值判断,如果为null则抛出NullPointerException(空指针异常)。(不可以注在类上)
@Value:
一种特殊的@Data,将类中所有的属性改为private final,同时修改类为final,同时添加一个全参的构造函数,提供get、euqals、hashcode和toString方法,不提供set方法。
@Builder:
官网是如此介绍的“@builder是构建API坚实的基础”,好吧,耐力有限,且本人暂时好像用不到,文档实在是读不下去。
@Log (and friends):
主要有如下这几个,用到哪个就用哪个注解:
- @CommonsLog
private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class); - @Flogger
private static final com.google.common.flogger.FluentLogger log = com.google.common.flogger.FluentLogger.forEnclosingClass(); - @JBossLog
private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class); - @Log
Creates
private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName()); - @Log4j
private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class); - @Log4j2
private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class); - @Slf4j
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class); - @XSlf4j
private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);
所以log对象默认是log,需要修改lombok配置项,我暂时还不会。