0

I am making a simple golf game. where there are 2 golf courses, when running the program I want the 2 golf courses added to the DB. I am using service and repository and endpoints for the golfers so new golfers can be added. but for the golf courses that have 3 holes each, I want them to be added automatically when the program runs. I have tried this:

Below is my code snippet. I want to execute addGolfCoursesAtStart() when my app is launched.

    @Autowired
    GolfCourseService gcs;
    
    public void addGolfCoursesAtStart() {
        // create course 1
        GolfCourse gc1 = new GolfCourse("Happy Gilmore", 220, 300, 50);
        System.out.println("we created a golf course for you named: " +gc1.getName());
        System.out.println("this course has the following holes:");
        for (Hole hole : gc1.getHoles()) {
            System.out.println(hole.getName());
            System.out.println("length: " + hole.getLenthHole());
        }
        
        gcs.addGolfCourse(gc1);
        System.out.println("\n\n");
    }

How would I achieve that?

Abhinaba Chakraborty
  • 3,488
  • 2
  • 16
  • 37
  • can you post the error. Without seeing the error I can't say what is the problem in your code. May be `gcs` will be null – deadshot Jul 12 '20 at 11:08
  • "i want them to be added automatically when the program runs" - Does this mean you want to add Golf courses before every test case ? or you want to add the golf courses in db when the spring boot app starts? – Abhinaba Chakraborty Jul 12 '20 at 11:24
  • @AbhinabaChakraborty i want to add the golf courses in db when the spring boot app starts – Maurice Dibbets Jul 12 '20 at 11:30
  • @deadshot: HAVE A GOOD DAY AT THE GOLF COURSE we created a golf course for you named: Happy Gilmore this course has the following holes: hole 1 length: 0 hole 2 length: 0 hole 3 length: 0 Exception in thread "main" java.lang.NullPointerException at com.golfgame.domain.Test.addGolfCoursesAtStart(Test.java:23) at com.golfgame.GolfgameApplication.main(GolfgameApplication.java:20) – Maurice Dibbets Jul 12 '20 at 11:31
  • @MauriceDibbets please check my answer. What I feel is, you need to make use of Spring Events. – Abhinaba Chakraborty Jul 12 '20 at 11:59

2 Answers2

0

Since you want to add the golf courses in db when the spring boot app starts, you need to listen to the ApplicationReadyEvent of Spring and put your logic there.

There are other events too that Spring, by default, provides us to hook into the application context lifecycle , like:

  1. ContextRefreshedEvent
  2. ContextStartedEvent
  3. ContextStoppedEvent
  4. ContextClosedEvent
  5. RequestHandledEvent etc etc

You can read more about them in the official Spring docs.

Here is the code using ApplicationReadyEvent:

@Component
public class MyApplicationListenerBean implements ApplicationListener<ApplicationReadyEvent> {

  @Autowired
  GolfCourseService gcs;

  @Override
  public void onApplicationEvent(ApplicationReadyEvent event) {

    //Your spring boot app is initialzied and application is ready
    addGolfCoursesAtStart();

  }

  public void addGolfCoursesAtStart() {
    // create course 1
    GolfCourse gc1 = new GolfCourse("Happy Gilmore", 220, 300, 50);
    System.out.println("we created a golf course for you named: " +gc1.getName());
    System.out.println("this course has the following holes:");
    for (Hole hole : gc1.getHoles()) {
      System.out.println(hole.getName());
      System.out.println("length: " + hole.getLenthHole());
    }

    gcs.addGolfCourse(gc1);
    System.out.println("\n\n");
  }
}


Abhinaba Chakraborty
  • 3,488
  • 2
  • 16
  • 37
-2

Can you annotate your test class with @ContextConfiguration and then run. Autowiring will not work unless the context is loaded .

Umesh Sanwal
  • 681
  • 4
  • 13