👍Java Spring Boot & Microservice Realtime Project development CT👍
👍Java Spring Boot & Microservice Realtime Project development CT👍
June 12, 2025 at 09:48 AM
*4 Tasks for today: 12Jun* ✅ 1. Get strong understanding of Core Spring concepts: Spring Container, IOC, Dependency Injection, Dependency Lookup, Container types, Spring Bean ✅ 2. Make MathServiceImpl a Spring Bean, then dependency inject it with field injection & constructor inject to controller. ✅ 3. Perform Dependency Lookup. Controller looks up service by dependency lookup. ✅ 4. Let spring manage predefined "java.uil.Random" class object as Spring Bean. ============================ 1. Get strong understanding of Core Spring concepts: Spring Container, IOC, Dependency Injection, Dependency Lookup, Container types, Spring Bean Prepare strongly on this, and also practice by drawing in diagram. Spring Container: The core component of Spring that manages the lifecycle and configuration of application objects (beans). IoC (Inversion of Control): A design principle where the control of object creation and dependency management is transferred to the Spring framework. Dependency Injection (DI): A technique where Spring automatically supplies an object’s dependencies rather than the object creating them itself. Dependency Lookup: A process where objects retrieve their dependencies manually from the Spring container. Container Types: Refers to the types of Spring containers like BeanFactory and ApplicationContext that manage beans and configurations. Spring Bean: An object managed by the Spring container, created, wired, and maintained according to the application context configuration. --------------------------------- 2. Make MathServiceImpl a Spring Bean, then dependency inject it with field injection & constructor inject to controller. - On MathServiceImpl add @Component. Notice spring creates the object. - In controller define field: private MathService mathService; - For field injection add @Autowired. Notice spring give the reference to mathService & we can use it in method. - For constructor inject, @Autowired is optional. You can add to constructor or remove it. public MathController(MathService mathService) { logger.info("MathController initialized.*****"); this.mathService = mathService; logger.info("mathService: {}", mathService); } Now note there are several annotations, which makes java class as spring bean. they are all linkedin to @Component. They are @Service, @Controller, @RestController, @Repository, @Configuration Now for MathServiceImpl, use @Service - since it represents business logic. ----------------------------- 3. Perform Dependency Lookup. Controller looks up service by dependency lookup. Dependency Lookup dependency object will not be given automatically, rather, you need to ask & get. In controller we need to ask for MathServiceImpl object from spring container. How to implement Dependency Lookup Spring has an object of ApplicationContext, which is available in spring container. Using this object, we can access any other object from spring container. In MathController, we need to ask for MathService We will dependency inject ApplicationContext object in MathController private ApplicationContext applicationContext; public MathController(ApplicationContext applicationContext) { logger.info("MathController initialized.*****"); this.applicationContext = applicationContext; logger.info("ApplicationContext: {}", applicationContext); } Using this ApplicationContext object we can lookup the MathService object. getBean mathService = applicationContext.getBean(MathService.class); ================== 4. Let spring manage predefined "java.uil.Random" class object as Spring Bean. In order to achieve this we need to use @Configuration & @Bean Create AppConfig class like below @Configuration public class AppConfig { @Bean Random makeRandom() { return new Random(); } } - Note @Configuration is also a @Component, when spring creats object of this, then it reads all methods with @Bean, and invokes them. - Whatever those methods return, spring starts managing them in spring container. - You can now dependency inject & dependency lookup those. - Inject this Random into Controller & experiment with it.

Comments