Introduction
lombok is a compilation-level plug-in that can generate some code when the project is compiled. For example, during daily development, a large number of JavaBean files need to be produced. Each JavaBean needs to provide a large number of get and set methods. If there are many fields and changes, it is relatively cumbersome to modify. The corresponding lombok can save us the trouble of manually creating getter and setter methods through annotations (@getter,@setter) . It can automatically generate getter and setter methods when we compile the source code.
That is, the effect it can achieve is: there are no getter and setter methods in the source code, but there are getter and setter methods in the compiled bytecode file.
In addition, during the project development stage, the properties of a class are constantly changing. A field may be added today, and a field may be deleted tomorrow. Each change requires modifying the corresponding template code. In addition, some class have so many fields that they can’t finish at first sight. If you add template code, it is even harder to see at a glance. What's more, because there are too many fields, I want to use builder to create it. Manually creating builder and fields are mixed with the original class, which looks messy. lombok 's @Builder can solve this problem.
Official website address: https://projectlombok.org/
Lombok latest version number: http://jcenter.bintray.com/org/projectlombok/lombok/
Official document: https://projectlombok.org/features/all
lombok annotation introduction: https://www.VeVB.COM/article/151363.htm
Install the lombok plugin
Add annotation support
Add dependencies
buildscript { ext { springBootVersion = '2.0.2.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") }}apply plugin: 'java'apply plugin: 'eclipse'apply plugin: 'org.springframework.boot'apply plugin: 'io.spring.dependency-management' group = 'com.yanjun'version = '0.0.1-SNAPSHOT'sourceCompatibility = 1.8repositories { mavenCentral()}dependencies { compile('org.springframework.boot:spring-boot-starter', 'org.springframework.boot:spring-boot-starter-web', ) //Add lombok dependency compile 'org.projectlombok:lombok:1.18.0'}Common annotations
@NonNull Query empty processing
package com.yanjun.mybatis.bean;import lombok.Data;import lombok.NoArgsConstructor;import lombok.NonNull;@Data@NoArgsConstructorpublic class User { public Integer age; @NonNull public String name;}Equivalent to
package com.yanjun.mybatis.bean;import lombok.NonNull;public class User { public Integer age; @NonNull public String name; public Integer getAge() { return this.age; } @NonNull public String getName() { return this.name; } public void setAge(Integer age) { this.age = age; } public void setName(@NonNull String name) { if (name == null) { throw new NullPointerException("name is marked @NonNull but is null"); } else { this.name = name; } }}test
User user = new User();user.setName(null);
Effect
Exception in thread "main" java.lang.NullPointerException: name is marked @NonNull but is null
at com.yanjun.mybatis.bean.User.setName(User.java:7)
at com.yanjun.mybatis.LombokApplication.main(LombokApplication.java:15)
@Data provides get and set methods
package com.yanjun.mybatis.bean;import lombok.Data;@Data //Automatically generate get and set methods public class User { Integer id; String name; Integer age; public static void main(String[] args) { //Test method User user = new User(); user.setName("zhaoyanjun"); user.setAge(20); user.setId(1); }}@Slf4j Log Printing
package com.yanjun.mybatis.bean;import lombok.Data;import lombok.extern.slf4j.Slf4j;@Data //Automatically generate get and set methods @Slf4j //Login printing public class User { Integer id; String name; Integer age; public static void main(String[] args) { User user = new User(); user.setName("zhaoyanjun"); user.setAge(20); user.setId(1); log.info("Login" + user.toString()); }}@AllArgsConstructor Full parameter constructor
package com.yanjun.mybatis.bean;import lombok.AllArgsConstructor;import lombok.Data;import lombok.extern.slf4j.Slf4j;@Data //Automatically generate get and set methods @AllArgsConstructor //Automatically generate full parameter constructor @Slf4j //Login printing public class User { Integer id; String name; Integer age; public static void main(String[] args) { User user = new User(1, "zhaoyanjun", 20); log.info("Login" + user.toString()); }}@ToString Automatically generate toString method
package com.yanjun.mybatis.bean;import lombok.AllArgsConstructor;import lombok.Data;import lombok.ToString;import lombok.extern.slf4j.Slf4j;@Data //Automatically generate get and set methods @AllArgsConstructor //Automatically generate full parameter constructor @Slf4j //Login print @ToString(of = {"id","age"}) //Tostring() method only prints id , name field public class User { Integer id; String name; Integer age; public static void main(String[] args) { User user = new User(1, "zhaoyanjun", 20); log.info("Login" + user.toString()); }}toString() method excludes fields
//Exclude field @ToString(exclude = {"name"}) //In the tostring() method, the name field does not participate in printing@Value is used to annotate final classes
The @Value annotation is similar to @Data , the difference is that it will define all member variables as private final modification by default and will not generate set method.
package com.yanjun.mybatis.bean;import lombok.Value;@Valuepublic class User { Integer id = 1; String name = "zhaoyanjun"; Integer age = 3 ;}Compiled code
public final class User { private final Integer id = 1; private final String name = "zhaoyanjun"; private final Integer age = 3; public User() { } public Integer getId() { return this.id; } public String getName() { this.getClass(); return "zhaoyanjun"; } public Integer getAge() { return this.age; } //Omit some code......}@Builder: Add a constructor pattern to the annotated class
import lombok.Builder;@Builderpublic class User { public Integer id; public String name; public Integer age;}Equivalent to the following code
package com.yanjun.mybatis.bean;public class User { public Integer id; public String name; public Integer age; User(Integer id, String name, Integer age) { this.id = id; this.name = name; this.age = age; } public static User.UserBuilder builder() { return new User.UserBuilder(); } public static class UserBuilder { private Integer id; private String name; private Integer age; UserBuilder() { } public User.UserBuilder id(Integer id) { this.id = id; return this; } public User.UserBuilder name(String name) { this.name = name; return this; } public User.UserBuilder age(Integer age) { this.age = age; return this; } public User build() { return new User(this.id, this.name, this.age); } public String toString() { return "User.UserBuilder(id=" + this.id + ", name=" + this.name + ", age=" + this.age + ")"; } }}use
UserBuilder userBuilder = User.builder();User user = userBuilder .age(10) .id(1) .name("yanjun") .build();System.out.println(": " + userBuilder.toString());@Synchronized: Add a synchronization lock
package com.yanjun.mybatis.bean;import lombok.Synchronized;public class User { public Integer age; public String name; //Ordinary method, equivalent to object lock @Synchronized int run1() { return 1; } //Static method, equivalent to class lock @Synchronized static int run2() { return 2; }}The effect of the code is equivalent to
public class User { private final Object $lock = new Object[0]; private static final Object $LOCK = new Object[0]; public Integer age; public String name; public User() { } int run1() { Object var1 = this.$lock; synchronized(this.$lock) { return 1; } } static int run2() { Object var0 = $LOCK; synchronized($LOCK) { return 2; } }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.