Pin It

How to turn on Auto Scanning of Spring Components for automatic scanning, detecting and instantiating the beans ?.

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 ?


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <!-- In order to turn on Auto Scanning in Spring we need to provide a tag 
        by name <context:component-scan> see the tag below .... -->

    <context:component-scan base-package="com.hubberspot.spring.components" />

</beans>


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.
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot