SpringBoot之SpringBoot整合lombok
什么是lombok?為什么要用?
lombok是IDEA中的一個插件,需要手動安裝,為什么要用呢?是因為它可以大大簡化模型的代碼,在打包編譯時自動生成,去除掉模型中的get方法set方法無參構造,全參構造等代碼,采用注解表現,簡化工作量,提高可讀性,當然這是我的見解,不是官方的
添加依賴
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
IDEA添加lombok插件
在插件中搜索到,然后點擊安裝就可以了,我的是因為安裝過了,所以是關閉
注意:安裝完成插件后需要重啟IDEA才能使用
常用注解及其含義
@Data
原代碼
package com.springboot.demo.model; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; /** * Component 定義為組件 * ConfigurationProperties 通過前綴+屬性自動注入 * PropertySource 指定配置文件 */ @Component @ConfigurationProperties(prefix = "flower",ignoreUnknownFields = true) @PropertySource(value = { "classpath:application.yml" }) public class Flower { private String name; private String age; public Flower(String name, String age) { this.name = name; this.age = age; } public Flower() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } @Override public String toString() { return "Flower{" + "name='" + name + '\'' + ", age='" + age + '\'' + '}'; } }
使用@Data后代碼
package com.springboot.demo.model; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; /** * Component 定義為組件 * ConfigurationProperties 通過前綴+屬性自動注入 * PropertySource 指定配置文件 */ @Data @Component @ConfigurationProperties(prefix = "flower",ignoreUnknownFields = true) @PropertySource(value = { "classpath:application.yml" }) public class Flower { private String name; private String age; public Flower(String name, String age) { this.name = name; this.age = age; } public Flower() { } }
1、@Data可以為類提供讀寫功能,從而不用寫get、set方法。
2、他還會為類提供 equals()、hashCode()、toString() 方法。
@NoArgsConstructor
上面的使用@Data后的是原代碼
使用@NoArgsConstructor后
package com.springboot.demo.model; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; /** * Component 定義為組件 * ConfigurationProperties 通過前綴+屬性自動注入 * PropertySource 指定配置文件 */ @Data @Component @NoArgsConstructor @ConfigurationProperties(prefix = "flower",ignoreUnknownFields = true) @PropertySource(value = { "classpath:application.yml" }) public class Flower { private String name; private String age; public Flower(String name, String age) { this.name = name; this.age = age; } }
@NoArgsConstructor注解是代替了無參的構造函數
@AllArgsConstructor
上面的使用了@NoArgsConstructor后的代碼是原碼
使用@AllArgsConstructor注解后
package com.springboot.demo.model; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; /** * Component 定義為組件 * ConfigurationProperties 通過前綴+屬性自動注入 * PropertySource 指定配置文件 */ @Data @Component @NoArgsConstructor @AllArgsConstructor @ConfigurationProperties(prefix = "flower",ignoreUnknownFields = true) @PropertySource(value = { "classpath:application.yml" }) public class Flower { private String name; private String age; }
@AllArgsConstructor注解代替了全部參數的構造方法
這些就是最常用的了,感覺是不是代碼少了一半,
問題:
其實有人會問,那么如果我的其中一個屬性的Set或者Get方法需要根據不同值來返回不同的值呢?
答疑:
可以顯示的寫出來,如果存在該字段的Set或者Get方法,lombok就不會生成該字段的Set或者Get方法
作者:彼岸舞
時間:2021\01\21
內容關於:SpringBoot
本文來源於網絡,只做技術分享,一概不負任何責任