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>: ...

November 24, 2024 · 1873 words · Mihyang Gu

Toby's Spring Chapter 7: Application of Core Spring Technologies

This chapter applies Spring’s three core technologies—IoC/DI, service abstraction, and AOP—to application development for creating novel functionality, thereby examining Spring’s development philosophy, pursued values, and requirements for Spring users. 7.1 Separating SQL and DAO Final UserDao improvement: Extracting SQL from DAO Runtime modifications to database tables, field names, or SQL statements necessitate DAO recompilation, proving impractical SQL Separation Methodologies XML configuration-based separation: Define SQL as XML property values for DAO injection SQL provision service: Create independent functionality providing SQL for DAO usage The second approach proves superior, enabling SQL storage in various formats beyond Spring configuration files and facilitating runtime modifications. ...

March 7, 2021 · 448 words · Mihyang Gu

Toby's Spring Chapter 8: What is Spring?

8.1 Spring’s Definition An open-source lightweight application framework facilitating Java enterprise development Application Framework Unlike libraries/frameworks specialized for specific domains or technologies Application frameworks encompass entire application scope comprehensively Spring originated from Rod Johnson’s J2EE technical book example code The book addressed design and development strategies across all J2EE application tiers Spring was developed incorporating core strategies for Java enterprise development across all tiers Lightweight Not heavy - Contrary to excessive engineering in technologies like EJB Spring operates on simplest server environments (Tomcat, Jetty) Code remains comparatively compact and simple Facilitating Java Enterprise Development ...

February 27, 2021 · 843 words · Mihyang Gu