< 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.


@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" > 



config.beans.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:util="springframework.org/schema/util"
       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/context
       https://www.springframework.org/context/spring-context.xsd
       ">
           
       
<context:component-scan base-package="decodejava"/>

</beans>





Adding JARs







Execution


Finally, after executing Utility class, you will get the following output within the Console window. This output shown below, shows how the Utility class has used the ApplicationContext container of Spring Framework to load the configuration xml file - config.beans.xml, access the beans specified in it, instantiate the Tennis class and calls its methods.

Jul 14, 2018 11:56:57 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@1ee0005: 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?





Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< Spring First Program
Spring Dependency Injection >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
Python

Ad2