如何调用@PostConstruct类中的方法

时间:2017-03-22 06:31:36

标签: java spring postconstruct

我有课程MyModel.java,我正在使用@PostConstruct构建课程。

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

    @PostConstruct
    private void postConstruct() {
    }
}

现在我想调用Model类的方法(包含field和getter setter)说:

сlass Model {

    ....
    //fields and getter setter

    public void testValues(){
    }
}

现在我想在testValues()中致电@PostContruct

我怎么称呼它?

1 个答案:

答案 0 :(得分:1)

作为Spring bean生命周期的一部分,我会在Assert.Throw()方法之后调用afterPropertiesSet(),您可以查看here,这样您就可以使用@PostConstruct来调用{ {1}}如下所示:

MyModel类:

afterPropertiesSet()

我在spring bean的lifecylce上添加了以下注释:

  

为同一个bean配置的多个生命周期机制   不同的初始化方法,如下所示(重点是我的):

     

使用@PostConstruct注释的方法(首先调用)

     

afterPropertiesSet()由InitializingBean回调接口定义(在@PostConstruct方法之后调用)

     

自定义配置的init()方法(在afterPropertiesSet方法之后调用)