欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot:condition

程序员文章站 2022-06-16 08:09:48
...
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();
    }
}