springboot:condition

public class LinuxCondition implements Condition{

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return context.getEnvironment().getProperty("os.name").contains("Linux");  }
}
=============================================
public class MacCondition implements Condition{

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return context.getEnvironment().getProperty("os.name").contains("Mac");  }
}
=============================================
public class WindowsCondition implements Condition{

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return context.getEnvironment().getProperty("os.name").contains("Windows");
    }
}
=============================================
@Configuration
public class MyConfiguration {

    @Bean(name="emailerService")
    @Conditional(WindowsCondition.class)
    public Object windowsEmailerService(){
        return new Object();
    }

    @Bean(name="emailerService")
    @Conditional(LinuxCondition.class)
    public Object linuxEmailerService(){
        return new Object();
    }

    @Bean(name="emailerService")
    @Conditional(MacCondition.class)
    public Object macEmailerService(){
        return new Object();
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327105786&siteId=291194637