// GameInterface.java public interface GameInterface {void play();} // PacmanGame.java (Beans from this class are independent) @Component @Primary public class PacmanGame implements GameInterface { @Override public void play() {System.out.println("Playing Pacman game...");} } // SuperContraGame.java (Beans from this class are independent) @Component("superContraGame") public class SuperContraGame implements GameInterface { @Override public void play() {System.out.println("Playing Super Contra game...");} } // GameRunner.java (Beans from this class are dependent on any class that implements GameInterface) @Component public class GameRunner { @Autowired // Spring automatically provide the bean of @Primary(top-priority) class "PacmanGame" as dependency connection/argument to it private GameInterface pacmanGame; @Autowired @Qualifier("superContraGame") // Spring automatically provide the bean of class component with name "superContraGame" as argument to it private GameInterface superContraGame; public void runGames() { System.out.println("Running Pacman game:"); pacmanGame.play(); System.out.println("Running Super Contra game:"); superContraGame.play(); } } // Main.java (This is the main application launcher) @Configuration @ComponentScan(basePackages = "your.package.name") // Helps to find where all components are located to set up dependency tree public class Main { public static void main(String[] args) { try (var context = new AnnotationConfigApplicationContext(Main.class)) { // Points to itself (Autoonfiguration with component scan) GameRunner gameRunner = context.getBean(GameRunner.class); gameRunner.runGames(); } } }