欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

lombok使用及常用注解

程序员文章站 2024-03-12 20:37:02
...

简介

大部分项目中都必不可少的包含数据库实体(Entity)、数据载体(dto,dataObject),而这两部分都包含着大量的没有业务逻辑的setter、getter、空参构造,同时我们一般要复写类的toString(),equals(),hashCode()方法(贫血模型)。这些工作都是重复性的工作,作为程序员,懒是必备素质之一,这些工作肯定已经有大牛封装好了处理方法,这就是lombok。

idea 安装插件,支持lombok

lombok是在编译阶段才生成相应的代码体,所以在项目中直接调用setter,getter,constructor会报错,这时候可以在IDE安装相应的插件支持lombok。这里介绍idea插件安装,eclipse请自行百度。

安装方法

  1. 进入设置页面(windows:setting,Mac:Preferences)
  2. 点击Plugin
  3. Browse repositories
  4. 搜索lombok
  5. 点击Install
  6. 安装完毕后开启注解权限才能正常使用: 
    • –>setting
    • –>Build,Execution,Deployment
    • –>Compiler
    • –>Annontation Processors
    • –>勾选Enable annotation processing
    • –> Apply
  7. 重启Idea

引入方法

gradle

  1. // https://mvnrepository.com/artifact/org.projectlombok/lombok
  2. compile group: 'org.projectlombok', name: 'lombok', version: '1.16.16'
  • 1
  • 2
  • 3

maven

  1. <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
  2. <dependency>
  3. <groupId>org.projectlombok</groupId>
  4. <artifactId>lombok</artifactId>
  5. <version>1.16.16</version>
  6. </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

常用方法

@Setter

生成setter方法,final变量不包含

  1. //原始类
  2. @Setter
  3. public class TestEntity {
  4. private String name;
  5. private Integer age;
  6. private final String type = "type";
  7. }
  8. //反编译的类
  9. public class TestEntity {
  10. private String name;
  11. private Integer age;
  12. private final String type = "person";
  13. public TestEntity() {
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public void setAge(Integer age) {
  19. this.age = age;
  20. }
  21. }

@Getter

生成getter方法,final变量不包含

  1. //原始类
  2. @Getter
  3. public class TestEntity {
  4. private String name;
  5. private Integer age;
  6. private final String type = "person";
  7. }
  8. //反编译的类
  9. public class TestEntity {
  10. private String name;
  11. private Integer age;
  12. private final String type = "person";
  13. public TestEntity() {
  14. }
  15. public String getName() {
  16. return this.name;
  17. }
  18. public Integer getAge() {
  19. return this.age;
  20. }
  21. public String getType() {
  22. this.getClass();
  23. return "person";
  24. }
  25. }

@NoArgsConstructor

生成空参构造

  1. //原始类
  2. @NoArgsConstructor
  3. public class TestEntity {
  4. private String name;
  5. private Integer age;
  6. private final String type = "person";
  7. }
  8. //反编译的类
  9. public class TestEntity {
  10. private String name;
  11. private Integer age;
  12. private final String type = "person";
  13. public TestEntity() {
  14. }
  15. }

@AllArgsConstructor

生成全部参数构造

  1. //原始类
  2. @AllArgsConstructor
  3. public class TestEntity {
  4. private String name;
  5. private Integer age;
  6. private final String type = "person";
  7. }
  8. //反编译的类
  9. public class TestEntity {
  10. private String name;
  11. private Integer age;
  12. private final String type = "person";
  13. @ConstructorProperties({"name", "age"})
  14. public TestEntity(String name, Integer age) {
  15. this.name = name;
  16. this.age = age;
  17. }
  18. }

@RequiredArgsConstructor

将标记为@NoNull的属性生成一个构造器

如果运行中标记为@NoNull的属性为null,会抛出空指针异常。

  1. //原始类
  2. @RequiredArgsConstructor
  3. public class TestEntity {
  4. private String name;
  5. @NonNull
  6. private Integer age;
  7. private final String type = "person";
  8. }
  9. //反编译的类
  10. public class TestEntity {
  11. private String name;
  12. @NonNull
  13. private Integer age;
  14. private final String type = "person";
  15. @ConstructorProperties({"age"})
  16. public TestEntity(@NonNull Integer age) {
  17. if(age == null) {
  18. throw new NullPointerException("age");
  19. } else {
  20. this.age = age;
  21. }
  22. }
  23. }

@ToString

生成所有属性的toString()方法

  1. //原始类
  2. @ToString
  3. public class TestEntity {
  4. private String name;
  5. private Integer age;
  6. private final String type = "person";
  7. }
  8. //反编译的类
  9. public class TestEntity {
  10. private String name;
  11. private Integer age;
  12. private final String type = "person";
  13. public TestEntity() {
  14. }
  15. public String toString() {
  16. StringBuilder var10000 = (new StringBuilder()).append("TestEntity(name=").append(this.name).append(", age=").append(this.age).append(", type=");
  17. this.getClass();
  18. return var10000.append("person").append(")").toString();
  19. }
  20. }

@EqualsAndHashCode

生成equals()方法和hashCode方法

  1. //原始类
  2. @EqualsAndHashCode
  3. public class TestEntity {
  4. private String name;
  5. private Integer age;
  6. private final String type = "person";
  7. }
  8. //反编译的类
  9. public class TestEntity {
  10. private String name;
  11. private Integer age;
  12. private final String type = "person";
  13. public TestEntity() {
  14. }
  15. public boolean equals(Object o) {
  16. if(o == this) {
  17. return true;
  18. } else if(!(o instanceof TestEntity)) {
  19. return false;
  20. } else {
  21. TestEntity other = (TestEntity)o;
  22. if(!other.canEqual(this)) {
  23. return false;
  24. } else {
  25. label47: {
  26. String this$name = this.name;
  27. String other$name = other.name;
  28. if(this$name == null) {
  29. if(other$name == null) {
  30. break label47;
  31. }
  32. } else if(this$name.equals(other$name)) {
  33. break label47;
  34. }
  35. return false;
  36. }
  37. Integer this$age = this.age;
  38. Integer other$age = other.age;
  39. if(this$age == null) {
  40. if(other$age != null) {
  41. return false;
  42. }
  43. } else if(!this$age.equals(other$age)) {
  44. return false;
  45. }
  46. this.getClass();
  47. String this$type = "person";
  48. other.getClass();
  49. String other$type = "person";
  50. if(this$type == null) {
  51. if(other$type != null) {
  52. return false;
  53. }
  54. } else if(!this$type.equals(other$type)) {
  55. return false;
  56. }
  57. return true;
  58. }
  59. }
  60. }
  61. protected boolean canEqual(Object other) {
  62. return other instanceof TestEntity;
  63. }
  64. public int hashCode() {
  65. boolean PRIME = true;
  66. byte result = 1;
  67. String $name = this.name;
  68. int result1 = result * 59 + ($name == null?43:$name.hashCode());
  69. Integer $age = this.age;
  70. result1 = result1 * 59 + ($age == null?43:$age.hashCode());
  71. this.getClass();
  72. String $type = "person";
  73. result1 = result1 * 59 + ($type == null?43:$type.hashCode());
  74. return result1;
  75. }
  76. }

@Data(常用)

@Data直接修饰POJO or beans, getter所有的变量,setter所有不为final的变量。如果你不需要默认的生成方式,直接填写你需要的annotation的就可以了。默认生成的所有的annotation都是public的,如果需要不同权限修饰符可以使用AccessLevel.NONE选项。当然@Data 也可以使用staticConstructor选项生成一个静态方法。

[email protected][email protected][email protected][email protected]

  1. //原始类
  2. @Data
  3. public class TestEntity {
  4. @Setter(AccessLevel.PRIVATE)
  5. private String name;
  6. private Integer age;
  7. private final String type = "person";
  8. }
  9. //反编译的类
  10. public class TestEntity {
  11. private String name;
  12. private Integer age;
  13. private final String type = "person";
  14. public TestEntity() {
  15. }
  16. public String getName() {
  17. return this.name;
  18. }
  19. public Integer getAge() {
  20. return this.age;
  21. }
  22. public String getType() {
  23. this.getClass();
  24. return "person";
  25. }
  26. public void setAge(Integer age) {
  27. this.age = age;
  28. }
  29. public boolean equals(Object o) {
  30. if(o == this) {
  31. return true;
  32. } else if(!(o instanceof TestEntity)) {
  33. return false;
  34. } else {
  35. TestEntity other = (TestEntity)o;
  36. if(!other.canEqual(this)) {
  37. return false;
  38. } else {
  39. label47: {
  40. String this$name = this.getName();
  41. String other$name = other.getName();
  42. if(this$name == null) {
  43. if(other$name == null) {
  44. break label47;
  45. }
  46. } else if(this$name.equals(other$name)) {
  47. break label47;
  48. }
  49. return false;
  50. }
  51. Integer this$age = this.getAge();
  52. Integer other$age = other.getAge();
  53. if(this$age == null) {
  54. if(other$age != null) {
  55. return false;
  56. }
  57. } else if(!this$age.equals(other$age)) {
  58. return false;
  59. }
  60. String this$type = this.getType();
  61. String other$type = other.getType();
  62. if(this$type == null) {
  63. if(other$type != null) {
  64. return false;
  65. }
  66. } else if(!this$type.equals(other$type)) {
  67. return false;
  68. }
  69. return true;
  70. }
  71. }
  72. }
  73. protected boolean canEqual(Object other) {
  74. return other instanceof TestEntity;
  75. }
  76. public int hashCode() {
  77. boolean PRIME = true;
  78. byte result = 1;
  79. String $name = this.getName();
  80. int result1 = result * 59 + ($name == null?43:$name.hashCode());
  81. Integer $age = this.getAge();
  82. result1 = result1 * 59 + ($age == null?43:$age.hashCode());
  83. String $type = this.getType();
  84. result1 = result1 * 59 + ($type == null?43:$type.hashCode());
  85. return result1;
  86. }
  87. public String toString() {
  88. return "TestEntity(name=" + this.getName() + ", age=" + this.getAge() + ", type=" + this.getType() + ")";
  89. }
  90. private void setName(String name) {
  91. this.name = name;
  92. }
  93. }

@Builder

构造Builder模式的结构。通过内部类Builder()进行构建对象。

  1. //原始类
  2. @Builder
  3. public class TestEntity {
  4. private String name;
  5. private Integer age;
  6. private final String type = "person";
  7. }
  8. //反编译的类
  9. public class TestEntity {
  10. private String name;
  11. private Integer age;
  12. private final String type = "person";
  13. @ConstructorProperties({"name", "age"})
  14. TestEntity(String name, Integer age) {
  15. this.name = name;
  16. this.age = age;
  17. }
  18. public static TestEntity.TestEntityBuilder builder() {
  19. return new TestEntity.TestEntityBuilder();
  20. }
  21. public static class TestEntityBuilder {
  22. private String name;
  23. private Integer age;
  24. TestEntityBuilder() {
  25. }
  26. public TestEntity.TestEntityBuilder name(String name) {
  27. this.name = name;
  28. return this;
  29. }
  30. public TestEntity.TestEntityBuilder age(Integer age) {
  31. this.age = age;
  32. return this;
  33. }
  34. public TestEntity build() {
  35. return new TestEntity(this.name, this.age);
  36. }
  37. public String toString() {
  38. return "TestEntity.TestEntityBuilder(name=" + this.name + ", age=" + this.age + ")";
  39. }
  40. }
  41. }
  42. //Builder模式使用方法
  43. @Test
  44. public void test(){
  45. TestEntity testEntity = TestEntity.builder()
  46. .name("java")
  47. .age(18)
  48. .build();
  49. }

@Value

与@Data相对应的@Value, 两个annotation的主要区别就是如果变量不加@NonFinal ,@Value会给所有的弄成final的。当然如果是final的话,就没有set方法了。

  1. //原始类
  2. @Value
  3. public class TestEntity {
  4. @Setter(AccessLevel.PRIVATE)
  5. private String name;
  6. private Integer age;
  7. private final String type = "person";
  8. }
  9. //反编译的类
  10. public final class TestEntity {
  11. private final String name;
  12. private final Integer age;
  13. private final String type = "person";
  14. @ConstructorProperties({"name", "age"})
  15. public TestEntity(String name, Integer age) {
  16. this.name = name;
  17. this.age = age;
  18. }
  19. public String getName() {
  20. return this.name;
  21. }
  22. public Integer getAge() {
  23. return this.age;
  24. }
  25. public String getType() {
  26. this.getClass();
  27. return "person";
  28. }
  29. public boolean equals(Object o) {
  30. if(o == this) {
  31. return true;
  32. } else if(!(o instanceof TestEntity)) {
  33. return false;
  34. } else {
  35. TestEntity other;
  36. label44: {
  37. other = (TestEntity)o;
  38. String this$name = this.getName();
  39. String other$name = other.getName();
  40. if(this$name == null) {
  41. if(other$name == null) {
  42. break label44;
  43. }
  44. } else if(this$name.equals(other$name)) {
  45. break label44;
  46. }
  47. return false;
  48. }
  49. Integer this$age = this.getAge();
  50. Integer other$age = other.getAge();
  51. if(this$age == null) {
  52. if(other$age != null) {
  53. return false;
  54. }
  55. } else if(!this$age.equals(other$age)) {
  56. return false;
  57. }
  58. String this$type = this.getType();
  59. String other$type = other.getType();
  60. if(this$type == null) {
  61. if(other$type != null) {
  62. return false;
  63. }
  64. } else if(!this$type.equals(other$type)) {
  65. return false;
  66. }
  67. return true;
  68. }
  69. }
  70. public int hashCode() {
  71. boolean PRIME = true;
  72. byte result = 1;
  73. String $name = this.getName();
  74. int result1 = result * 59 + ($name == null?43:$name.hashCode());
  75. Integer $age = this.getAge();
  76. result1 = result1 * 59 + ($age == null?43:$age.hashCode());
  77. String $type = this.getType();
  78. result1 = result1 * 59 + ($type == null?43:$type.hashCode());
  79. return result1;
  80. }
  81. public String toString() {
  82. return "TestEntity(name=" + this.getName() + ", age=" + this.getAge() + ", type=" + this.getType() + ")";
  83. }
  84. }

@Synchronized

同步方法

  1. //原始类
  2. public class TestEntity {
  3. private String name;
  4. private Integer age;
  5. private final String type = "person";
  6. @Synchronized
  7. public void write(){
  8. //do something
  9. }
  10. }
  11. //反编译的类
  12. public class TestEntity {
  13. private final Object $lock = new Object[0];
  14. private String name;
  15. private Integer age;
  16. private final String type = "person";
  17. public TestEntity() {
  18. }
  19. public void write() {
  20. Object var1 = this.$lock;
  21. synchronized(this.$lock) {
  22. ;
  23. }
  24. }
  25. }

@Cleanup @@SneakyThrows

自动调用close方法关闭资源。

  1. //原始类
  2. public class TestEntity {
  3. private String name;
  4. private Integer age;
  5. private final String type = "person";
  6. @SneakyThrows
  7. public void outputStream(){
  8. @Cleanup OutputStream outputStream = new FileOutputStream(new File("/Users/hello"));
  9. }
  10. }
  11. //反编译的类
  12. public class TestEntity {
  13. private String name;
  14. private Integer age;
  15. private final String type = "person";
  16. public TestEntity() {
  17. }
  18. public void outputStream() {
  19. try {
  20. FileOutputStream $ex = new FileOutputStream(new File("/Users/hello"));
  21. if(Collections.singletonList($ex).get(0) != null) {
  22. $ex.close();
  23. }
  24. } catch (Throwable var2) {
  25. throw var2;
  26. }
  27. }