spring

Spring @Order Annotation Tutorial

Welcome readers, in this tutorial, we will explore the @Order annotation of the spring framework.

1. Introduction

  • Spring Boot is a module that provides rapid application development feature to the spring framework including auto-configuration, standalone-code, and production-ready code
  • It creates applications that are packaged as jar and are directly started using an embedded servlet container (such as Tomcat, Jetty or Undertow). Thus, no need to deploy the war files
  • It simplifies the maven configuration by providing the starter template and helps to resolve the dependency conflicts. It automatically identifies the required dependencies and imports them in the application
  • It helps in removing the boilerplate code, extra annotations, and xml configurations
  • It provides a powerful batch processing and manages the rest endpoints
  • It provides an efficient jpa-starter library to effectively connect the application with the relational databases

1.1 @Order annotation in spring

@Order annotation in spring defines the sorting order of the component classes. It is available in spring framework since version 2.0 but Spring 4.0 introduced enhancements. Earlier this annotation only supported the AspectJ advice, but later it introduced support to the ordering of autowired components in collections like List and Arrays.

  • Internally, @Order annotation uses OrderComparator class in xml based spring applications and AnnotationAwareOrderComparator class in annotation based spring applications
  • This annotation contains one attribute which accepts the integer values. Lowest values have the highest precedence and come first in list or array

To understand the above concept, let us open the eclipse ide and implement the @Order annotation in the spring framework using spring boot.

2. Spring @Order Annotation Tutorial

Here is a systematic guide for implementing this tutorial.

2.1 Tools Used

We are using Eclipse Kepler SR2, JDK 8, and Maven.

2.2 Project Structure

In case you are confused about where you should create the corresponding files or folder, let us review the project structure of the spring boot application.

Spring @Order Annotation - Application Structure
Fig. 1: Application Structure

2.3 Project Creation

This section will demonstrate how to create a Java-based Maven project with Eclipse. In Eclipse IDE, go to File -> New -> Maven Project.

Spring @Order Annotation - Maven Project
Fig. 2: Create a Maven Project

In the New Maven Project window, it will ask you to select a project location. By default, ‘Use default workspace location’ will be selected. Select the ‘Create a simple project (skip archetype selection)’ checkbox and just click on the next button to proceed.

Spring @Order Annotation - Project Details
Fig. 3: Project Details

It will ask you to ‘Enter the group and the artifact id for the project’. We will input the details as shown in the below image. The version number will be by default: 0.0.1-SNAPSHOT.

Spring @Order Annotation - Archetype Parameters
Fig. 4: Archetype Parameters

Click on Finish and the creation of a maven project is completed. If you observe, it has downloaded the maven dependencies and a pom.xml file will be created. It will have the following code:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.spring.order.annotation.tutorial</groupId>
	<artifactId>Springorderannotationtutorial</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</project>

We can start adding the dependency that developers want like spring boot, aop etc. Let us start building the application!

3. Creating a Spring Boot application

Below are the steps involved in developing the application.

3.1 Maven Dependency

Here, we specify the dependency for the spring boot and aop. Maven will automatically resolve the other dependencies. The updated file will have the following code.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.spring.order.annotation.tutorial</groupId>
	<artifactId>Springorderannotationtutorial</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	
	<name>Spring @Order annotation tutorial</name>
	<description>A tutorial to understand the order annotation in spring framework</description>
	
	<!-- spring boot parent dependency jar. -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
	</parent>

	<dependencies>
		<!-- spring boot jar. -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
	</dependencies>
	<build>
		<finalName>Springorderannotationtutorial</finalName>
	</build>
</project>

3.2 Java Classes

Let us assume that we are developing an application using Spring 5. We have a set of company classes and use a list or array to store these company names in a result component using autowiring. Following classes are required to understand the @Order annotation.

3.2.1 Apple Class

Add the following code to this class.

Myapple.java

package com.ducat.spring.order.model;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(value= 2)
public class Myapple implements Mycompany {

	private String name = "Myapple with Order-2";

	public String toString(){
		return this.name;
	}	
}

3.2.2 Nokia Class

Add the following code to this class.

Mynokia.java

package com.ducat.spring.order.model;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(value= 1)
public class Mynokia implements Mycompany {

	private String name = "Mynokia with Order-1";

	public String toString(){
		return this.name;
	}	
}

3.2.3 Samsung Class

Add the following code to this class.

Mysamsung.java

package com.ducat.spring.order.model;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(value= 3)
public class Mysamsung implements Mycompany {

	private String name = "Mysamsung with Order-3";

	public String toString(){
		return this.name;
	}	
}

3.2.4 Result Class

Add the following code to this class.

Myresults.java

package com.ducat.spring.order;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ducat.spring.order.model.Mycompany;

@Component
public class Myresults {

	@Autowired
	private List<Mycompany> companynames;

	public String toString(){
		return companynames.toString();
	}
}

3.2.5 Implementation/Main Class

Let us write the implementation/main class involved in this application. This class is the entry point of the spring boot application containing @SpringBootApplication annotation and the static main method.

Myapplication.java

package com.ducat.spring.order;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Myapplication {

	public static void main(String[] args) {

		ConfigurableApplicationContext context = SpringApplication.run(Myapplication.class, args);

		// Fetching the company names with order from the application context.				
		Myresults res = context.getBean(Myresults.class);
		System.out.println(res);

		// Closing the context object.
		context.close();
	}
}

4. Run the Application

To execute the application, right click on the Myapplication.java class, Run As -> Java Application.

Spring @Order Annotation - Run the Application
Fig. 5: Run the Application

5. Project Demo

The code shows the following logs as the output of this tutorial.

2019-02-21 19:17:40.664  INFO 4144 --- [           main] com.ducat.spring.order.Myapplication     : Started Myapplication in 1.142 seconds (JVM running for 1.644)

[Mynokia with Order-1, Myapple with Order-2, Mysamsung with Order-3]

That is all for this tutorial and I hope the article served you whatever you were looking for. Happy Learning and do not forget to share!

6. Conclusion

In this section, developers learned how to create a simple spring aop application. That is all for this tutorial and I hope the article served you whatever you were looking for. Happy Learning and do not forget to share! Developers can download the sample application as an Eclipse project in the Downloads section.

7. Download the Eclipse Project

This was an example of Order annotation in the spring aop module.

Download
You can download the full source code of this example here: Spring @Order Annotation Tutorial

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button