简体   繁体   English

具有默认构造函数的Lombok

[英]Lombok with default-constructor

I have a class like this: 我有一个这样的课:

@Service("aSpringService")
@RequiredArgsConstructor(onConstructor = @__({@Autowired}))
public class ServiceImpl implements Service {
    @NonNull
    private final Member1 m1;

    @NonNull
    private final Member2 m2;

    @NonNull
    private final Member3 m3;

}

The constructor will be created by Lombok and at runtime, spring will inject the members into the constructor. 构造函数将由Lombok创建,在运行时,sp​​ring会将成员注入构造函数。 Now I need a setup-method and got stuck with lombok. 现在我需要一个设置方法,并坚持使用lombok。 It seems, that Lombok cannot call something self-written. 看来,龙目岛不能称之为自编的东西。

What I want 我想要的是

I want a new parameter for the Lombok-Annotation like useDefaultConstructor . 我想要一个像useDefaultConstructor这样的Lombok-Annotation的新参数。 When this parameter is present, then the automated-code (from Lombok) will call a parameter-less constructor, which I can write for my own. 当存在此参数时,自动代码(来自Lombok)将调用无参数构造函数,我可以为自己编写。

@RequiredArgsConstructor(onConstructor = @__({@Autowired}), useDefaultConstructor = true)
// Note the "useDefaultConstructor = true" 
public class ServiceImpl implements Service {
    @NonNull
    private final Member1 m1; 

    private ServiceImpl() {
        //some self-written setup-code
    }
}

Generated class: 生成的类:

public class ServiceImpl implements Service {
    private final Member1 m1;

    // This constructor is not generated by lombok
    private ServiceImpl() {
        //some self-written setup-code
    }

    // Constructor generated by lombok
    @Autowired
    public ServiceImpl(Member1 m1) {
       this(); // <- only created when "useDefaultConstructor" is present
       this.m1 = m1;
    }
}

The question 这个问题

Is there a way to do this with lombok? 有没有办法用lombok做到这一点? Iam to lazy to write the constructor for my own (and change it everytime, when a need a new spring-member). 我懒得为自己编写构造函数(并且每当需要一个新的spring-member时更改它)。

Have you tried Spring @PostConstruct annotation? 你试过Spring @PostConstruct注释吗?

@PostConstruct
public void init() {
    // setup-code
}

It is a part of Spring's beans lifecycle management . 它是Spring的bean生命周期管理的一部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GWT实现Serializable - Private Default-Constructor - GWT Implementing Serializable - Private Default-Constructor 是否可以调用默认构造函数而不是零参数构造函数? - Is it possible to call the default-constructor instead of the zero-argument constructor? 如何在默认构造函数中使用 lombok - How to use lombok with default constructor Lombok builder 覆盖默认构造函数 - Lombok builder override default constructor Lombok @Builder 和 JPA 默认构造函数 - Lombok @Builder and JPA Default constructor 项目 Lombok + Hibernate 没有实体的默认构造函数 - Project Lombok + Hibernate No Default Constructor for Entity lombok 中的默认值。 如何使用构造函数和构建器初始化默认值 - Default value in lombok. How to init default with both constructor and builder 由于lombok创建的非默认构造函数,Jackson反序列化失败 - Jackson Deserialization Fails because of non-default constructor created by lombok 当抽象类具有final字段时,lombok @Data抱怨“ lombok需要基类中的默认构造函数” - lombok @Data complains “lombok needs a default constructor in the base class”, when abstract class having final field @NonNull注释是否允许默认构造函数赋予空值? - Is @NonNull annotation of Lombok allows the default constructor to give null values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM