如何调用 @PostConstruct class 中存在的方法

How to call a method present in @PostConstruct class

我有 class MyModel.java,我正在使用 @PostConstruct 构建它。

@ConfigurationProperties(prefix = "test")
public static class MyModel extends Model {

    @PostConstruct
    private void postConstruct() {
    }
}

现在我想调用模型class的一个方法(包含字段和getter setter)说:

сlass Model {

    ....
    //fields and getter setter

    public void testValues(){
    }
}

现在我想在 @PostContruct 中调用 testValues()

如何称呼它?

作为Springbean生命周期的一部分,afterPropertiesSet()会在@PostConstruct方法之后被调用,你可以看看here,所以你可以使用afterPropertiesSet() 调用您的 testValues() 如下所示:

我的模型class:

public class MyModel extends Model implements  InitializingBean {

    @PostConstruct
    private void postConstruct() {
        //set your values here
    }

    @Override
    public void afterPropertiesSet() {
        testValues();
    }
}

我在 spring bean 的生命周期上添加了 link 中的以下注释:

Multiple lifecycle mechanisms configured for the same bean, with different initialization methods, are called as follows (emphasis is mine):

Methods annotated with @PostConstruct (called first)

afterPropertiesSet() as defined by the InitializingBean callback interface (called after @PostConstruct method)

custom configured init() method (called after afterPropertiesSet method)