From 904e9b9def386497e092d2f9deaf8d2db29114a2 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Wed, 28 Dec 2022 19:20:51 +0100 Subject: [PATCH] refactor: Moved configuration in a separate class and made it more explicit --- .../chat/backend/ChatBackendApplication.java | 7 ---- .../backend/ChatBackendConfiguration.java | 32 +++++++++++++++++++ .../persistence/InMemoryChatroomFactory.java | 2 -- .../InMemoryPersistenceStrategy.java | 7 ---- 4 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java diff --git a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendApplication.java b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendApplication.java index f8c38b63..7f6f213f 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendApplication.java +++ b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendApplication.java @@ -10,13 +10,6 @@ import java.time.Clock; @SpringBootApplication public class ChatBackendApplication { - @Bean - public Clock clock() - { - return Clock.systemDefaultZone(); - } - - public static void main(String[] args) { SpringApplication.run(ChatBackendApplication.class, args); diff --git a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java new file mode 100644 index 00000000..34036dda --- /dev/null +++ b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java @@ -0,0 +1,32 @@ +package de.juplo.kafka.chat.backend; + +import de.juplo.kafka.chat.backend.domain.ChatroomFactory; +import de.juplo.kafka.chat.backend.persistence.InMemoryChatroomFactory; +import de.juplo.kafka.chat.backend.persistence.InMemoryPersistenceStrategy; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.time.Clock; + + +@Configuration +public class ChatBackendConfiguration +{ + @Bean + ChatroomFactory chatroomFactory(InMemoryPersistenceStrategy persistenceStrategy) + { + return new InMemoryChatroomFactory(persistenceStrategy); + } + + @Bean + InMemoryPersistenceStrategy persistenceStrategy() + { + return new InMemoryPersistenceStrategy(); + } + + @Bean + public Clock clock() + { + return Clock.systemDefaultZone(); + } +} diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryChatroomFactory.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryChatroomFactory.java index 4f33b110..b2b7f1df 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryChatroomFactory.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryChatroomFactory.java @@ -4,11 +4,9 @@ import de.juplo.kafka.chat.backend.domain.Chatroom; import de.juplo.kafka.chat.backend.domain.ChatroomFactory; import de.juplo.kafka.chat.backend.domain.PersistenceStrategy; import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Component; import java.util.UUID; -@Component @RequiredArgsConstructor public class InMemoryChatroomFactory implements ChatroomFactory { diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryPersistenceStrategy.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryPersistenceStrategy.java index f6c76fa9..454419ed 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryPersistenceStrategy.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryPersistenceStrategy.java @@ -3,22 +3,15 @@ package de.juplo.kafka.chat.backend.persistence; import de.juplo.kafka.chat.backend.domain.Message; import de.juplo.kafka.chat.backend.domain.MessageMutationException; import de.juplo.kafka.chat.backend.domain.PersistenceStrategy; -import lombok.Getter; import lombok.RequiredArgsConstructor; -import lombok.Value; import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import reactor.core.publisher.Sinks; import java.time.LocalDateTime; import java.util.LinkedHashMap; -import java.util.UUID; -import java.util.stream.Stream; -@Component @RequiredArgsConstructor @Slf4j public class InMemoryPersistenceStrategy implements PersistenceStrategy -- 2.20.1