Lombok simplifies JavaBean development

1. Install Lombok

1. Why import Lombok dependencies

Lombok introduced to depend at compile time to generate different codes depending on the annotation, such as annotation @Tostring at compile time will help us toString method automatically generates rewritten.

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

2. Why install Lombok plugin in idea

  • The Lombok plug-in is installed in the idea to allow us to use Lombok annotations without errors.
  • For example, we used the @AllArgsConstructor notes, the notes will be compiled automatically help us generate the constructor have parameters, because @AllArgsConstructor annotations are compiled only have helped us generate a reference constructor, before compiling the code is not there If the constructor is involved, an error will occur at this time. Installing the Lombok plug-in in idea is to remind idea and let idea know that we are now using Lombok to simplify the development of JavaBean.

Insert picture description here

Two. The related solution in Lombok

1. @Getter/@Setter

  • Annotations can be used on classes and attributes
  • @Getter/@Setter: automatically generate get and set methods at compile time
  • The default generated method is public, if you want to modify the method modifier, you can set the value of AccessLevel, for example: @Getter(access = AccessLevel.PROTECTED)

2. @ToString

  • Annotations are used on classes
  • @ToString: automatically rewrite the toString method for us at compile time
  • By default, it will print your class names and each field in order (separated by commas). You can set which fields are not included like this @ToString(exclude = “xx”) or @ToString(exclude = {"xxx","xxx"})
  • If the inherited has a parent class, you can set callSuper to call the toString() method of the parent class, for example: @ToString(callSuper = true)
@ToString(callSuper = true,exclude = {
    
    "name","age"})

3. @AllArgsConstructor

  • Annotations are used on classes
  • @AllArgsConstructor: A parameterized constructor consisting of all parameters is automatically generated for us at compile time.

4. @RequiredArgsConstructor

  • Annotations are used on classes
  • @RequiredArgsConstructor: Part of the parameter constructor is automatically generated for us at compile time
  • It may or may not have parameters. If you have parameters, this parameter can only be an uninitialized field modified by final, or an uninitialized field annotated by @NonNull

5. @ NoArgsConstructor

  • Annotations are used on classes
  • @NoArgsConstructor: The no-argument constructor is automatically generated for us at compile time
  • When a final field in the class is not initialized, the compiler will report an error. At this time, @NoArgsConstructor(force = true) can be used, and then the default value 0 / false / null will be set for the final field that is not initialized.
  • For fields with constraints (such as @NonNull fields), no checks or assignments are generated, so please note that these constraints are invalid until these fields are initialized properly.

6. @EqualsAndHashCode

  • Annotations are used on classes
  • @EqualsAndHashCode: Automatically override the hashCode and equals methods for us at compile time
  • By default, it will use all non-static, non-transient fields.
  • But you can exclude more fields by using the optional exclude parameter.
@EqualsAndHashCode(exclude = "name")

7. @NonNull

  • Annotations are used on attributes or constructors
  • @NonNull: A non-null declaration will be generated, which can be used to verify parameters and can help avoid null pointers.

8. @Data

  • Annotations are used on classes
  • @Data: Setter/getter, equals, canEqual, hashCode, and toString methods will be automatically generated for all properties of the class. If it is a final property, no setter method will be generated for the property.

For example:

//Lombok
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@EqualsAndHashCode
//@Data

public class Person {
    
    
	@NonNull
    public String name;
    public int age;
    public String gender;

    public Person( int age, String gender) {
    
    
        this.age = age;
        this.gender = gender;
    }
}

9. @ Slf4j

  • Annotations are used on classes
  • @Slf4j: Simplify log development.
  • Inject the log class, and inject the log attribute into the class where the annotation is located. This is our log recorder. In the future, we don't need to actively output in the console, and use the log.info() method directly.

For example:

//Lombok
@Slf4j

@Controller
@ResponseBody以字符串的方式写给浏览器,而不是跳转到某个页面
//@ResponseBody

//@RestController =  @Controller +  @ResponseBody
@RestController
public class HelloController {
    
    

    //自动注入属性
    @Qualifier(value = "human-com.ysw.boot.properties.Person")
    @Autowired
    Person person;

    @RequestMapping("/person")
    public Person getPerson(){
    
    
        log.info("into person");
        return person;
    }

    @RequestMapping("/hello")
    public String handle01(){
    
    
        log.info("into hello");
        return "Hello, SpringBoot 2.0 ! 加油!";
    }
}

Insert picture description here
Insert picture description here

Three. Lombok's advantages and disadvantages

advantage:

  • It can automatically generate constructors, getter/setter, equals, hashCode, toString and other methods through annotations, which improves development efficiency.
  • Make the code concise without paying too much attention to the corresponding method
  • When properties are modified, it also simplifies maintenance of the getter/setter methods generated for these properties, etc.

Disadvantages:

  • Does not support overloading of multiple parameter constructors
  • When the compiler is compiling, the bytecode generation is changed by manipulating the AST (Abstract Syntax Tree), which means that it is changing the Java syntax. It is not a runtime feature like Spring's dependency injection or Mybatis' ORM, but a compile-time feature.
  • Although it saves the trouble of manually creating getter/setter methods, it greatly reduces the readability and integrity of the source code, and reduces the comfort of reading the source code

Guess you like

Origin blog.csdn.net/MrYushiwen/article/details/112393592