Home > Spring, Spring AOP concepts > Spring Bean Automatic Detection using context:component-scan element

Spring Bean Automatic Detection using context:component-scan element

Introduction:

In Spring 3.0, the IOC container can be configured in two ways, They are

  • XML Based Configuration
  • Java Based Configuration.

If we go for XML Based Configuration, the sizes of the xml configuration file is so excess for large applications. But we can reduce the size of the xml configuration file by using Spring’s one of the greatest aspect, automatic detection of Spring beans(classes).

This can be achieved by using context:component-scan element in xml configuration file and @component annotation in bean classes. This article deals with spring bean automatic detection using context:component-scan element. Let’s get over that.

Prerequisites:

JDK 1.5 and Above

Your favourite IDE

spring latest  jars

commons-logging.jar

aopalliance.jar

Creation of component using @Component annotation:

By using @component annotation,we can make the bean class as component.By the same way,the below operator bean class make it as component.

Operator.java


package com.bsj.componentscan;

import org.springframework.stereotype.Component;

/**
 * @author Jhothi
 */
@Component("operator")
public class Operator
{
 public int add(int i,int j)
 {
 return i+j;
 }
 public int subtract(int i,int j)
 {
 return i-j;
 }

 public int multiply(int i,int j)
 {
 return i*j;
 }

 public int divide(int i,int j)
 {
 return i/j;
 }
}

By using auto wired feature of spring we can represent our Operator class as dependency of Calculator class.

By using @Autowired annotation we can make the dependency Operator class as Auto wired one.

Calculator.java


package com.bsj.componentscan;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author Jhothi
 */
@Component("calculator")
public class Calculator
{
 @Autowired
 private Operator operator;
 public void calculation(int i, int j)
 {
 System.out.println("i+j : " + operator.add(i, j));
 System.out.println("i-j : " + operator.subtract(i, j));
 System.out.println("i*j : " + operator.multiply(i, j));
 System.out.println("i/j : " + operator.divide(i, j));
 }

 /**
 * @param operator the operator to set
 */
 public void setOperator(Operator operator)
 {
 this.operator = operator;
 }
}

Note:
The component annotation has the name of the component with in the bracket with quotes. The component name is used for detect the bean by the method getBean() of ApplicationContext.For ex,In @Component (“calculator”), calculator is the name of the component.

Container Configuration:

The IOC container configuration xml file is shown below,The container has the <context:component-scan> element and <context:annotation-config/>

<context:annotation-config/> used to intimate the beans of this IOC container are annotation supported.

By pass the base path of the beans as the value of the base-package attribute of context:component-scan element, we can detect the beans and registering their bean definitions automatically without lots of overhead.

The value of base-package attribute is fully qualified package name of the bean classes. We can pass more than one package names by comma separated like the below one.


<context:component-scan base-package="package1, package2"/>

applicationContext.xml:


<?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:p="http://www.springframework.org/schema/p"
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd">

 <context:component-scan base-package="com.bsj.componentscan"/>
 <context:annotation-config/>
</beans>

Finally the @component is one of the stereotype annotations. Some other stereotype annotations are avail. They are @Repository, @Service, and @Controller. The next article deals the stereo type annotaions.Before that if you feel this article is helpful to you doesn’t forget to leave your valuable comments.

  1. miriyala
    December 9, 2011 at 5:31 am

    very good explanation and every one should suppose to know this when using spring

  2. Naveen
    March 21, 2012 at 3:16 am

    Very Nice explanations. It helped ! Thanks

  3. Saumya Darshan
    March 21, 2012 at 12:56 pm

    Very good and simple explanation

  4. Venkat
    April 18, 2012 at 7:23 am

    Thanks alot for exposing this information..

  5. Vichai VUN
    June 27, 2012 at 1:12 am

    Good explanation.

  6. sanjeev
    October 1, 2012 at 6:50 am

    short and sweet explanation!! thanks

  7. Sachin
    February 4, 2013 at 5:37 pm

    very pricise

  8. Mahesh
    February 22, 2013 at 4:45 am

    superb

  1. No trackbacks yet.

Leave a comment