lombok注解

170 阅读1分钟

@Slf4j

使用lombok @Slf4j注解的前提:在首先需要引入lombok, maven中增加相关依赖, 有了 lombok 后, 只需要在类的上面添加 @Slf4j 注解即可:

@Slf4j
@RestController
public class DemoController {
	// ...
}

在这之后, lombok 会为我们注入一个 name 为 log 的日志属性, 在代码中直接使用即可

@Slf4j
@RestController
public class DemoController {

	@GetMapping("/hello")
	public String hello() {
		log.info("enter hello...");
		return "hello rest";
	}
}

@AllArgsConstructor

会生成一个包含所有变量,同时如果变量使用了NotNull annotation,会进行是否为空的校验,全部参数的构造函数的自动生成,该注解的作用域也是只有在实体类上,参数的顺序与属性定义的顺序一致

@NoArgsConstructor

无参构造函数

@RequiredArgsConstructor

会生成一个包含常量(final),和标识了@NotNull的变量的构造方法。