Exercise 7
This commit is contained in:
parent
559600ca77
commit
72de788456
|
@ -7,6 +7,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||
public class WorkshopApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.setProperty("my.custom.condition", "true");
|
||||
SpringApplication.run(WorkshopApplication.class, args);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
debug=true
|
||||
workshop.greeting.text=Gude
|
||||
workshop.greeting.type=logger
|
||||
workshop.greeting.type=none
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.workshop.magic.config;
|
||||
|
||||
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.stdout.StdOutGreetingService;
|
||||
|
||||
|
@ -32,4 +33,11 @@ public class GreetingAutoConfiguration {
|
|||
return new LoggerGreetingService(properties.getText());
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@MyCustomCondition
|
||||
@ConditionalOnClass(BeepGreetingService.class)
|
||||
GreetingService beepGreetingService(GreetingProperties properties) {
|
||||
return new BeepGreetingService(properties.getText());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue