@Order in Spring

This commit is contained in:
Umesh Awasthi
2018-07-27 22:42:06 -07:00
parent 1cffbf2ce7
commit 0e9e10d85c
9 changed files with 171 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
<?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.javadevjournal</groupId>
<artifactId>spring-order-annotation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-order-annotation</name>
<description>Spring @Order annotation example</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,12 @@
package com.javadevjournal;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringOrderAnnotationApplication {
public static void main(String[] args) {
SpringApplication.run(SpringOrderAnnotationApplication.class, args);
}
}

View File

@@ -0,0 +1,10 @@
package com.javadevjournal.car;
public interface Car {
/**
* This method is responsible for recommending car to customer based on our algorithm
* @return Recommended car
*/
String getCarRecommendation();
}

View File

@@ -0,0 +1,14 @@
package com.javadevjournal.car;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(2)
public class Honda implements Car {
@Override
public String getCarRecommendation() {
return "Honda";
}
}

View File

@@ -0,0 +1,14 @@
package com.javadevjournal.car;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(1)
public class Toyota implements Car {
@Override
public String getCarRecommendation() {
return "Toyota";
}
}

View File

@@ -0,0 +1,15 @@
package com.javadevjournal.car;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(Ordered.LOWEST_PRECEDENCE)
public class UsedCar implements Car {
@Override
public String getCarRecommendation() {
return "Certified Car";
}
}

View File

@@ -0,0 +1,30 @@
package com.javadevjournal;
import com.javadevjournal.car.Car;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest
public class OrderAnnotationTest {
@Autowired
private List<Car> cars;
@Test
public void test_spring_order_annotation(){
assertThat(cars.get(0).getCarRecommendation(),is(equalTo("Toyota")));
assertThat(cars.get(1).getCarRecommendation(),is(equalTo("Honda")));
assertThat(cars.get(2).getCarRecommendation(),is(equalTo("Certified Car")));
}
}

View File

@@ -0,0 +1,16 @@
package com.javadevjournal;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringOrderAnnotationApplicationTests {
@Test
public void contextLoads() {
}
}