下面的代码示例演示了如果当前服务的启动环境是windows系统,则不会注入Tyc这个类的实例到spring容器,如果不是windows系统,则会注入Tyc这个类的实例到spring容器
@Service
@Conditional(value = WindowsPlatformCondition.class)
public class Tyc{}
/*** 如果是windows平台,则不会往spring中注入某些类的实例对象** @author shiwentian* @see org.springframework.context.annotation.Condition* @since 14.7.2023**/
@Component
public class WindowsPlatformCondition extends SpringBootCondition {private static final boolean IS_WINDOWS = System.getProperties().getProperty("os.name").toLowerCase(Locale.ROOT).contains("windows");@Overridepublic ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {if (IS_WINDOWS) {return new ConditionOutcome(false, "windows platform");} else {return new ConditionOutcome(true, "windows platform");}}}