×
☰ See All Chapters

Spring @Order Annotation

Spring 4.0 supports the ordering of injected bean to a collection through @Order annotation. This feature may be used to add beans in a specific order into a collection (i.e. via @Autowired). The value attribute of @Order annotation accepts integer value which represents the precedence. Lower number has the higher the precedence. Lower numbers indicate a higher priority.

Let us see an example for spring @Order annotation.

Spring Order Annotation Example

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

        <modelVersion>4.0.0</modelVersion>

        <groupId>com.java4coding</groupId>

        <artifactId>Spring4.0_ORDER_to_Order_Beans_in_Collection</artifactId>

        <version>0.0.1-SNAPSHOT</version>

 

        <dependencies>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-core</artifactId>

                        <version>${spring.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-context</artifactId>

                        <version>${spring.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-aspects</artifactId>

                        <version>${spring.version}</version>

                </dependency>

        </dependencies>

 

        <properties>

                <spring.version>4.2.4.RELEASE</spring.version>

        </properties>

 

</project>

Car.java

package com.java4coding.car;

 

public interface Car {

        public abstract void printCarName();

}

Audi.java

package com.java4coding.car;

 

import org.springframework.core.annotation.Order;

import org.springframework.stereotype.Component;

 

@Component

@Order(value=2)

public class Audi implements Car{

 

        public void printCarName() {

                System.out.println("Audi");

        }

}

Ferrari.java

package com.java4coding.car;

 

import org.springframework.core.annotation.Order;

import org.springframework.stereotype.Component;

 

@Component

@Order(value=1)

public class Ferrari implements Car{

 

        public void printCarName() {

                System.out.println("Ferrari");

        }

}

Demo.java

package com.java4coding.demo;

 

import java.util.List;

 

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

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

 

import com.java4coding.car.Car;

 

@Configuration

@ComponentScan(basePackages = { "com.java4coding.car" })

public class Demo {

        @Autowired

        List<Car> carList;

       

        public static void main(String[] args) {

                AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(

                                Demo.class);

                Demo demo = (Demo)ctx.getBean("demo");

                demo.carList.stream().forEach(car -> car.printCarName());

                ctx.close();

        }

}

 

Project Directory Structure

spring-order-annotation-0
 

Output:

spring-order-annotation-1
 
spring-order-annotation-2
 

All Chapters
Author