"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : Spring Core - Component Scan Date : 22/03/2025 (Session - 10) _____________________________________________________________________________________________________________________________ Yesterday Session ================= * different annotations have been used for Java Based Configuration 1) @configuration 2) @Bean 3) @Import 4) @ImportResource 5) @PropertySource 6) @Value 7) @Autowired 8) @Qualifier Today session : Component Scan ============================== > Automatically IOC Container will recognize the Spring Beans from packages of Project > In the Component Scan Feature as programmer we no need to configure any Java Class as Spring Bean in spring configuration file (or) Java based configuration class > To Activate the Component Scan feature for Spring Application for below tag. xml configuration ----> Java Based Configuration ----> @Componentscan > To Make Java class as spring bean we need to use either any one of stero type annotations as below 1) @Controller 2) @Service 3) @Repository 4) @Component * The above annotations are used in the application development according to layered component @Service >> To Mark Java class as Spring Bean Class and it can be used for only Business classes. @Controller >> To Mark Java Class as Spring Bean class and it can be used for only Handling the Requests in webapplication. @Repoistory >> To Mark Java Class as spring bean class and it can be used only for DAO Classes. @Component >> To Mark Java Class as spring bean class and it can be used for both Helper and Utilities classes in Application. +++++++ Example +++++++ com |-> ashokit |-> controller ------------------------------>We can have the classes which annotated @Controller Annotation |-> *.java |-> services -------------------------------->We can have the classes which annotated @Service Annotation |-> *.java |-> dao-------------------------------------->We can have the classes which annotated @Repoistory Annotation |-> *.java |-> helper----------------------------------->We can have the classes which annotated @Component Annotation |-> *.java |-> util------------------------------------->We can have the classes which annotated @Component Annotation |-> *.java NOTE ==== Here base package will be "com.ashokit" the IOC Container will scan the packages from this package and its sub packages as well. Example Application =================== UserController.java =================== package com.ashokit.controller; import org.springframework.stereotype.Controller; @Controller public class UserController { public UserController() { System.out.println("Constructor From UserController...."); } } CustomerController.java ======================= package com.ashokit.controller; import org.springframework.stereotype.Controller; @Controller public class CustomerController { public CustomerController() { System.out.println("Constructor From CustomerController...."); } } CustomerDaoImpl.java ==================== package com.ashokit.dao; import org.springframework.stereotype.Repository; @Repository public class CustomerDaoImpl { public CustomerDaoImpl() { System.out.println("Constructor From CustomerDaoImpl...."); } } UserDaoImpl.java ================ package com.ashokit.dao; import org.springframework.stereotype.Repository; @Repository public class UserDaoImpl { public UserDaoImpl() { System.out.println("Constructor From UserDaoImpl...."); } } CustomerHelper.java =================== package com.ashokit.helper; import org.springframework.stereotype.Component; @Component public class CustomerHelper { public CustomerHelper() { System.out.println("Constructor From CustomerHelper...."); } } UserHelper.java ================ package com.ashokit.helper; import org.springframework.stereotype.Component; @Component public class UserHelper { public UserHelper() { System.out.println("Constructor From UserHelper"); } } CustomerService.java ==================== package com.ashokit.service; import org.springframework.stereotype.Service; @Service public class CustomerService { public CustomerService() { System.out.println("Constructor From Customer Service...."); } } UserService.java ================ package com.ashokit.service; import org.springframework.stereotype.Service; @Service public class UserService { public UserService() { System.out.println("Constrcutor from userservice...."); } } DateUtils.java ============== package com.ashokit.utils; import org.springframework.stereotype.Component; @Component public class DateUtils { public DateUtils() { System.out.println("Constructor From DateUtils..."); } } StringUtils.java ================ package com.ashokit.utils; import org.springframework.stereotype.Component; @Component public class StringUtils { public StringUtils() { System.out.println("Constructor From StringUtils"); } } spring.xml ========== App.java ======== package com.ashokit; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ashokit.controller.UserController; import com.ashokit.dao.UserDaoImpl; import com.ashokit.service.UserService; import com.ashokit.utils.DateUtils; public class App { public static void main( String[] args ){ //Activate the Container ApplicationContext context = new ClassPathXmlApplicationContext("com/ashokit/spring/config/spring.xml"); UserController userController = context.getBean(UserController.class); System.out.println(userController); UserService userService = context.getBean(UserService.class); System.out.println(userService); UserDaoImpl UserDaoImpl = context.getBean(UserDaoImpl.class); System.out.println(UserDaoImpl); DateUtils dateUtils = context.getBean(DateUtils.class); System.out.println(dateUtils); /*String[] beanNames = context.getBeanDefinitionNames(); Arrays.stream(beanNames).forEach(beanName -> System.out.println(beanName));*/ } } ApplicationConfig.java ====================== package com.ashokit.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration //Enabling the component scan for java based configuration @ComponentScan(basePackages = "com.ashokit") public class ApplicationConfig { } OUTPUT ====== Constructor From CustomerController.... Constructor From UserController.... Constructor From CustomerDaoImpl.... Constructor From UserDaoImpl.... Constructor From CustomerHelper.... Constructor From UserHelper Constructor From Customer Service.... Constrcutor from userservice.... Constructor From DateUtils... Constructor From StringUtils com.ashokit.controller.UserController@740cae06 com.ashokit.service.UserService@26d9b808 com.ashokit.dao.UserDaoImpl@f78a47e com.ashokit.utils.DateUtils@644baf4a