Learn How component scanning works in Spring framework

0
4507
Component Scanning

Generally, programmers tend to declare the beans or components in a bean file of XML configuration, in order to allow the Spring container to detect and register them. However, you should know that Spring is capable of auto scanning, detecting and further, instantiating beans from the already defined project package. So, that can save you from the tedious task of having to declare beans in a separate XML file. This is what we are going to look at in this tutorial.

What is automatic discovery of beans?
When working with Spring, dependency injection can be used by using bean, constructor arguments and property tags. However, when we are encountering large applications, it is obvious that the number of beans increase and the likewise, the amount of XML to be written can become unwieldy and large.

So, Spring provides a feature known as Auto-Wiring, using which, the amount of XML can be minimized considering that some assumptions can be made about the naming of beans and properties. By using this feature, the number of tags, like property and constructor-arg, are reduced to a large extent and the size of the XML can also be reduced for large applications.

And, despite the fact that bean tags that are auto-wired still require to be defined in the spring-config.xml file, Spring can help in avoiding bean definitions as well. Thus, you have a way of automatically discovering beans to be injected.

The automatic discovery of beans is governed by certain rules. These are –

  • The usage of the tag context:annotation-tag in the spring-config.xml allows Spring to use Annotations.
  • Using context:component-scan tag in the spring-config.xml tells Spring the package where it can auto-discover beans.
  • The use of @Component annotation is to mark a class as an auto-discoverable bean for Spring.

So, you can see that if the @Component annotation is used then there is no need to declare beans in the spring-config.xml file, thus keeping the XML light as well.

Why automatic component scan is important?
Automatic component scan is largely important since it helps reduce the amount of XML, as there is no need to declare bean definitions separately in the spring-config.xml file. Also, since you do not have to declare bean definitions separately, it does save a lot of time and makes the process simple.

What are the annotation types for automatic component scan?
As per Spring 2.5, there are four types of auto components scan annotations. These are –

  • @Component – Meant for an auto scan component.
  • @Repository – Indication of DAO component in the persistence layer.
  • @Service – Indication of service component in the business layer.
  • @Controller – This indicates controller component in the presentation layer.

The use of just @Component annotation can be used to annotate the other three annotations. So, this is the most preferred choice when writing code.

How automatic discovery is achieved in Spring framework?
All of the four annotations that have been mentioned above can be set for automatic canning and discovery. The process to do so is rather simple.
You will need to use the context:component-scan tag in the spring-cofig.xml file to enable the automatic scanning feature.

Environment setup:-
Setting up the environment for Spring based application development involves just three major steps.

  • Setting up Java Development Kit
    Download JDK from the Oracle site and then, install and configure it. Then, set the PATH and JAVA_HOME environment variables.
  • Setting of Eclipse IDE
    Eclipse can be downloaded from the official website. Once downloaded, unpack the binaries and then set the PATH as well.
  • Setting up Spring libraries
    The Spring libraries may be obtained from http://repo.spring.io/release/org/springframework/spring/. Again, it is important to set the CLASSPATH correctly.
    Now your environment is ready to start developing Spring based Java applications.

Sample application: –
In this section we will write annotation based beans and then use the component scan in the XML file to identify the beans to the Spring container.
First is the company bean with default values mentioned by using annotations.

Listing 1: – This is the company bean

This is the product bean with a simple getter and setter methods.

Listing 2: – This is the product bean

This is the most important part containing the instruction for auto-scanning of components in the package. You can use multiple packages or any other package location to enable auto-scan. It instructs the container to find the related beans and then integrate them as and when required. It does the actual magic in spring application.

The instruction syntax is as shown below. It scans all the beans under com.eduonix.autoscan package.

Listing 3: – This is the XML configuration file

This is the main file to test the application.

Listing 4: – This is the main Java file

Now, run the application and it will show the following output
Run the Application
Conclusion
Defining beans separately in the configuration file can be hectic job for any programmer. Automatic component scanning in Spring is a very useful feature that can help a programmer to cut down on massive XML files for applications that are large.

This tutorial shows you how to setup and further explains how to implement auto component scanning with the help of a simple example.

LEAVE A REPLY

Please enter your comment!
Please enter your name here