转载地址:https://blog.csdn.net/sunsfan/article/details/53542374
1.
@NonNUll
用在方法或者构造函数的参数前面,校验非空
2.
@ClearUp
用在需要关闭资源的变量前面,默认调用close() 方法
public static void main(String[] args) throws IOException {
@Cleanup FileInputStream in=new FileInputStream("/work/auth.txt");
@Cleanup FileOutputStream ou=new FileOutputStream("/work/auth2.txt");
byte[] b=new byte[1024];
while (true){
int i=in.read();
if (i==-1){
break;
}
ou.write(i);
}
}
这样就不用手动调用try-catch然后调用close方法关闭资源了
3.@Getter/@Setter
用在非静态成员变量上,自动添加get/set方法
4.@ToString
生成方法时只会使用类中的非静态和非transient成员变量
为类添加toString方法
@ToString(of = {"param1"})
public class TestLombok {
private String param1;
private String param2;
private String param3;
}
@ToString(of = {"param1"}) 生成的toString方法只有param1
@ToString(exclude= {"param1"}) 生成的toString方法不包含param1
5.@EqualsAndHashCode
生成方法时只会使用类中的非静态和非transient成员变量
为类添加 equals()和hashCode()方法,
exclude和of属性同样适用
6.@NoArgsConstructor
添加无参构造方法
@AllArgsConstructor
添加包含所有参数的构造方法
@RequiredArgsConstructor
构造函数,字段只包括被@NonNull修饰和final的成员变量组成的构造函数
@RequiredArgConstructor(staticName = "testT")
staticName,产生一个名为"testT"的静态方法,返回 @RequiredArgConstructor 产生的构造函数返回的实例
7.@Data
添加如下:
- @Getter/@Setter
- @ToString
- @EqualsAndHashCode
- @RequiredArgsConstructor
8.@Value
和@Data效果类似,@Value不会产生set方法,同时会把每个成员变量设置成private final类型的
9.@Log
@CommonsLog
private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
@JBossLog
private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class);
@Log
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);