SpringBoot框架学习笔记(三):Lombok 和 Spring Initailizr

1 Lombok

1.1 Lombok 介绍

(1)Lombok 作用

  • 简化JavaBean开发,可以使用Lombok的注解让代码更加简洁
  • Java项目中,很多没有技术含量又必须存在的代码:POJO的getter/setter/toString;异常处理;I/O流的关闭操作等等,这些代码既没有技术含量,又影响着代码的美观,Lombok应运而生

(2)SpringBoot 和 IDEA 官方支持

  • IDEA 2020已经内置了Lombok插件
  • SpringBoot 2.1.x之后的版本也在Starter中内置了Lombok依赖

1.2 Lombok 常用注解

  • @Data:注解在类上;提供所有属性的 getter 和 setter 方法,此外还提供了equals、canEqual、hashCode、toString 方法
  • @Setter:注解在属性上;为属性提供 setter 方法
  • @Getter:注解在属性上;为属性提供 getter 方法
  • @ToString:注解在类上;提供toString 方法
  • @Log4j:注解在类上;为类提供一个属性名为 log 的 Log4j 对象
  • @NoArgsConstructor:注解在类上;为类提供一个无参的构造方法
  • @AllArgsConstructor:注解在类上;为类提供一个全参的构造方法
  • @CleanUp:可以关闭流
  • @Builder:给被注解的类加个构造者模式
  • @Synchronized:加个同步锁
  • @SneakyThrows:等同于try/catch 捕获异常
  • @NotNull:如果给参数加个这个注解,参数为 null 时会抛出空指针异常
  • @Value:该注解和@Data类似,区别在于它会把所有成员变量默认定于为private final修饰,并且不会生成setter方法

Lombok扩展注解(需要在idea上安装Lombok插件才能使用) 

  • @Slf4j:注解在类上;为类提供一个属性名为 log 的 Slf4j 对象进行日志输出

1.3 Lombok 应用实例

需求说明:使用Lombok 简化实体类 Furn.java 代码,让代码简洁高效

代码实现

(1)Furn 实体类如下 

package com.springboot.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "furn01")
public class Furn {
    private Integer id;
    private String name;
    private Double price;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }
}

(2)在pom.xml中引入lombok,虽然springboot内置了lombok,但是如果要使用的话,仍然需要显示得引入一下

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

不需要指定版本,版本在spring-boot-dependencies-2.5.3中指定了

但我这里出现了问题。这样设置后也无法使用Lombok的注解。解决方法:在pom文件设置高版本的Lombok

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

(3)修改 Furn.java 使用Lombok注解简化代码,可以通过idea自带的反编译功能,看Furn.class的源码,就可以看到生成的完整代码

简化代码如下

package com.springboot.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@NoArgsConstructor
@AllArgsConstructor
@Data
@Component
@ConfigurationProperties(prefix = "furn01")
public class Furn {
    private Integer id;
    private String name;
    private Double price;
}

在目录 target/classes/com/springboot/bean/furn.class 即可看到反编译后的源码

完整源码如下 

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.springboot.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(
    prefix = "furn01"
)
public class Furn {
    private Integer id;
    private String name;
    private Double price;

    public Furn() {
    }

    public Furn(final Integer id, final String name, final Double price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    public Integer getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public Double getPrice() {
        return this.price;
    }

    public void setId(final Integer id) {
        this.id = id;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public void setPrice(final Double price) {
        this.price = price;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Furn)) {
            return false;
        } else {
            Furn other = (Furn)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label47: {
                    Object this$id = this.getId();
                    Object other$id = other.getId();
                    if (this$id == null) {
                        if (other$id == null) {
                            break label47;
                        }
                    } else if (this$id.equals(other$id)) {
                        break label47;
                    }

                    return false;
                }

                Object this$price = this.getPrice();
                Object other$price = other.getPrice();
                if (this$price == null) {
                    if (other$price != null) {
                        return false;
                    }
                } else if (!this$price.equals(other$price)) {
                    return false;
                }

                Object this$name = this.getName();
                Object other$name = other.getName();
                if (this$name == null) {
                    if (other$name != null) {
                        return false;
                    }
                } else if (!this$name.equals(other$name)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof Furn;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $id = this.getId();
        result = result * 59 + ($id == null ? 43 : $id.hashCode());
        Object $price = this.getPrice();
        result = result * 59 + ($price == null ? 43 : $price.hashCode());
        Object $name = this.getName();
        result = result * 59 + ($name == null ? 43 : $name.hashCode());
        return result;
    }

    public String toString() {
        return "Furn(id=" + this.getId() + ", name=" + this.getName() + ", price=" + this.getPrice() + ")";
    }
}

2 Spring Initailizr

2.1 Spring Initailizr 作用

通过Spring官方提供的Spring Initailizr 来构建Maven项目,能完美支持IDEA和Eclipse,让程序员来选择需要的开发场景(starter),还能自动生成启动类和单元测试代码

需要注意得是 Spring Initailizr 对Idea版本有要求,同时还要走网络

2.2  Spring Initailizr 使用演示

需求说明:使用 Spring Initailizr 创建SpringBoot项目,并支持web应用场景,支持MyBatis

2.2.1 方式1:IDEA 创建

(1)文件->新建->项目

(2)选择 Spring Initailizr (如果看不到这个项,需要安装 Spring Initailizr 插件) 

(3)效果如下

 

2.2.2 方式2:start.spring.io 创建

(1)打开网站  start.spring.io

 

(2)将该压缩包解压后使用IDEA打开,效果如下

 

  • 14
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值