spingboot的lombok的使用

lombok 是采用注解用于自动为 POJO 生成 getter()、setter()、hashCode()、toString() 等方法的第三方类库。其常用注解介绍如下:
1) @Getter / @Setter:可以作用在类上和属性上,放在类上,会对所有的非静态(non-static)属性生成Getter/Setter方法,放在属性上,会对该属性生成Getter/Setter方法。并可以指定Getter/Setter方法的访问级别。
2) @EqualsAndHashCode :默认情况下,会使用所有非瞬态(non-transient)和非静态(non-static)字段来生成equals和hascode方法,也可以指定具体使用哪些属性。 @ToString 生成toString方法,默认情况下,会输出类名、所有属性,属性会按照顺序输出,以逗号分割。
3) @NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor:无参构造器、部分参数构造器、全参构造器

4) @Data:包含@ToString, @EqualsAndHashCode, 所有属性的@Getter, 所有non-final属性的@Setter和     @RequiredArgsConstructor的组合,通常情况下,基本上使用这个注解就足够了。

5) @Budilder:可以进行Builder方式初始化。

6) @Slf4j:等同于:private final Logger logger = LoggerFactory.getLogger(XXX.class);简直不能更爽了!一般上用在其他java类上

 

1、在pom.xml文件引入lombok的依赖

      
            org.projectlombok
            lombok
            1.16.22
            provided
       
       
        
                 org.springframework.boot
                 spring-boot-configuration-processor
                 true
        

2、在实体bean的类名上加上@Data

package com.example.demo.entity;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.List;

@Data
@Component
@ConfigurationProperties(prefix="config")
public class MyConfig {

    private String name;
    private Byte age;
    private List hobby;

}

编译后会自动生成所有属性,如下:

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

package com.example.demo.entity;

import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(
    prefix = "config"
)
public class MyConfig {
    private String name;
    private Byte age;
    private List hobby;

    public MyConfig() {
    }

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

    public Byte getAge() {
        return this.age;
    }

    public List getHobby() {
        return this.hobby;
    }

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

    public void setAge(Byte age) {
        this.age = age;
    }

    public void setHobby(List hobby) {
        this.hobby = hobby;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof MyConfig)) {
            return false;
        } else {
            MyConfig other = (MyConfig)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label47: {
                    Object this$name = this.getName();
                    Object other$name = other.getName();
                    if (this$name == null) {
                        if (other$name == null) {
                            break label47;
                        }
                    } else if (this$name.equals(other$name)) {
                        break label47;
                    }

                    return false;
                }

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

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

                return true;
            }
        }
    }

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

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $name = this.getName();
        int result = result * 59 + ($name == null ? 43 : $name.hashCode());
        Object $age = this.getAge();
        result = result * 59 + ($age == null ? 43 : $age.hashCode());
        Object $hobby = this.getHobby();
        result = result * 59 + ($hobby == null ? 43 : $hobby.hashCode());
        return result;
    }

    public String toString() {
        return "MyConfig(name=" + this.getName() + ", age=" + this.getAge() + ", hobby=" + this.getHobby() + ")";
    }
}

 

你可能感兴趣的:(java)