package com.mongoubiubiu.condition; import org.springframework.beans.factory.support.BeanDefinitionDefaults; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.core.type.AnnotationMetadata; import com.mongoubiubiu.bean.RainBow; public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar{ /** * AnnotationMetadata: 当前类的注解信息 * BeanDefinitionRegistry:BeanDefinition的注册类 */ @Override public void registerBeanDefinitions(AnnotationMetadata arg0, BeanDefinitionRegistry registry) { boolean redExit = registry.containsBeanDefinition("com.mongoubiubiu.bean.Red"); boolean blueExit = registry.containsBeanDefinition("com.mongoubiubiu.bean.Yellow"); if(redExit && blueExit){ RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(RainBow.class); //指定bean名 registry.registerBeanDefinition("RainBow", rootBeanDefinition); } } }