lombok在生成的构造器、方法及参数上生成注解

2022-08-21 13:01:59 浏览数 (1)

我们可以在lombok生成的构造器、方法、参数上再附带注解,参考:

https://projectlombok.org/features/experimental/onX

例如下面代码:

指定构造器上新增@Autowired@Lazy(true)

getter上新增@Id@JsonIgnore(true)

setter上新增@NonNull

代码语言:javascript复制
package com.ruben.simplescaffold.component;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.annotation.Id;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.annotation.JsonIgnore;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Component
@AllArgsConstructor(onConstructor = @__({@Autowired, @Lazy(true)}))
public class SpringBean {

    @Getter(onMethod_ = {@Id, @JsonIgnore(true)})
    @Setter(onParam_ = {@NonNull})
    private JdbcTemplate jdbcTemplate;
}

会生成

代码语言:javascript复制
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.ruben.simplescaffold.component;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.annotation.Id;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

@Component
public class SpringBean {
    private JdbcTemplate jdbcTemplate;

    @Autowired
    @Lazy(true)
    public SpringBean(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Id
    @JsonIgnore(true)
    public JdbcTemplate getJdbcTemplate() {
        return this.jdbcTemplate;
    }

    public void setJdbcTemplate(@NonNull JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}

可谓是非常的好用,但是idea会爆红警告我们

即便它仍然能完成编译和运行

0 人点赞