Spring Automatically Discovering Beans Using @Component Annotations

Pre-requisite trails

Spring Auto-Wiring Using Annotations

Concept Overview

Typically in Spring, dependency injection is achieved using
bean
,
constructor-arg
and
property
tags.

However, in large applications, the number of beans will increase and the corresponding XML written to configure the numerous beans will become very large and unwieldly.

Spring provides a feature called ‘Auto-Wiring’ that minimizes the XML to be written provided that certain assumptions are made about the nomenclature of beans and properties. Using ‘Auto-Wiring’ feature, the number of tags like
constructor-arg
and
property
is significantly minimized and the size of XML is reduced considerably for large applications.

However, in ‘Auto-wiring’ the bean definitions using
bean
tag still need to be defined within spring-config.xml.

Spring provides a way to avoid declaring even the bean definitions within spring-config.xml thereby automatically discovering beans to be injected.

Automatic discovery of beans is based on the following rules:

  • Use context:annotation-config tag in spring-config.xml to let Spring use Annotations
  • Use context:component-scan tag in spring-config.xml and tell Spring the package in which to look for auto-discovering beans
  • Use @Component annotation to mark a class as a Spring auto-discoverable bean

If
@Component
annotation is used, then the bean declarations need not be declared in spring-config.xml and this minimizes XML to a great extent.

Sample Program Overview

The sample program showcases the automatic discovery of beans by Spring framework using
@Component
annotation.

We will create the Person class with members as name and email. We will then annotate the Person class with
@Component
annotation.

We will also create the spring-config.xml and use the
context:annotation-config
tag in spring-context.xml to let Spring know that annotation based auto-wiring will be used.. We will also use the
context:component-scan
tag and provide the name of the package for Spring to look into for auto-discovery.

Finally, we will test our setup using TestAnnotationAutoDiscovery class which will load Spring context and get a reference to Person class. We will print name and email of the Person bean on the console to verify that automatic discovery of beans using
@Component
annotation has occured successfully.

Required Libraries
  • com.springsource.javax.inject-0.9.0.PFD.jar
  • commons-logging.jar
  • log4j.jar
  • org.springframework.aop.jar
  • org.springframework.asm.jar
  • org.springframework.beans.jar
  • org.springframework.context.jar
  • org.springframework.context.support.jar
  • org.springframework.core.jar
  • org.springframework.expression.jar



Source Code

Create the Person (see sample code below).

Add
@Component
annotation on Person class (see line 5 below).

Create members name and email (see lines 9-10 below)

Create accessor methods for name and email (see lines 11-22 below).

Create the spring-config.xml file (see below).

Let Spring know that annotation based auto-wiring will be used by declaring the tag
context:annotation-config
tag(see line 14 below)

Declare the tag
context:component-scan
tag and provide the name of the package that Spring should look into for automatic discovery of beans(see line 16 below)

Note that there are no bean declarations in spring-config.xml file thereby highlighting the automatic bean discovery mechanism of Spring.


Finally, we need a java program to test the our setup.This is done by TestAnnotationAutoDiscovery (see source code below).

We need to tell Spring framework to use the ‘spring-config.xml’ to load our beans (see line 11 below).

We get the reference to Person class through Spring using the bean name ‘person’ (see line 12 below).

We access the getName() and getEmail() methods of Person class and print the output to verify that automatic discovery of beans using
@Component
annotation has occured successfully (see lines 13-14 below).


Running Sample Program

This sample program has been packaged as a jar installer which will copy the source code (along with all necessary dependencies) on your machine and automatically run the program for you as shown in the steps below. To run the sample program, you only need Java Runtime Environment (JRE) on your machine and nothing else.

Download And Automatically Run Sample Program
  • Save the springannotationautodiscovery-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment


  • (Alternatively you can go the folder containing the springannotationautodiscovery-installer.jar and execute the jar using
    java -jar springannotationautodiscovery-installer.jar
    command)

  • You will see a wizard as shown below
  • Enter the location of the directory where you want the program to install and run (say, C:\Temp)
  • The installer will copy the program on your machine and automatically execute it. The expected output indicating that the program has run successfully on your machine is shown in the image below.
  • Browsing the Program

    This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called
    springannotationautodiscovery
    . All the required libraries have also been downloaded and placed in the same location. You can open this project from Eclipe IDE and directly browse the source code. See below for details of the project structure.

    Leave a Comment