public interface GameInterface {void play();}
@Component
@Primary
public class PacmanGame implements GameInterface {
@Override
public void play() {System.out.println("Playing Pacman game...");}
}
@Component("superContraGame")
public class SuperContraGame implements GameInterface {
@Override
public void play() {System.out.println("Playing Super Contra game...");}
}
@Component
public class GameRunner {
@Autowired
private GameInterface pacmanGame;
@Autowired
@Qualifier("superContraGame")
private GameInterface superContraGame;
public void runGames() {
System.out.println("Running Pacman game:");
pacmanGame.play();
System.out.println("Running Super Contra game:");
superContraGame.play();
}
}
@Configuration
@ComponentScan(basePackages = "your.package.name")
public class Main {
public static void main(String[] args) {
try (var context = new AnnotationConfigApplicationContext(Main.class)) {
GameRunner gameRunner = context.getBean(GameRunner.class);
gameRunner.runGames();
}
}
}