首页 > 其他分享 >Lombok不常见但实用的注解

Lombok不常见但实用的注解

时间:2022-10-29 20:13:01浏览次数:71  
标签:SneakyThrows private SimpleDateFormat 实用 class new 注解 Lombok public

目录

资料参考地址1:@SneakyThrows注解

资料参考地址2: Lombok之@Cleanup使用

资料参考地址3: lombok 实验性注解之 @UtilityClass

@SneakyThrows

为了消除遇到Exception异常时必须try catch 或向上抛处理的模板代码

示例代码

import lombok.SneakyThrows;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * SneakyThrows是为了消除这样的模板代码
 * https://blog.csdn.net/qq_22162093/article/details/115486647
 * @author : lyn
 */
public class SneakyThrowsTest {

    /**
     * 使用前必须用try catch 包一层
     */
    private Date strToDate1(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(str);
        } catch (ParseException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 使用@SneakyThrows后简洁了很多
     */
    @SneakyThrows
    private Date strToDate2(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.parse(str);
    }
}

@UtilityClass

作用于类,将类标记为 final,并且类、内部类中的方法、字段都标记为 static

并私有了构造方法,一般用在工具类上

编译前

@UtilityClass
public class UtilityClassTest {
    private String name;
    public void methodOne(){
        System.out.println("");
    }
}

编译后

public final class UtilityClassTest {
  private static String name;
  
  private UtilityClassTest() {
    throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
  }
  
  public static void methodOne() {
    System.out.println("");
  }
}

@CleanUp

清理流对象,不用手动去关闭流,多么优雅

编译前

 @Cleanup Reader fileReader = new FileReader("D:\\code.txt");

编译后

    try {
      Reader fileReader = new FileReader("D:\\code.txt");
      if (Collections.<Reader>singletonList(fileReader).get(0) != null)
        fileReader.close(); 
    } catch (Throwable $ex) {
      throw $ex;
    } 

Collections.singletonList被限定只被分配一个内存空间,也就是只能存放一个元素的内容

Collections.singletonList的作用和使用方法

@AllArgsConstructor

与private final 结合使用

替代@Autowired构造注入,多个bean 注入时更加清晰,但有循环依赖的问题

@AllArgsConstructor 
@Slf4j
@Component
@AllArgsConstructor
public class CustomAuthenticationSuccessEventHandler implements AuthenticationSuccessHandler {

   private final OperationLogHandler operationLogHandler;

}

标签:SneakyThrows,private,SimpleDateFormat,实用,class,new,注解,Lombok,public
From: https://www.cnblogs.com/lyn8100/p/16839541.html

相关文章

  • Spring注解之@Value基于Apollo或者YAML文件为静态变量赋值
    摘要:SpringBoot微服务中,把在Apollo配置中心或者YAML文件里配置的属性赋值给静态变量。综述  Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同......
  • Python数据分析教程(三):实用代码
    Python数据分析教程专栏:数据分析-标签-孤飞-博客园(cnblogs.com)Python数据分析教程(一):Numpy-孤飞-博客园(cnblogs.com)Python数据分析教程(二):Pandas-孤飞-......
  • Java注解
    1.Java注解的描述java注解又叫做java标注,是java的一种注释机制,在jdk5.0引入。其可以作用在类、方法、变量、参数和包上。另外,其可以通过反射来获取注解标注的内容。可以说......
  • spring注解--切面(AOP)相关注解
    切面(AOP)相关注解Spring支持AspectJ的注解式切面编程。@Aspect声明一个切面(类上)使用@After、@Before、@Around定义建言(advice),可直接将拦截规则(切点)作为参数。@After在......
  • spring注解--@Bean的属性支持
    @Bean的属性支持@Scope设置Spring容器如何新建Bean实例(方法上,得有@Bean)其设置类型包括:·Singleton(单例,一个Spring容器中只有一个bean实例,默认模式),·Protetype(每......
  • spring注解--声明注解
    1.声明bean的注解@Component 组件,通用的注解方式@Component作用:调用无参构造创建一个bean对象,并把对象存入spring的IOC容器,交由spring容器进行管理。相当于在xml中配......
  • spring注解--java配置类相关注解
    java配置类相关注解@Configuration声明当前类为配置类,相当于xml形式的Spring配置(类上)@Bean注解在方法上,声明当前方法的返回值为一个bean,替代xml中的方式(方法上)@Config......
  • day02-创建项目,注解,环境,容器
    2.2创建SpringBoot项目2.2.1第一种方式,使用Spring提供的初始化器,就是向导创建SpringBoot应用使用的地址:https://start.spring.iostep1:创建项目step2step......
  • c++17 注解标签 attributes & c++ 枚举值
    c++标注c++17后逐渐完善注解标签语法:[[attribute]]types/functions/enums/etc 告诉编译器没有返回值[[noreturn]]常用于系统函数设计,如std::abort()std::exit()......
  • java基础-注解
    以下为本人的学习笔记 1.认识Annotation  JDK1.5开始,java增加了对元数据(即类的组成单元数据)的支持,也就是(Annotation)注解,它是代码里做的特殊标记,这些标记可以在编译,类......