Lombok 注解简洁说明

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/fxbin123/article/details/90144525

Lombok注解:

@Data:注解在类上,将类提供的所有属性都添加get、set方法,并添加、equals、canEquals、hashCode、toString方法

@Setter:注解在类上,为所有属性添加set方法、注解在属性上为该属性提供set方法

@Getter:注解在类上,为所有的属性添加get方法、注解在属性上为该属性提供get方法

@NotNull:在参数中使用时,如果调用时传了null值,就会抛出空指针异常

@Synchronized 用于方法,可以锁定指定的对象,如果不指定,则默认创建一个对象锁定

@Log作用于类,创建一个log属性

@Builder:使用builder模式创建对象

@NoArgsConstructor:创建一个无参构造函数

@AllArgsConstructor:创建一个全参构造函数, 替代@Autowired构造注入,多个bean 注入时更加清晰

@Slf4j
@Configuration
@AllArgsConstructor
public class RouterFunctionConfiguration {
   private final HystrixFallbackHandler hystrixFallbackHandler;
   private final ImageCodeHandler imageCodeHandler;
   
}


@Slf4j
@Configuration
public class RouterFunctionConfiguration {
   @Autowired
   private  HystrixFallbackHandler hystrixFallbackHandler;
   @Autowired
   private  ImageCodeHandler imageCodeHandler;
}

@ToString:创建一个toString方法

@Accessors(chain = true)使用链式设置属性,set方法返回的是this对象。

@RequiredArgsConstructor:创建对象, 例: 在class上添加@RequiredArgsConstructor(staticName = “of”)会创建生成一个静态方法

@UtilityClass:工具类再也不用定义static的方法了,直接就可以Class.Method 使用

@UtilityClass
public class Utility {

    public String getName() {
        return "name";
    }
}

public static void main(String[] args) {
    System.out.println(Utility.getName());
}

@ExtensionMethod:设置父类

@FieldDefaults:设置属性的使用范围,如private、public等,也可以设置属性是否被final修饰。

@SneakyThrows

@SneakyThrows
private void checkCode(ServerHttpRequest request) {
   String code = request.getQueryParams().getFirst("code");

   if (StrUtil.isBlank(code)) {
   	throw new ValidateCodeException("验证码不能为空");
   }

   redisTemplate.delete(key);
}


// 不使用就要加这个抛出
private void checkCode(ServerHttpRequest request) throws ValidateCodeException {
   String code = request.getQueryParams().getFirst("code");

   if (StrUtil.isBlank(code)) {
   	throw new ValidateCodeException("验证码不能为空");
   }
}

@EqualsAndHashCode:重写equals和hashcode方法。

@Cleanup: 清理流对象,不用手动去关闭流

@Cleanup
OutputStream outStream = new FileOutputStream(new File("text.txt"));
@Cleanup
InputStream inStream = new FileInputStream(new File("text2.txt"));
byte[] b = new byte[65536];
while (true) {
   int r = inStream.read(b);
   if (r == -1) break;
   outStream.write(b, 0, r); 
}

猜你喜欢

转载自blog.csdn.net/fxbin123/article/details/90144525