@PostConstruct method is being called without the dependency being ready

package com.coaches.demoCoaches;

import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class footballCoach implements coach{
    outfit coachOutfit;
    @Override
    public String getDailyWorkout() {
        return "score 10 goals";
    }
    @PostConstruct
    public void coachIsReady(){
        System.out.println("Coach is now ready");
        System.out.println(coachOutfit);
    }
    public outfit getCoachOutfit() {
        return coachOutfit;
    }
    public void setCoachOutfit(outfit coachOutfit) {
        this.coachOutfit = coachOutfit;
    }


}

Here is the class i’m creating the PostConstruct method for

2023-11-01T00:30:25.910+02:00  INFO 7902 --- [  restartedMain] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.15]
2023-11-01T00:30:25.936+02:00  INFO 7902 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-11-01T00:30:25.937+02:00  INFO 7902 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 756 ms
Coach is now ready
null
2023-11-01T00:30:26.134+02:00  INFO 7902 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2023-11-01T00:30:26.154+02:00  INFO 7902 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-11-01T00:30:26.160+02:00  INFO 7902 --- [  restartedMain] c.c.demoCoaches.DemoCoachesApplication   : Started DemoCoachesApplication in 1.266 seconds (process running for 1.612)

and here is the output as you can see the postConstruct method is called and printing the dependency as Null already

I wanted to make sure that the function until I pass the outfit instance to the coach

    package com.coaches.demoCoaches;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class DemoCoachesApplication {

    public static void main(String[] args) {
        ApplicationContext myContext = SpringApplication.run(DemoCoachesApplication.class, args);

        footballCoach mycoach =  myContext.getBean(footballCoach.class);
    }

}

  • Can you include the code where the coach object is being instantiated?

    – 

  • @TimBiegeleisen added main function to the post

    – 

  • Welcome to Stack Overflow! Thank you for your question. However it seems like you are not familiar with the guide of Stack Overflow to How To: Ask a good question. Please read it and adjust your question accordingly by using the Edit option. Happy coding!

    – 

  • I do not understand. Field coachOutfit is never set. How should it be anything different than null? Where is theh coachOutfit suppose to come from? Is the outfit-class annotated with a bean-defining scope-annotation? — The class footballCoach is missing a means to inject the outfit in it (preferrably through c’tor injection). — As an aside: In java, classes should start with an uppercase letter. If you are new to java, maybe don’t start with a framework like spring-boot.

    – 




  • @Turing85 I know coachOutfit is null I’m not asking for a value but I thought the PostConstruct function won’t run until I call the setter for coachOutfit and initiate the coachOutfit. isn’t that when PostConstruct is suppose to run ?

    – 

Leave a Comment