< Prev
Next >

Spring Program With Annotations

Today we are going to understand how to put a simple Spring project into execution using the Annotations without configuring the beans in configuration file(.xml). For this, we will use the Eclipse Integrated Development Environment(IDE) application.

As you know that Spring Framework allows us to develop scalable and maintainable enterprise applications while keeping the code simple and loosely coupled. So, in the next section, we are going to create a Java class and we will not configure its bean in a configuration xml file, instead the beans will be created, loaded and instantiated using some specific Spring Annotations.

@Component - A Spring Annotation

Before we proceed with our Spring project, we need to make you understand about an important Spring Annotation - @Component.

class Tennis

The @Component annotation is placed on the top of each Java class to configure it as a java bean to be wired with another class in a Spring project. For example, in the code above we have placed @Component Annotation just above the Tennis class, doing so, will make Spring Framework treat this class as a java bean and will automatic the creation, loading, instantiation and deletion of its beans.

In the absence of @Component annotation, Spring Framework would take the class as just a regular java class, without configuring it as a java bean.

Creating a Java class in our Spring Project

This configuration document is an Extensible Markup Language(XML) file, ending with .xml extension, and we have named it config.beans.xml. In this configuration file, we don't need to configure a Tennis instance with a unique id like we did in the example Spring Program without Annotations, but we only need to specify a mechanism that automatically looks for the classes with @Component Annotation and loads it as a bean.

Spring Framework provides a tag <context:component-scan> which allows us to provide a package which is searched for the classes annotated with @Component Annotation and register these classes as a bean with the Spring Container. This tag has an attribute base-package which allows us to specify the a single or multiple packages to search for the classes annotated with @Component Annotation.

For example, our Tennis class is annotated with @Component Annotation and it is created in the package decodejava hence we will specify its package as a value for base-package attribute.

 <context:component-scan base-package="decodejava" > 

<?xml version="1.0" encoding="utf-8"?>

<beans xmlns=""
<context:component-scan base-package="decodejava"/>


Adding JARs

  • We are going to add some JARs files to our Java project. These JARs are required in order to successfully execute a Spring project.

    All these JARs are included in the folder named libs(within our Spring installation folder). So, we need to add all the JARs in the libs folder to our build path of our Java project.

    Note : Those who don't know how to add JARs to the build path of your Java project in Eclipse IDE, please refer to our section Adding JARs to your Spring project in Eclipse.


Finally, after executing Utility class, you will get the following output within the Console window. This output shown below displays how the Utility class has instantiated the Tennis class and calls its method.

Jul 14, 2018 11:56:57 AM prepareRefresh
INFO: Refreshing startup date [Sat Jul 14 11:56:57 IST 2018]; root of context hierarchy
Jul 14, 2018 11:56:57 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [config.beans.xml]
Hello World! Do you like Tennis?

The Utility class has used the ApplicationContext container of Spring Framework to load the configuration xml file - config.beans.xml, in which we haven't configured the bean with a unique id(like we did in the example Spring Program without Annotations) but have rather used an important tag i.e. <context:component-scan>, which looks for the classes with @Component Annotation and loads it as a bean.

Please share this article -

< Prev
Next >
< Spring First Program
Spring Dependency Injection >


Please Subscribe

Please subscribe to our social media channels for daily updates.

Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page



Please check our latest addition