Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions spring-core-order-annotation/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?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">
<parent>
<artifactId>spring-framework-tutorial-parent</artifactId>
<groupId>com.jstobigdata</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-core-order-annotation</artifactId>
<description>Use of Spring's @Order annotation</description>
<dependencies>

<!-- Step-1 Add the dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>

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

<!-- Test Dependencies-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.jstobigdata</groupId>
<artifactId>spring-tutorial-boms</artifactId>
<type>pom</type>
<scope>import</scope>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>

<!-- For Junit-jupiter-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.jsbd.order;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import com.jsbd.order.model.NotificationChannel;

@Configuration
@ComponentScan("com.jsbd.order")
public class ApplicationConfig {


@Bean
@Order(2)
public NotificationChannel email(){
// System.out.println("Notification Channel - Email");
return new NotificationChannel("Email");
}

@Bean
@Order(4)
public NotificationChannel twitter(){
// System.out.println("Notification Channel - Twitter");
return new NotificationChannel("Twitter");
}

@Bean
@Order(3)
public NotificationChannel slack(){
// System.out.println("Notification Channel - Slack");
return new NotificationChannel("Slack");
}

@Bean
@Order(1)
public NotificationChannel sms(){
// System.out.println("Notification Channel - Sms");
return new NotificationChannel("Sms");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.jsbd.order.model;

public class NotificationChannel {

private String name;

public NotificationChannel(String channelName) {
this.name = channelName;
}

public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.jsbd.order.service;

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

@Service
@Order(3)
public class EmailNotification implements NotificationHandler {

public EmailNotification() {
System.out.println("EmailNotification Service Created.");
}

@Override
public void send() {
System.out.println("Email Notification Handler");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.jsbd.order.service;

public interface NotificationHandler {
void send();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.jsbd.order.service;

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

@Service
@Order(2)
class SlackNotification implements NotificationHandler {

public SlackNotification() {
System.out.println("SlackNotification Service created.");
}

@Override
public void send() {
System.out.println("Slack Notification Handler");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.jsbd.order.service;

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

@Service
@Order(1)
class SmsNotification implements NotificationHandler {

public SmsNotification() {
System.out.println("SmsNotification Service created.");
}

@Override
public void send() {
System.out.println("SMS Notification Handler");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.jsbd.order.service;

import org.springframework.stereotype.Service;

@Service
// Not an Ordered Service, will be the last in sequence
class TwitterNotification implements NotificationHandler {

public TwitterNotification() {
System.out.println("TwitterNotification Service created.");
}

@Override
public void send() {
System.out.println("Twitter Direct Message Notification Handler");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.jsbd.order;

import java.util.List;

import com.jsbd.order.model.NotificationChannel;
import com.jsbd.order.service.NotificationHandler;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

@SpringJUnitConfig(ApplicationConfig.class)
public class TestOrderOnCollectionInjection {

@Autowired
private List<NotificationHandler> notificationHandlers;

@Autowired
private List<NotificationChannel> notificationChannels;

@Test
public void testAllChannelSendingOrder() {
notificationHandlers.forEach(NotificationHandler::send);
}

@Test
public void testNotificationChannelBeanOrder(){
notificationChannels.forEach(channel -> System.out.println(channel.getName()));
}

}