Java Code Examples for org.springframework.beans.factory.support.BeanDefinitionRegistry#containsBeanDefinition()

The following examples show how to use org.springframework.beans.factory.support.BeanDefinitionRegistry#containsBeanDefinition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BindingBeansRegistrar.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata,
		BeanDefinitionRegistry registry) {
	AnnotationAttributes attrs = AnnotatedElementUtils.getMergedAnnotationAttributes(
			ClassUtils.resolveClassName(metadata.getClassName(), null),
			EnableBinding.class);
	for (Class<?> type : collectClasses(attrs, metadata.getClassName())) {
		if (!registry.containsBeanDefinition(type.getName())) {
			BindingBeanDefinitionRegistryUtils.registerBindingTargetBeanDefinitions(
					type, type.getName(), registry);
			BindingBeanDefinitionRegistryUtils
					.registerBindingTargetsQualifiedBeanDefinitions(ClassUtils
							.resolveClassName(metadata.getClassName(), null), type,
							registry);
		}
	}
}
 
Example 2
Source File: AopConfigUtils.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private static BeanDefinition registerOrEscalateApcAsRequired(Class<?> cls, BeanDefinitionRegistry registry, Object source) {
	Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
	if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) {
		BeanDefinition apcDefinition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME);
		if (!cls.getName().equals(apcDefinition.getBeanClassName())) {
			int currentPriority = findPriorityForClass(apcDefinition.getBeanClassName());
			int requiredPriority = findPriorityForClass(cls);
			if (currentPriority < requiredPriority) {
				apcDefinition.setBeanClassName(cls.getName());
			}
		}
		return null;
	}
	RootBeanDefinition beanDefinition = new RootBeanDefinition(cls);
	beanDefinition.setSource(source);
	beanDefinition.getPropertyValues().add("order", Ordered.HIGHEST_PRECEDENCE);
	beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	registry.registerBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME, beanDefinition);
	return beanDefinition;
}
 
Example 3
Source File: MybatisAuditingRegistrar.java    From spring-data-mybatis with Apache License 2.0 6 votes vote down vote up
@Override
protected void registerAuditListenerBeanDefinition(
		BeanDefinition auditingHandlerDefinition, BeanDefinitionRegistry registry) {
	if (!registry.containsBeanDefinition(MYBATIS_MAPPING_CONTEXT_BEAN_NAME)) {
		registry.registerBeanDefinition(MYBATIS_MAPPING_CONTEXT_BEAN_NAME, //
				new RootBeanDefinition(MybatisMappingContextFactoryBean.class));
	}
	BeanDefinitionBuilder builder = BeanDefinitionBuilder
			.rootBeanDefinition(AuditingEntityListener.class);
	builder.addPropertyValue("auditingHandler", ParsingUtils
			.getObjectFactoryBeanDefinition(getAuditingHandlerBeanName(), null));
	builder.addPropertyReference("sqlSessionTemplate",
			DEFAULT_SQL_SESSION_TEMPLATE_BEAN_NAME);
	registerInfrastructureBeanWithId(builder.getRawBeanDefinition(),
			AuditingEntityListener.class.getName(), registry);
}
 
Example 4
Source File: DDDBeanRegistryPostProcessor.java    From flower with Apache License 2.0 6 votes vote down vote up
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
  Set<String> serviceTypes = new HashSet<>();
  Class<?> services[] =
      new Class<?>[] {CommandHandlerService.class, EventHandlerService.class, QueryHandlerService.class,
          DDDStartService.class, DDDEndService.class, DefaultCommandGateway.class, DefaultQueryGateway.class};

  for (Class<?> service : services) {
    BeanDefinition beanDefinition = new RootBeanDefinition(service);
    String handlerServiceName = BeanDefinitionReaderUtils.generateBeanName(beanDefinition, registry);
    registry.registerBeanDefinition(handlerServiceName, beanDefinition);
    if (Service.class.isAssignableFrom(service)) {
      serviceTypes.add(service.getName());
    }
  }

  final String dddFactoryName = "dddFactory";
  if (registry.containsBeanDefinition(dddFactoryName)) {
    BeanDefinition dddFactoryBeanDefinition = registry.getBeanDefinition(dddFactoryName);
    dddFactoryBeanDefinition.getPropertyValues().add("serviceTypes", serviceTypes);
  }

}
 
Example 5
Source File: ProfileXmlBeanDefinitionTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private static Matcher<BeanDefinitionRegistry> containsBeanDefinition(final String beanName) {
	return new TypeSafeMatcher<BeanDefinitionRegistry>() {

		@Override
		public void describeTo(Description desc) {
			desc.appendText("a BeanDefinitionRegistry containing bean named ")
				.appendValue(beanName);
		}

		@Override
		public boolean matchesSafely(BeanDefinitionRegistry beanFactory) {
			return beanFactory.containsBeanDefinition(beanName);
		}

	};
}
 
Example 6
Source File: RefreshAutoConfiguration.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)
		throws BeansException {
	bindEnvironmentIfNeeded(registry);
	for (String name : registry.getBeanDefinitionNames()) {
		BeanDefinition definition = registry.getBeanDefinition(name);
		if (isApplicable(registry, name, definition)) {
			BeanDefinitionHolder holder = new BeanDefinitionHolder(definition,
					name);
			BeanDefinitionHolder proxy = ScopedProxyUtils
					.createScopedProxy(holder, registry, true);
			definition.setScope("refresh");
			if (registry.containsBeanDefinition(proxy.getBeanName())) {
				registry.removeBeanDefinition(proxy.getBeanName());
			}
			registry.registerBeanDefinition(proxy.getBeanName(),
					proxy.getBeanDefinition());
		}
	}
}
 
Example 7
Source File: RpcDefinitionPostProcessor.java    From joyrpc with Apache License 2.0 6 votes vote down vote up
/**
 * 注册
 *
 * @param registry BeanDefinitionRegistry
 * @param config   配置
 * @param defName  默认名称
 * @param <T>
 */
protected <T extends AbstractIdConfig> String register(final BeanDefinitionRegistry registry, final T config,
                                                       final String defName) {
    if (config == null) {
        return null;
    }
    String beanName = config.getId();
    if (isEmpty(beanName)) {
        beanName = defName;
    }
    if (!registry.containsBeanDefinition(beanName)) {
        RootBeanDefinition definition = new RootBeanDefinition((Class<T>) config.getClass(), () -> config);
        //避免Spring警告信息
        definition.setRole(RootBeanDefinition.ROLE_INFRASTRUCTURE);
        registry.registerBeanDefinition(beanName, definition);
    } else {
        throw new BeanInitializationException("duplication bean name " + beanName);
    }
    return beanName;
}
 
Example 8
Source File: GspAutoConfiguration.java    From grails-boot with Apache License 2.0 5 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    if(removeDefaultViewResolverBean) {
        if(registry.containsBeanDefinition("defaultViewResolver")) {
            registry.removeBeanDefinition("defaultViewResolver");
        }
    }
    if(replaceViewResolverBean) {
        if(registry.containsBeanDefinition("viewResolver")) {
            registry.removeBeanDefinition("viewResolver");
        }
        registry.registerAlias("gspViewResolver", "viewResolver");
    }
}
 
Example 9
Source File: BeanRegistrar.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
/**
 * Register Infrastructure Bean
 *
 * @param beanDefinitionRegistry {@link BeanDefinitionRegistry}
 * @param beanType               the type of bean
 * @param beanName               the name of bean
 */
public static void registerInfrastructureBean(BeanDefinitionRegistry beanDefinitionRegistry,
                                              String beanName,
                                              Class<?> beanType) {

    if (!beanDefinitionRegistry.containsBeanDefinition(beanName)) {
        RootBeanDefinition beanDefinition = new RootBeanDefinition(beanType);
        beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        beanDefinitionRegistry.registerBeanDefinition(beanName, beanDefinition);
    }

}
 
Example 10
Source File: ModuleTestAutoConfiguration.java    From moduliths with Apache License 2.0 5 votes vote down vote up
private void setBasePackagesOn(BeanDefinitionRegistry registry, String beanName, String fieldName,
		List<String> packages) {

	if (!registry.containsBeanDefinition(beanName)) {
		return;
	}

	BeanDefinition definition = registry.getBeanDefinition(beanName);
	definition.getConstructorArgumentValues().addIndexedArgumentValue(0, packages);
}
 
Example 11
Source File: SpringNamespaceHandler.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
    if (ready.compareAndSet(false, true)) {
        BeanDefinitionRegistry registry = parserContext.getRegistry();
        if (!registry.containsBeanDefinition(DependsOnDefinitionPostProcessor.BEAN_NAME)) {
            registry.registerBeanDefinition(DependsOnDefinitionPostProcessor.BEAN_NAME,
                    new RootBeanDefinition(DependsOnDefinitionPostProcessor.class));
        }
    }
    return super.parse(element, parserContext);
}
 
Example 12
Source File: WebAdminComponentScanRegistrar.java    From wallride with Apache License 2.0 5 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
	Set<String> packagesToScan = getPackagesToScan(importingClassMetadata);
	if (!registry.containsBeanDefinition(BEAN_NAME)) {
		addWebAdminComponentScanBeanPostProcessor(registry, packagesToScan);
	}
	else {
		updateWebAdminComponentScanBeanPostProcessor(registry, packagesToScan);
	}
}
 
Example 13
Source File: AmazonWebserviceClientConfigurationUtils.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
public static void replaceDefaultCredentialsProvider(BeanDefinitionRegistry registry,
		String customGlobalCredentialsProvider) {
	if (registry.containsBeanDefinition(CREDENTIALS_PROVIDER_BEAN_NAME)) {
		registry.removeBeanDefinition(CREDENTIALS_PROVIDER_BEAN_NAME);
	}
	registry.registerAlias(customGlobalCredentialsProvider,
			CREDENTIALS_PROVIDER_BEAN_NAME);
}
 
Example 14
Source File: AbstractStoreBeanDefinitionRegistrar.java    From spring-content with Apache License 2.0 5 votes vote down vote up
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
	Assert.notNull(importingClassMetadata, "AnnotationMetadata must not be null!");
	Assert.notNull(registry, "BeanDefinitionRegistry must not be null!");
	Assert.isTrue(registry instanceof ConfigurableListableBeanFactory, "BeanDefinitionRegistry must be instance of ConfigurableListableBeanFactory");

	// Guard against calls for sub-classes
	// if (importingClassMetadata.getAnnotationAttributes(getAnnotation().getName())
	// == null) {
	// return;
	// }

	RootBeanDefinition repositoryInterfacePostProcessor = new RootBeanDefinition(REPOSITORY_INTERFACE_POST_PROCESSOR);
	repositoryInterfacePostProcessor.setSource(importingClassMetadata);
	if (registry.containsBeanDefinition(REPOSITORY_INTERFACE_POST_PROCESSOR) == false) {
		registry.registerBeanDefinition(REPOSITORY_INTERFACE_POST_PROCESSOR,repositoryInterfacePostProcessor);
	}

	BeanDefinition storeServiceBeanDef = createBeanDefinition(ContentStoreServiceImpl.class);
	if (registry.containsBeanDefinition("contentStoreService") == false) {
		registry.registerBeanDefinition("contentStoreService", storeServiceBeanDef);
	}

	BeanDefinition annotatedStoreEventHandlerDef = createBeanDefinition(AnnotatedStoreEventInvoker.class);
	if (registry.containsBeanDefinition("annotatedStoreEventHandler") == false) {
		registry.registerBeanDefinition("annotatedStoreEventHandler", annotatedStoreEventHandlerDef);
	}

	if (registry.containsBeanDefinition("renditionService") == false) {
		BeanDefinition renditionServiceBeanDef = createBeanDefinition(RenditionServiceImpl.class);
		registry.registerBeanDefinition("renditionService", renditionServiceBeanDef);
	}

	createOperationsBean(registry);

	registerContentStoreBeanDefinitions(importingClassMetadata, registry);
}
 
Example 15
Source File: CustomizeImportBeanDefinitionRegistrar.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    if (!registry.containsBeanDefinition(BEAN_NAME)) {
        Utils.printTrack("start registerBeanDefinitions");
        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClass(CustomizeServiceImpl4.class);
        beanDefinition.setSynthetic(true);
        registry.registerBeanDefinition(BEAN_NAME, beanDefinition);
    }
}
 
Example 16
Source File: ReactiveCosmosRepositoryConfigurationExtension.java    From spring-data-cosmosdb with MIT License 5 votes vote down vote up
@Override
public void registerBeansForRoot(BeanDefinitionRegistry registry, RepositoryConfigurationSource config) {
    super.registerBeansForRoot(registry, config);

    if (!registry.containsBeanDefinition(Constants.COSMOS_MAPPING_CONTEXT)) {
        final RootBeanDefinition definition = new RootBeanDefinition(CosmosMappingContext.class);
        definition.setRole(AbstractBeanDefinition.ROLE_INFRASTRUCTURE);
        definition.setSource(config.getSource());

        registry.registerBeanDefinition(Constants.COSMOS_MAPPING_CONTEXT, definition);
    }
}
 
Example 17
Source File: GlobalBeanDefinitionUtils.java    From spring-cloud-aws with Apache License 2.0 4 votes vote down vote up
static void registerResourceIdResolverBeanIfNeeded(BeanDefinitionRegistry registry) {
	if (!(registry.containsBeanDefinition(RESOURCE_ID_RESOLVER_BEAN_NAME))) {
		registry.registerBeanDefinition(RESOURCE_ID_RESOLVER_BEAN_NAME,
				buildResourceIdResolverBeanDefinition());
	}
}
 
Example 18
Source File: AopConfigUtils.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public static void forceAutoProxyCreatorToUseClassProxying(BeanDefinitionRegistry registry) {
	if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) {
		BeanDefinition definition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME);
		definition.getPropertyValues().add("proxyTargetClass", Boolean.TRUE);
	}
}
 
Example 19
Source File: AopConfigUtils.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
static void forceAutoProxyCreatorToExposeProxy(BeanDefinitionRegistry registry) {
	if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) {
		BeanDefinition definition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME);
		definition.getPropertyValues().add("exposeProxy", Boolean.TRUE);
	}
}
 
Example 20
Source File: KeyValueRepositoryConfigurationExtension.java    From spring-data-keyvalue with Apache License 2.0 3 votes vote down vote up
@Override
public void registerBeansForRoot(BeanDefinitionRegistry registry, RepositoryConfigurationSource configurationSource) {

	super.registerBeansForRoot(registry, configurationSource);

	registerIfNotAlreadyRegistered(() -> {

		RootBeanDefinition definitionefinition = new RootBeanDefinition(KeyValueMappingContext.class);
		definitionefinition.setSource(configurationSource.getSource());

		return definitionefinition;

	}, registry, getMappingContextBeanRef(), configurationSource);

	Optional<String> keyValueTemplateName = configurationSource.getAttribute(KEY_VALUE_TEMPLATE_BEAN_REF_ATTRIBUTE);

	// No custom template reference configured and no matching bean definition found
	if (keyValueTemplateName.isPresent() && getDefaultKeyValueTemplateRef().equals(keyValueTemplateName.get())
			&& !registry.containsBeanDefinition(keyValueTemplateName.get())) {

		AbstractBeanDefinition beanDefinition = getDefaultKeyValueTemplateBeanDefinition(configurationSource);

		if (beanDefinition != null) {
			registerIfNotAlreadyRegistered(() -> beanDefinition, registry, keyValueTemplateName.get(),
					configurationSource.getSource());
		}
	}
}