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() . These examples are extracted from open source projects. 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
@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
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
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 4
@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 5
/**
 * 注册
 *
 * @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 6
@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 7
@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 8
Source Project: dubbo-2.6.5   File: BeanRegistrar.java    License: 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 9
@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 10
@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 11
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 12
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 13
@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 14
Source Project: joyrpc   File: SpringNamespaceHandler.java    License: 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 15
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 16
@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 17
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 18
static void registerResourceIdResolverBeanIfNeeded(BeanDefinitionRegistry registry) {
	if (!(registry.containsBeanDefinition(RESOURCE_ID_RESOLVER_BEAN_NAME))) {
		registry.registerBeanDefinition(RESOURCE_ID_RESOLVER_BEAN_NAME,
				buildResourceIdResolverBeanDefinition());
	}
}
 
Example 19
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
@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());
		}
	}
}