Program to demonstrate how to turn on Auto Scanning of Spring Components for automatic scanning, detecting and instantiating the beans
Spring Framework container uses Spring Configuration XML file for scanning , detecting and instantiating of beans / components. Spring Framework now provides us a way to automate this whole process of dealing with beans. Instead of dealing with beans and components manually, now we can ask Spring container to scan into a particular package and see if there are any classes with specific annotations for getting registered as a bean. By-default Auto Scanning of beans is turned off.
In order to turn on Auto Scanning we need to provide an entry in Spring Configuration file. The entry is just a tag with name "context:component-scan". As soon as we provide Spring this tag, it gets capabilities of automatically discovering and declaring beans.
This tag is very much similar to the tag "context:annotation-config". The only difference between the two is that "context:annotation-config" does not scans the beans in package.
Tag "context:component-scan" scans package and all its sub-packages for classes having specific annotations such as @Component , @Service etc to register that class as a Spring bean. This tag comes up with a attribute called as base-package, which tells the container from where it has to start scanning.
There are usually four major annotations for which the "context:component-scan" tag scans :
1. @Component - This annotation tells "context:component-scan" tag that its a class which can be treated as Spring Component.
2. @Service - This annotation tells "context:component-scan" tag that its a class which can be treated as Service.
3. @Repository - This annotation tells "context:component-scan" tag that its a class which can be treated as data repository.
4. @Controller - This annotation tells "context:component-scan" tag that its a class which can be treated as Controller for Spring Model-View-Controller.
How to provide entry for "context:component-scan" tag in Spring Configuration File ?
Above XML contains an entry for "context:component-scan" which makes Spring container detect, scan and instantiate bean automatically. By above example, the container looks in the package com.hubberspot.components and all its sub-packages for the classes having specific components declared above.
Spring Framework container uses Spring Configuration XML file for scanning , detecting and instantiating of beans / components. Spring Framework now provides us a way to automate this whole process of dealing with beans. Instead of dealing with beans and components manually, now we can ask Spring container to scan into a particular package and see if there are any classes with specific annotations for getting registered as a bean. By-default Auto Scanning of beans is turned off.
In order to turn on Auto Scanning we need to provide an entry in Spring Configuration file. The entry is just a tag with name "context:component-scan". As soon as we provide Spring this tag, it gets capabilities of automatically discovering and declaring beans.
This tag is very much similar to the tag "context:annotation-config". The only difference between the two is that "context:annotation-config" does not scans the beans in package.
Tag "context:component-scan" scans package and all its sub-packages for classes having specific annotations such as @Component , @Service etc to register that class as a Spring bean. This tag comes up with a attribute called as base-package, which tells the container from where it has to start scanning.
There are usually four major annotations for which the "context:component-scan" tag scans :
1. @Component - This annotation tells "context:component-scan" tag that its a class which can be treated as Spring Component.
2. @Service - This annotation tells "context:component-scan" tag that its a class which can be treated as Service.
3. @Repository - This annotation tells "context:component-scan" tag that its a class which can be treated as data repository.
4. @Controller - This annotation tells "context:component-scan" tag that its a class which can be treated as Controller for Spring Model-View-Controller.
How to provide entry for "context:component-scan" tag in Spring Configuration File ?
| ||
Above XML contains an entry for "context:component-scan" which makes Spring container detect, scan and instantiate bean automatically. By above example, the container looks in the package com.hubberspot.components and all its sub-packages for the classes having specific components declared above.