|
| 1 | +package com.baeldung.dependencyinjectiontypes.app; |
| 2 | + |
| 3 | +import com.baeldung.dependencyinjectiontypes.annotation.CarQualifier; |
| 4 | +import com.baeldung.dependencyinjectiontypes.model.Car; |
| 5 | +import com.baeldung.dependencyinjectiontypes.model.CarHandler; |
| 6 | +import com.baeldung.dependencyinjectiontypes.model.Motorcycle; |
| 7 | +import com.baeldung.dependencyinjectiontypes.model.Vehicle; |
| 8 | +import org.springframework.boot.SpringApplication; |
| 9 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 10 | +import org.springframework.context.ConfigurableApplicationContext; |
| 11 | +import org.springframework.context.annotation.Bean; |
| 12 | +import org.springframework.context.annotation.ComponentScan; |
| 13 | + |
| 14 | +@SpringBootApplication |
| 15 | +@ComponentScan("com.baeldung.dependencyinjectiontypes.model") |
| 16 | +public class CustomConfiguration { |
| 17 | + @Bean |
| 18 | + @CarQualifier |
| 19 | + public Car getMercedes() { |
| 20 | + return new Car("E280", "Mercedes", "Diesel"); |
| 21 | + } |
| 22 | + |
| 23 | + public static void main(String[] args) throws NoSuchFieldException { |
| 24 | + ConfigurableApplicationContext context = SpringApplication.run(CustomConfiguration.class, args); |
| 25 | + CarHandler carHandler = context.getBean(CarHandler.class); |
| 26 | + carHandler.getVehicles().forEach(System.out::println); |
| 27 | + } |
| 28 | + |
| 29 | + @Bean |
| 30 | + @CarQualifier |
| 31 | + public Car getBmw() { |
| 32 | + return new Car("M5", "BMW", "Petrol"); |
| 33 | + } |
| 34 | + |
| 35 | + @Bean |
| 36 | + public Motorcycle getSuzuki() { |
| 37 | + return new Motorcycle("Yamaguchi", "Suzuki", true); |
| 38 | + } |
| 39 | +} |
0 commit comments