Exercise 7

This commit is contained in:
Moritz Halbritter 2025-05-13 15:30:30 +02:00
parent 559600ca77
commit 72de788456
5 changed files with 48 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
public class WorkshopApplication { public class WorkshopApplication {
public static void main(String[] args) { public static void main(String[] args) {
System.setProperty("my.custom.condition", "true");
SpringApplication.run(WorkshopApplication.class, args); SpringApplication.run(WorkshopApplication.class, args);
} }
} }

View File

@ -1,3 +1,3 @@
debug=true debug=true
workshop.greeting.text=Gude workshop.greeting.text=Gude
workshop.greeting.type=logger workshop.greeting.type=none

View File

@ -1,6 +1,7 @@
package com.workshop.magic.config; package com.workshop.magic.config;
import com.workshop.magic.service.GreetingService; import com.workshop.magic.service.GreetingService;
import com.workshop.magic.service.slf4j.BeepGreetingService;
import com.workshop.magic.service.slf4j.LoggerGreetingService; import com.workshop.magic.service.slf4j.LoggerGreetingService;
import com.workshop.magic.service.stdout.StdOutGreetingService; import com.workshop.magic.service.stdout.StdOutGreetingService;
@ -32,4 +33,11 @@ public class GreetingAutoConfiguration {
return new LoggerGreetingService(properties.getText()); return new LoggerGreetingService(properties.getText());
} }
@Bean
@ConditionalOnMissingBean
@MyCustomCondition
@ConditionalOnClass(BeepGreetingService.class)
GreetingService beepGreetingService(GreetingProperties properties) {
return new BeepGreetingService(properties.getText());
}
} }

View File

@ -0,0 +1,16 @@
package com.workshop.magic.config;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Conditional;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional({OnCustomCondition.class})
@interface MyCustomCondition {
}

View File

@ -0,0 +1,22 @@
package com.workshop.magic.config;
import java.util.Locale;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
class OnCustomCondition extends SpringBootCondition {
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
String value = System.getProperty("my.custom.condition");
if (value == null) {
return ConditionOutcome.noMatch("No 'my.custom.condition' system property found");
}
if (value.toLowerCase(Locale.ROOT).equals("true")) {
return ConditionOutcome.match("'my.custom.condition' system property is true");
}
return ConditionOutcome.noMatch("'my.custom.condition' system property is '%s'".formatted(value));
}
}