目录
① 为什么使用
项目中经常使用bean,entity等类,绝大部分数据类类中都需要get、set、toString、equals和hashCode方法,虽然eclipse和idea开发环境下都有自动生成的快捷方式,但自动生成这些代码后,如果bean中的属性一旦有修改、删除或增加时,需要重新生成或删除get/set等方法,给代码维护增加负担。而使用了lombok则不一样,使用了lombok的注解(@Setter,@Getter,@ToString,@@RequiredArgsConstructor,@EqualsAndHashCode或@Data)之后,就不需要编写或生成get/set等方法,很大程度上减少了代码量,而且减少了代码维护的负担。故强烈建议项目中使用lombok,去掉bean中get、set、toString、equals和hashCode等方法的代码。
② 如何安装
当前你使用的ide未安装lombok. lombok能够达到的效果就是在源码中不需要写一些通用的方法,但是在编译生成的字节码文件中会帮我们生成这些方法,减少代码冗余.
1.安装lombok插件:
具体流程如图:
点击下载就好啦,此处已经下载,创建项目测试
2.添加lombok的maven的pom.xml依赖:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
3.实体类示例代码Student.java
package com.lombok.demo;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Setter
@Getter
@ToString
@EqualsAndHashCode
public class Student {
private String name;
private int age;
private String male;
private String studentNo;
}
4.测试类LombokTest.java
package com.lombok.demo;
import lombok.extern.java.Log;
@Log
public class LombokTest {
public static void main(String[] args) {
Student student = new Student();
student.setAge(27);
student.setMale("woman");
student.setName("xs");
student.setStudentNo("2017");
System.out.println(student.toString());
Student student2 = new Student();
student2.setAge(27);
student2.setMale("woman");
student2.setName("xs");
student2.setStudentNo("2017");
System.out.println(student.equals(student2));
student2.setStudentNo("2018");
System.out.println(student.equals(student2));
log.info("lombok test");
}
}
5. 输出结果:
Student(name=xs, age=27, male=woman, studentNo=2017)
true
false
lombok test
③ 常用且特殊注解
@AllArgsConstructor
@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;
}
@RequiredArgsConstructor
@RequiredArgsConstructor 只会构造注入 final 注释的属性,推荐使用
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
@Api(value = "user", tags = "用户管理模块")
public class SysUserController {
private final SysUserService userService;
}
@SneakyThrows
@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("验证码不能为空");
}
}
@UtilityClass
@UtilityClass 工具类再也不用定义static的方法了,直接就可以Class.Method 使用
@UtilityClass
public class Utility {
public String getName() {
return "name";
}
}
public static void main(String[] args) {
System.out.println(Utility.getName());
}
@Cleanup
@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);
}