Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

Spring Framework 4.3.4 @Order Annotation Tutorial – Sort Order for an Annotated Bean Component

$
0
0

Web MVC framework – Spring is the best web controller framework and configuration model for Java based programming enterprise application.

We do have more than 40 Spring MVC tutorials on Crunchify so far. In this tutorial we will go over @Order Annotation. What is the use of @Order in spring? Ordering aspects with Spring AOP & MVC.

Here are the short steps:

  • We are going to create Dynamic Web Project
  • Create Spring Config file crunchify-bean.xml
  • Convert it to Maven Project
  • Create Beans with Order Annotation
  • Create test-case and execute

Detailed Steps: Let’s get started

Step-1

  1. Go to Eclipse
  2. Click on File
  3. Click on New
  4. Click on Dynamic Web Project

Create Dynamic Web Project - Eclipse - Crunchify Tips

Step-2

  1. Provide Project name: CrunchifySpringMVC4OrderAnnotation
  2. Provide Target runtime. Mainly Apache Tomcat location in Eclipse.
  3. Choose Dynamic Web Module version: 3.1

Target Runtime 8.0 and Dynamic Web Module 3.1

Step-3

  1. Right click on project
  2. Click on Configure
  3. Convert project to Maven project

Convert Project to Maven

Step-4

Choose default setting and click Finish.

Maven Group ID and Artifact ID for Maven Project

Step-5

Here is a project structure before we start.

Spring-MVC-@Order-Annotation-Tutorial-Java-Eclipse-Project-Structure-Crunchify-Spring Config File

Open pom.xml file and add Spring MVC 4.3.4 dependency.

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>4.3.4.RELEASE</version>
</dependency>

here is my complete pom.xml file

<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>CrunchifySpringMVC4OrderAnnotation</groupId>
  <artifactId>CrunchifySpringMVC4OrderAnnotation</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-context</artifactId>
  		<version>4.3.4.RELEASE</version>
  	</dependency>
  </dependencies>
</project>

Step-6

  1. Right click on Java Resources
  2. Click on New
  3. Click on Source Folder and provide name: resources

Create resource folder under Java Resources

Step-7

Create file crunchify-bean.xml file under resource folder. Here is a complete file content.

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	<context:annotation-config />
	
	<!-- Specify Bean ID "orders" -->
	<bean id="orders" class="com.crunchify.spring.tutorials.CrunchifyPrintResult" />
	
	<!-- This is required and loads each class under below package -->
	<context:component-scan base-package="com.crunchify.spring.tutorials" />
</beans>

Step-8

Now we will create 5 files with @Order annotation.

  1. CrunchifyCompany.java Interface
  2. CrunchifyGoogle1.java  ==> With @Order(1)
  3. CrunchifyFacebook2.java ==> With @Order(2)
  4. CrunchifyYahoo3.java ==> With @Order(3)
  5. CrunchifyPrintResult.java

CrunchifyCompany.java

package com.crunchify.spring.tutorials;

/**
 * @author Crunchify.com
 * 
 */

public interface CrunchifyCompany {
	// do nothing here
}

CrunchifyGoogle1.java

package com.crunchify.spring.tutorials;

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

/**
 * @author Crunchify.com
 * 
 */

@Component
@Order(1) // @Order defines the sort order for an annotated component. The value() is optional and represents an order value as
			// defined in the Ordered interface. Lower values have higher priority. The default value is
			// Ordered.LOWEST_PRECEDENCE, indicating lowest priority (losing to any other specified order value).
public class CrunchifyGoogle1 implements CrunchifyCompany {

	private String order = "Crunchify Google with Order-1";

	public String toString() {
		return "Class Name: " + this.getClass().getSimpleName() + " - Result: " + this.order;
	}
}

CrunchifyFacebook2.java

package com.crunchify.spring.tutorials;

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

@Component
@Order(2)
public class CrunchifyFacebook2 implements CrunchifyCompany {

	private String order = "Crunchify Facebook with Order-2";

	public String toString() {
		return "Class Name: " + this.getClass().getSimpleName() + " - Result: " + this.order;
	}
}

CrunchifyYahoo3.java

package com.crunchify.spring.tutorials;

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

@Component
@Order(3)
public class CrunchifyYahoo3 implements CrunchifyCompany {

	private String order = "Crunchify Yahoo with Order-3";

	public String toString() {
		return "Class Name: " + this.getClass().getSimpleName() + " - Result: " + this.order;
	}
}

CrunchifyPrintResult.java

package com.crunchify.spring.tutorials;

import java.util.List;

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

/**
 * @author Crunchify.com
 * 
 */

@Component
public class CrunchifyPrintResult {
	@Autowired
	private List<CrunchifyCompany> order;
	private String result = "";

	public String toString() {
		order.stream().forEach((temp) -> {
			this.result = result + temp + "\n"; // print result and add new line
		});
		return this.result;
	}
}

Step-9

Now let’s create testcase CrunchifyOrderTest.java

package com.crunchify.spring.tests;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.crunchify.spring.tutorials.CrunchifyPrintResult;

/**
 * @author Crunchify.com
 * 
 */

public class CrunchifyOrderTest {

	@SuppressWarnings("resource")
	public static void main(String[] args) {

		// Load Spring ApplicationContext file crunchify-beans.xml
		ApplicationContext context = new ClassPathXmlApplicationContext("crunchify-beans.xml");
		
		// get the bean which we specified in file crunchify-beans.xml file
		CrunchifyPrintResult results = (CrunchifyPrintResult) context.getBean("orders");
		
		// After loading each class - just print result
		System.out.println(results);
	}
}

Step-10

Now just right click on file CrunchifyOrderTest.java and Run As -> Java Application. You should see result printed in the order which we specified Order.

Class Name: CrunchifyGoogle1 - Result: Crunchify Google with Order-1
Class Name: CrunchifyFacebook2 - Result: Crunchify Facebook with Order-2
Class Name: CrunchifyYahoo3 - Result: Crunchify Yahoo with Order-3

Spring @Order Annotation Example by Crunchify

Have a suggestion on article? Please chime in and share it as a comment.

The post Spring Framework 4.3.4 @Order Annotation Tutorial – Sort Order for an Annotated Bean Component appeared first on Crunchify.

Author: App Shah

Crunchify, LLC Logo


Viewing all articles
Browse latest Browse all 1037

Trending Articles