Spring OnContion
实现一个自己得conditon
public class WindowsCondition implements Condition {
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return context.getEnvironment().getProperty("os.name").contains("Windows");
}
}
当条件为真时才会创建Bean
@Configuration
public class Config {
@Bean
@Conditional(WindowsCondition.class)
public ListService window() {
return new WindowsService();
}
@Bean
@Conditional(LinuxCondition.class)
public ListService linux() {
return new LinuxService();
}
}
@ConditionalOnBean(仅仅在当前上下文中存在某个对象时,才会实例化一个Bean)
@ConditionalOnClass(某个class位于类路径上,才会实例化一个Bean)
@ConditionalOnExpression(当表达式为true的时候,才会实例化一个Bean)
@ConditionalOnMissingBean(仅仅在当前上下文中不存在某个对象时,才会实例化一个Bean)
@ConditionalOnMissingClass(某个class类路径上不存在的时候,才会实例化一个Bean)
@ConditionalOnNotWebApplication(不是web应用)