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

The following examples show how to use org.springframework.beans.factory.support.BeanDefinitionRegistry#removeBeanDefinition() . 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: 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 2
Source File: DataSourceBeanFactoryPostProcessor.java    From sofa-tracer with Apache License 2.0 5 votes vote down vote up
private void createDataSourceProxy(ConfigurableListableBeanFactory beanFactory,
                                   String beanName, BeanDefinition originDataSource,
                                   String jdbcUrl) {
    // re-register origin datasource bean
    BeanDefinitionRegistry beanDefinitionRegistry = (BeanDefinitionRegistry) beanFactory;
    beanDefinitionRegistry.removeBeanDefinition(beanName);
    boolean isPrimary = originDataSource.isPrimary();
    originDataSource.setPrimary(false);
    beanDefinitionRegistry.registerBeanDefinition(transformDatasourceBeanName(beanName),
        originDataSource);
    // register proxied datasource
    RootBeanDefinition proxiedBeanDefinition = new RootBeanDefinition(SmartDataSource.class);
    proxiedBeanDefinition.setRole(BeanDefinition.ROLE_APPLICATION);
    proxiedBeanDefinition.setPrimary(isPrimary);
    proxiedBeanDefinition.setInitMethodName("init");
    proxiedBeanDefinition.setDependsOn(transformDatasourceBeanName(beanName));
    MutablePropertyValues originValues = originDataSource.getPropertyValues();
    MutablePropertyValues values = new MutablePropertyValues();
    String appName = environment.getProperty(TRACER_APPNAME_KEY);
    Assert.isTrue(!StringUtils.isBlank(appName), TRACER_APPNAME_KEY + " must be configured!");
    values.add("appName", appName);
    values.add("delegate", new RuntimeBeanReference(transformDatasourceBeanName(beanName)));
    values.add("dbType",
        DataSourceUtils.resolveDbTypeFromUrl(unwrapPropertyValue(originValues.get(jdbcUrl))));
    values.add("database",
        DataSourceUtils.resolveDatabaseFromUrl(unwrapPropertyValue(originValues.get(jdbcUrl))));
    proxiedBeanDefinition.setPropertyValues(values);
    beanDefinitionRegistry.registerBeanDefinition(beanName, proxiedBeanDefinition);
}
 
Example 3
Source File: BeanRegistrar.java    From spring-batch-lightmin with Apache License 2.0 5 votes vote down vote up
/**
 * unregisters a bean of the current application context
 *
 * @param beanName name of the bean, which should be destroyed on current application context
 * @throws NoSuchBeanDefinitionException if the context does not contain a bean with the given name
 */
public void unregisterBean(final String beanName) {
    final BeanDefinitionRegistry factory = (BeanDefinitionRegistry) context.getAutowireCapableBeanFactory();
    if (factory.containsBeanDefinition(beanName)) {
        ((DefaultListableBeanFactory) factory).destroySingleton(beanName);
        factory.removeBeanDefinition(beanName);
    } else {
        throw new NoSuchBeanDefinitionException(
                "No Bean definition exists for bean name: " + beanName);
    }
}
 
Example 4
Source File: MailboxConfigurationBeanFactoryPostProcessor.java    From james-project with Apache License 2.0 5 votes vote down vote up
private void removeMailboxManagersExceptRightSelectedOne(BeanDefinitionRegistry registry, String selectedMailboxManager) {
    for (String mailboxManagerId : MAILBOX_MANAGER_IDS) {
        if (registeredAndNotSelected(registry, selectedMailboxManager, mailboxManagerId)) {
            registry.removeBeanDefinition(mailboxManagerId);
        }
    }
}
 
Example 5
Source File: AmazonWebserviceClientConfigurationUtils.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
public static void replaceDefaultRegionProvider(BeanDefinitionRegistry registry,
		String customGlobalRegionProvider) {
	if (registry.containsBeanDefinition(REGION_PROVIDER_BEAN_NAME)) {
		registry.removeBeanDefinition(REGION_PROVIDER_BEAN_NAME);
	}
	registry.registerAlias(customGlobalRegionProvider, REGION_PROVIDER_BEAN_NAME);
}
 
Example 6
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 7
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");
    }
}