阿超
>
lombok在生成的构造器、方法及参数上生成注解
冷眼观人,冷耳听语,冷情当感,冷心思理。——洪应明《菜根谭》
我们可以在lombok
生成的构造器、方法、参数上再附带注解,参考:
https://projectlombok.org/features/experimental/onX
例如下面代码:
指定构造器上新增@Autowired
、@Lazy(true)
getter
上新增@Id
、 @JsonIgnore(true)
setter
上新增@NonNull
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| 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; }
|
会生成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
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
会爆红警告我们
即便它仍然能完成编译和运行