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
@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
We are going to create a java class called Tennis within the decodejava package and we are going to use
the @Component annotation on the top of this Java class to configure it as a java bean.
Tennis.java
package decodejava;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Tennis
{
public void message()
{
System.out.println("Hello World! Do you like Tennis?");
}
}
Adding the Utility class that calls the Spring API
Next, we are going to create another class named - Utility, which is a simple java class and we won't annotate it with @Component annotation to configure it as a bean.
The Utility class uses the ApplicationContextcontainer(an interface) of Spring Framework by
creating its instance using its implemented class FileSystemXmlApplicationContext to -
Load the configuration xml file - config.beans.xml,
To access the bean specified in the configuration file by calling the getBean() method of ApplicationContextcontainer,
Instantiate the Tennis class and call its methods.
Utility.java
package decodejava;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Utility
{
public static void main(String[] args)
{
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:config.beans.xml");
Tennis tennisBean = context.getBean("TennisBean", Tennis.class);
tennisBean.playSport();
}
}
Adding a configuration file
Next, we are going to add a configuration file to our project and
we are going to name this configuration file as config.beans.xml.
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.
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.
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?