From 559600ca77ad6e426480c969a2a6232db559310f Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Tue, 13 May 2025 15:15:26 +0200 Subject: [PATCH] Exercise 6 --- app/app/src/main/resources/application.properties | 2 ++ .../magic/config/GreetingAutoConfiguration.java | 15 +++++++++------ .../workshop/magic/config/GreetingProperties.java | 3 +++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/app/app/src/main/resources/application.properties b/app/app/src/main/resources/application.properties index c00a613..c4f7376 100644 --- a/app/app/src/main/resources/application.properties +++ b/app/app/src/main/resources/application.properties @@ -1 +1,3 @@ debug=true +workshop.greeting.text=Gude +workshop.greeting.type=logger diff --git a/components/library-autoconfigure/src/main/java/com/workshop/magic/config/GreetingAutoConfiguration.java b/components/library-autoconfigure/src/main/java/com/workshop/magic/config/GreetingAutoConfiguration.java index c42abbd..8bc8fd9 100644 --- a/components/library-autoconfigure/src/main/java/com/workshop/magic/config/GreetingAutoConfiguration.java +++ b/components/library-autoconfigure/src/main/java/com/workshop/magic/config/GreetingAutoConfiguration.java @@ -7,26 +7,29 @@ import com.workshop.magic.service.stdout.StdOutGreetingService; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; @AutoConfiguration @ConditionalOnClass(GreetingService.class) +@EnableConfigurationProperties(GreetingProperties.class) public class GreetingAutoConfiguration { @Bean @ConditionalOnMissingBean @ConditionalOnClass(StdOutGreetingService.class) - @ConditionalOnMissingClass("com.workshop.magic.service.slf4j.LoggerGreetingService") - GreetingService stdOutGreetingService() { - return new StdOutGreetingService(); + @ConditionalOnProperty(name = "workshop.greeting.type", havingValue = "stdout", matchIfMissing = true) + GreetingService stdOutGreetingService(GreetingProperties properties) { + return new StdOutGreetingService(properties.getText()); } @Bean @ConditionalOnMissingBean @ConditionalOnClass(LoggerGreetingService.class) - GreetingService slf4jGreetingService() { - return new LoggerGreetingService(); + @ConditionalOnProperty(name = "workshop.greeting.type", havingValue = "logger") + GreetingService slf4jGreetingService(GreetingProperties properties) { + return new LoggerGreetingService(properties.getText()); } } diff --git a/components/library-autoconfigure/src/main/java/com/workshop/magic/config/GreetingProperties.java b/components/library-autoconfigure/src/main/java/com/workshop/magic/config/GreetingProperties.java index 08c800a..cc9dd9b 100644 --- a/components/library-autoconfigure/src/main/java/com/workshop/magic/config/GreetingProperties.java +++ b/components/library-autoconfigure/src/main/java/com/workshop/magic/config/GreetingProperties.java @@ -1,5 +1,8 @@ package com.workshop.magic.config; +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "workshop.greeting") public class GreetingProperties { private String text = "Hello"; private Type type = Type.STDOUT;