How to Inject Spring List-Type Beans in a Specific Order
intro Although I use Spring regularly, I sometimes get confused with configurations in less familiar situations. Recently, while reviewing bean configurations in a project, I encountered a case where both a List type and the type of objects inside the List (for convenience, I’ll refer to them as List<T> type and T type) were configured, as shown in the code below. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 @Bean List<Cake> availableCakes() { return List.of( lemonCake(), chocolateCake(), strawberryCake() ); } @Bean Cake chocolateCake() { return new Cake("Chocolate"); } @Bean Cake strawberryCake() { return new Cake("Strawberry"); } @Bean Cake lemonCake() { return new Cake("Lemon"); } Code that receives and uses List<Cake>: ...