Spring Boot @Order
last modified July 6, 2020
Spring Boot @Order tutorial shows how to order beans with @Order annotation.
Spring is a popular Java application framework and Spring Boot is an evolution of Spring that helps create stand-alone, production-grade Spring based applications easily.
@Order
@Order
defines the sort order for an annotated component.
The value()
is optional and represents an order value.
Lower values have higher priority.
Spring Boot @Order example
The following application orders the execution of beans implementing CommandLineRunner
.
pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ Application.java │ │ MyRunner.java │ │ MyRunner2.java │ └───resources └───test └───java
This is the project structure.
<?xml version="1.0" encoding="UTF-8"?> <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.zetcode</groupId> <artifactId>springbootorder</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <java.version>13</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.4.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
This is the Maven pom.xml
file.
package com.zetcode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Component @Order(value = 2) public class MyRunner implements CommandLineRunner { private static final Logger logger = LoggerFactory.getLogger(MyRunner.class); @Override public void run(String... args) { logger.info("Running MyRunner"); } }
The bean is started when the application starts. With the @Order
annotation we give it a priority level.
package com.zetcode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Component @Order(value = 1) public class MyRunner2 implements CommandLineRunner { private static final Logger logger = LoggerFactory.getLogger(MyRunner2.class); @Override public void run(String... args) { logger.info("Running MyRunner2"); } }
This is MyRunner2
. It has a higher priority set with @Order
,
so it is executed before MyRunner
.
package com.zetcode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
Application
is the entry point which sets up Spring Boot
application.
$ mvn -q spring-boot:run ... 2020-03-30 09:17:27.552 INFO 3444 --- [ main] com.zetcode.MyRunner2 : Running MyRunner2 2020-03-30 09:17:27.553 INFO 3444 --- [ main] com.zetcode.MyRunner : Running MyRunner
We run the application.
In this tutorial, we have shown how to use @Order
annotation to
set the order of execution of beans.