Springboot系列文章4:好用工具

Springboot系列文章

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。




前言

Springboot项目中,经常使用的好工具有哪些呢,我们来看一看


一、lombok

常见用法

import lombok.*;
@Data //代替属性的get,
@ToString  //代替toString
@AllArgsConstructor  //代替全参构造函数
@NoArgsConstructor   //代替无参构造函数
public class Pet {
    @NonNull //setter方法进行非空检查
    private String name;
    private int age;
    public Pet(String name)
    {
        this.name=name;
    }
}

@Data

代替Getter,Setter方法
All together now: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields, and @RequiredArgsConstructor!

@AllArgsConstructor

代替全参构造函数

@NoArgsConstructor

代替无参构造函数

@Synchronized

synchronized done right: Don’t expose your locks.

@Log

创建一个log指定类型的对象,然后在再调用log方法
Captain’s Log, stardate 24435.7: “What was that line again?”

val

@Cleanup

Automatic resource management: Call your close() methods safely with no hassle.

@Value

创建后,不可改变的类;

二、dev-tools

安装

pom中引入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

自动重启:ctrl+f9

使用ctrl+f9,自动检测项目是否变更,如果变更那么自动重启。

三、Spring Initailizr(项目初始化向导)

1、选择我们需要的开发场景

在这里插入图片描述

2、自动依赖引入

在这里插入图片描述

3、自动创建项目结构

在这里插入图片描述

4、自动编写好主配置类

在这里插入图片描述

configuration-processor 配置提示

添加配置提示功能

自定义的类和配置文件绑定一般没有提示。

<!-- 配置类提示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>


 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <!--发布时排除掉 processor-->
                        <exclude>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

自定义配置参数

需要在自定义类前增加@Component,@ConfigurationProperties

@Component
@ConfigurationProperties(prefix = "person")

配置类实例

  • java类
@Data
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String userName;
    private Boolean boss;
    private Date birth;
    private Integer age;
    private Pet pet;
    private String[] interests;
    private List<String> animal;
    private Map<String, Object> score;
    private Set<Double> salarys;
    private Map<String, List<Pet>> allPets;
}
  • yml文件
person:
  userName: 张三
  #data 默认分隔符/
  birth: 1978/01/01
  age: 40
  #object
  Pet: {name: 阿黄, weight: 20.5 }
  #array, 兴趣爱好
  interests: [跑步,羽毛球]
  animal: [小猫,小狗,金鱼]
  #array, 分数
  score:
    - math: 85
    - english: 90
    - Chinese: 80
  salarys: [9999,29999]
  #map, 所有宠物
  allPets:
    sick:
    - {name: 小猫,weight: 20.5}
    - {name: 小狗,weight: 30.5}
    health:
    - {name: 金鱼,weight: 0.5}
  boss: true
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值