SpringBoot之SpringBoot整合lombok

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

本文来源于网络,只做技术分享,一概不负任何责任

posted @ 2021-01-21 18:20  彼岸舞  阅读(3905)  评论(0编辑  收藏  举报