首页 > 其他分享 >Lombok 注解

Lombok 注解

时间:2022-08-20 17:26:21浏览次数:82  
标签:String byte args new 注解 Lombok public out

 

 

@RequiredArgsConstructor用法

 

在我们写controller或者Service层的时候,需要注入很多的mapper接口或者另外的service接口,这时候就会写很多的@Autowired注解,代码看起来很乱.
lombok提供了一个注解:

@RequiredArgsConstructor(onConstructor =@_(@Autowired))
写在类上可以代替@Autowired注解,需要注意的是在注入时需要用final定义,或者使用@notnull注解

 

@SneakyThrows的作用:

普通Exception类,也就是我们常说的受检异常或者Checked Exception会强制要求抛出它的方法声明throws,调用者必须显示的去处理这个异常。设计的目的是为了提醒开发者处理一些场景中必然可能存在的异常情况。比如网络异常造成IOException。

但是现实大部分情况下的异常,我们都是一路往外抛了事。所以渐渐的java程序员处理Exception的常见手段就是外面包一层RuntimeException,接着往上丢

try{
}catch(Exception e){
throw new RuntimeException(e);
}

而Lombok的@SneakyThrows就是为了消除这样的模板代码。
使用注解后不需要担心Exception的处理

import lombok.SneakyThrows;

public class SneakyThrowsExample implements Runnable {
  @SneakyThrows(UnsupportedEncodingException.class)
  public String utf8ToString(byte[] bytes) {
    return new String(bytes, "UTF-8");
  }
  
  @SneakyThrows
  public void run() {
    throw new Throwable();
  }
}

起通过编译器生成真正的代码:

import lombok.Lombok;

public class SneakyThrowsExample implements Runnable {
  public String utf8ToString(byte[] bytes) {
    try {
      return new String(bytes, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      throw Lombok.sneakyThrow(e);
    }
  }
  
  public void run() {
    try {
      throw new Throwable();
    } catch (Throwable t) {
      throw Lombok.sneakyThrow(t);
    }
  }
}

 

@Cleanup:

  • 用于确保已分配的资源被释放,自动帮我们调用close()方法。比如IO的连接关闭。
public static void main(String[] args) throws IOException {
     @Cleanup InputStream in = new FileInputStream(args[0]);
     @Cleanup OutputStream out = new FileOutputStream(args[1]);
     byte[] b = new byte[1024];
     while (true) {
       int r = in.read(b);
       if (r == -1) break;
       out.write(b, 0, r);
     }
 }
  • 实际效果相当于:
public static void main(String[] args) throws IOException {
     InputStream in = new FileInputStream(args[0]);
     try {
       OutputStream out = new FileOutputStream(args[1]);
       try {
         byte[] b = new byte[10000];
         while (true) {
           int r = in.read(b);
           if (r == -1) break;
           out.write(b, 0, r);
         }
       } finally {
         if (out != null) {
           out.close();
         }
       }
     } finally {
       if (in != null) {
         in.close();
       }
    }
}

 

标签:String,byte,args,new,注解,Lombok,public,out
From: https://www.cnblogs.com/hanzewa/p/16608152.html

相关文章

  • spring注解之@Import注解的三种使用方式
    @目录1、@Import注解须知2、@Import的三种用法2.1、第一种用法:直接填class数组2.2、第二种用法:ImportSelector方式【重点】2.3、第三种用法:ImportBeanDefinitionRegistrar......
  • 聊聊@SpringBootApplication注解
    @SpringBootApplication其实就是以下三个注解的总和@Configuration: 用于定义一个配置类@EnableAutoConfiguration :SpringBoot会自动根据你jar包的依赖来自动配置项......
  • spring5 ioc 管理bean 注解
    1.注解种类@Component(value="student")@Service@Repository@Controller 2.使用注解扫描包<context:component-scanbase-package="com.cj"></context:componen......
  • @Transactional注解加不加 rollbackFor = Exception.class 的区别
    先上结论:1.@Transactional只能回滚RuntimeException和RuntimeException下面的子类抛出的异常不能回滚Exception异常2.如果需要支持回滚Exception异常请用@Transactio......
  • 2、spring+mybatis注解+idea+maven
    1、在idea中配置database连接数据库,用来在idea中编写sql脚本操作数据库         2、sql详细脚本如下:1--1.创建部门表2createtabledept3......
  • Spring和SpringMVC的常用注解
    Spring和SpringMVC的常用注解1.Spring相关注解 1.1声明bean的注解@Component通用注解,用于声明bean。可以配置任意组件。@Repository派生注解,与@Component等效,Dao......
  • SpringBoot中常用的参数注解
    1.@PathVariable获取浏览器请求路径的参数(rest风格)2.@RequestHeader获取请求头3.@RequestParam获取请求参数请求连接:接口:4.@CookieValue获取cookie的值......
  • 定义简单的注解
    定义一个注解@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public@interfaceUserCase{publicintid();publicStringdescription()d......
  • 8.使用注解开发
    ......
  • SpringBoot+Lombok+Builder实现任意个数属性的对象构造
    场景某个类有多个属性,在不同的业务场景下需要对不同对象赋值不同的属性。如果使用原始构造方法赋值,需要有几种情况的参数赋值,就在实体类中声明对应参数的构造方法。可以......