根据条件动态创建bean
public class TestConditon implements Condition { public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { //获取IOC容器使用的BeanFactory ConfigurableListableBeanFactory beanFactory=context.getBeanFactory(); //获取类加载器 ClassLoader classLoader=context.getClassLoader(); //获取资源加载器 ResourceLoader resourceLoader = context.getResourceLoader(); //获取系统环境 Environment environment = context.getEnvironment(); String osName = environment.getProperty("os.name"); if (osName.contains("Windows")){ return true; } return false; } }
是windows系统才创建Bean
@Configuration public class MainConfig { @Conditional(TestConditon.class) @Bean public Person person(){ return new Person(); } }