2

Now I look for my first job and I received a test task from one company. So, I did and sent it for review. I was refused because of a bad design code. Here is my code with the main method:

package com.lapinskyi.universitytest;

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

import com.lapinskyi.universitytest.console_menu.Menu;

@SpringBootApplication
public class Main {

    public static void main(String[] args) {
        
        ConfigurableApplicationContext context =  SpringApplication.run(Main.class, args);
        
        Menu menu = context.getBean(Menu.class);
        System.out.println("Hello!");
        
        boolean isContinue = true;
        
        while(isContinue) {
            menu.getMenu();
            menu.makeChoosenAction();
            isContinue = menu.isContinue();
        }
        
        System.out.println("Good bye");
    }

}

This code was commented like this:

It's not a good idea to work with ApplicationContext directly to get beans, you should use the Dependency injection mechanism of the Spring Framework for that (Menu class).

So, help me, please, how can I get a bean of the class Menu in static method main not using context directly?

Git repository

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    "So, help me, please, to understand how should I change my main method in order to fix the pointed problem?" - Is the problem that you would like to but don't know how to use DI to get the bean? – Jeff Scott Brown Jan 03 '22 at 18:36
  • Yes, I don't know how to get bean of class Menu in the main menu without using context. – Valentyn Lapinskyi Jan 03 '22 at 18:42

0 Answers0