- Switched the existing `@Configuration`-classes into an auto-configuration.
- Added a `@ConditionalOnProperty`, that loades the default-configuration.
- Added an Auto-Configuration for the `MongoDbStorageStrategy`, which is
inactive by default and can be switched on by the property
`chat.backend.storage` to `mongodb`.
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
-import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
import java.time.Clock;
@Configuration
@EnableConfigurationProperties(ChatBackendProperties.class)
-@EnableReactiveMongoRepositories
public class ChatBackendConfiguration
{
@Bean
--- /dev/null
+package de.juplo.kafka.chat.backend.persistence.inmemory;
+
+import de.juplo.kafka.chat.backend.ChatBackendProperties;
+import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.time.Clock;
+
+
+@ConditionalOnProperty(
+ prefix = "chat.backend",
+ name = "services",
+ havingValue = "in-memory",
+ matchIfMissing = true)
+@Configuration
+public class InMemoryServicesAutoConfiguration
+{
+ @Bean
+ InMemoryChatHomeService chatHomeService(
+ StorageStrategy storageStrategy,
+ Clock clock,
+ ChatBackendProperties properties)
+ {
+ return new InMemoryChatHomeService(
+ storageStrategy.read(),
+ clock,
+ properties.getChatroomBufferSize());
+ }
+}
+++ /dev/null
-package de.juplo.kafka.chat.backend.persistence.inmemory;
-
-import de.juplo.kafka.chat.backend.ChatBackendProperties;
-import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-import java.time.Clock;
-
-
-@Configuration
-public class InMemoryServicesConfiguration
-{
- @Bean
- InMemoryChatHomeService chatHomeService(
- StorageStrategy storageStrategy,
- Clock clock,
- ChatBackendProperties properties)
- {
- return new InMemoryChatHomeService(
- storageStrategy.read(),
- clock,
- properties.getChatroomBufferSize());
- }
-}
--- /dev/null
+package de.juplo.kafka.chat.backend.persistence.storage.files;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import de.juplo.kafka.chat.backend.ChatBackendProperties;
+import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
+import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomService;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.nio.file.Paths;
+import java.time.Clock;
+
+
+@ConditionalOnProperty(
+ prefix = "chat.backend",
+ name = "storage",
+ havingValue = "files",
+ matchIfMissing = true)
+@Configuration
+public class FilesStorageAutoConfiguration
+{
+ @Bean
+ public StorageStrategy storageStrategy(
+ ChatBackendProperties properties,
+ Clock clock,
+ ObjectMapper mapper)
+ {
+ return new FilesStorageStrategy(
+ Paths.get(properties.getStorageDirectory()),
+ clock,
+ properties.getChatroomBufferSize(),
+ messageFlux -> new InMemoryChatRoomService(messageFlux),
+ mapper);
+ }
+}
+++ /dev/null
-package de.juplo.kafka.chat.backend.persistence.storage.files;
-
-import com.fasterxml.jackson.databind.ObjectMapper;
-import de.juplo.kafka.chat.backend.ChatBackendProperties;
-import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
-import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomService;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-import java.nio.file.Paths;
-import java.time.Clock;
-
-
-@Configuration
-public class FilesStorageConfiguration
-{
- @Bean
- public StorageStrategy storageStrategy(
- ChatBackendProperties properties,
- Clock clock,
- ObjectMapper mapper)
- {
- return new FilesStorageStrategy(
- Paths.get(properties.getStorageDirectory()),
- clock,
- properties.getChatroomBufferSize(),
- messageFlux -> new InMemoryChatRoomService(messageFlux),
- mapper);
- }
-}
--- /dev/null
+package de.juplo.kafka.chat.backend.persistence.storage.mongodb;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import de.juplo.kafka.chat.backend.ChatBackendProperties;
+import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
+import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomService;
+import de.juplo.kafka.chat.backend.persistence.storage.files.FilesStorageStrategy;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
+
+import java.nio.file.Paths;
+import java.time.Clock;
+
+
+@ConditionalOnProperty(
+ prefix = "chat.backend",
+ name = "mongodb",
+ havingValue = "files")
+@Configuration
+@EnableReactiveMongoRepositories
+public class MongoDbStorageConfiguration
+{
+ @Bean
+ public StorageStrategy storageStrategy(
+ ChatBackendProperties properties,
+ Clock clock,
+ ObjectMapper mapper)
+ {
+ return new FilesStorageStrategy(
+ Paths.get(properties.getStorageDirectory()),
+ clock,
+ properties.getChatroomBufferSize(),
+ messageFlux -> new InMemoryChatRoomService(messageFlux),
+ mapper);
+ }
+}
--- /dev/null
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
+de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryServicesAutoConfiguration,\
+de.juplo.kafka.chat.backend.persistence.storage.files.FilesStorageAutoConfiguration