2

I have class MyModel.java and I'm constructing it using @PostConstruct.

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

    @PostConstruct
    private void postConstruct() {
    }
}

Now I want to call a method of Model class(contains field and getter setter) say:

сlass Model {

    ....
    //fields and getter setter

    public void testValues(){
    }
}

Now I want to call testValues() in @PostContruct .

How can I call it ?

Ken Bekov
  • 13,696
  • 3
  • 36
  • 44
Sai prateek
  • 11,842
  • 9
  • 51
  • 66
  • Do you mean that you'd like to call a method that is present on the super-class? Then you'd just do this within `postConstruct()`: `testValues();`, as any other method in a hierarchical state would – vegaasen Mar 22 '17 at 06:38
  • I dont want to call within postconstruct(). as Model class is loading in postconstrct so we can call it from Model class. – Sai prateek Mar 22 '17 at 06:42
  • @Saiprateek can you clean up the question a bit? what you are writing in question is contradicting with what you wrote here ( *Now I want to call testValues() in @PostContruct* vs *I don't want to call forn annotated method* ). And what you asked is also hard to understand: *" as Model class is loading in postconstrct"* Model class is loading what? "*we can call it from Model class*" Call from which place? Do you mean you want spring to invoke some method in `Model`, after all `@PostConstruct` annotated methods (in child and `Model`) are finished? – Adrian Shum Mar 22 '17 at 08:39

1 Answers1

1

As part of the Spring bean's life cycle, afterPropertiesSet() will be called after @PostConstruct method, you can look here, so you can use afterPropertiesSet() to call your testValues() as shown below:

MyModel class:

public class MyModel extends Model implements  InitializingBean {

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

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

I have added the below notes from the link on spring bean's lifecylce:

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)

Community
  • 1
  • 1
Vasu
  • 21,832
  • 11
  • 51
  • 67
  • As Model.java class is loading in postconstruct so I don't want to call forn annotated method, I want to call from same class Model. – Sai prateek Mar 22 '17 at 06:40
  • Can you add briefly on what are you doing inside postconstruct for the Model? Are you setting some values? – Vasu Mar 22 '17 at 06:42
  • Yes..you can see in Model class there are some fields and there getter and setter methods which is initialising in postconstruct. – Sai prateek Mar 22 '17 at 06:43
  • I got your point now and you can use afterPropertiesSet() as explained above – Vasu Mar 22 '17 at 07:06