Java Code Examples for org.springframework.beans.factory.config.ConfigurableListableBeanFactory#containsSingleton()

The following examples show how to use org.springframework.beans.factory.config.ConfigurableListableBeanFactory#containsSingleton() . 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: DefaultLifecycleProcessor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Retrieve all applicable Lifecycle beans: all singletons that have already been created,
 * as well as all SmartLifecycle beans (even if they are marked as lazy-init).
 * @return the Map of applicable beans, with bean names as keys and bean instances as values
 */
protected Map<String, Lifecycle> getLifecycleBeans() {
	ConfigurableListableBeanFactory beanFactory = getBeanFactory();
	Map<String, Lifecycle> beans = new LinkedHashMap<>();
	String[] beanNames = beanFactory.getBeanNamesForType(Lifecycle.class, false, false);
	for (String beanName : beanNames) {
		String beanNameToRegister = BeanFactoryUtils.transformedBeanName(beanName);
		boolean isFactoryBean = beanFactory.isFactoryBean(beanNameToRegister);
		String beanNameToCheck = (isFactoryBean ? BeanFactory.FACTORY_BEAN_PREFIX + beanName : beanName);
		if ((beanFactory.containsSingleton(beanNameToRegister) &&
				(!isFactoryBean || matchesBeanType(Lifecycle.class, beanNameToCheck, beanFactory))) ||
				matchesBeanType(SmartLifecycle.class, beanNameToCheck, beanFactory)) {
			Object bean = beanFactory.getBean(beanNameToCheck);
			if (bean != this && bean instanceof Lifecycle) {
				beans.put(beanNameToRegister, (Lifecycle) bean);
			}
		}
	}
	return beans;
}
 
Example 2
Source File: DefaultLifecycleProcessor.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Retrieve all applicable Lifecycle beans: all singletons that have already been created,
 * as well as all SmartLifecycle beans (even if they are marked as lazy-init).
 * @return the Map of applicable beans, with bean names as keys and bean instances as values
 */
protected Map<String, Lifecycle> getLifecycleBeans() {
	ConfigurableListableBeanFactory beanFactory = getBeanFactory();
	Map<String, Lifecycle> beans = new LinkedHashMap<>();
	String[] beanNames = beanFactory.getBeanNamesForType(Lifecycle.class, false, false);
	for (String beanName : beanNames) {
		String beanNameToRegister = BeanFactoryUtils.transformedBeanName(beanName);
		boolean isFactoryBean = beanFactory.isFactoryBean(beanNameToRegister);
		String beanNameToCheck = (isFactoryBean ? BeanFactory.FACTORY_BEAN_PREFIX + beanName : beanName);
		if ((beanFactory.containsSingleton(beanNameToRegister) &&
				(!isFactoryBean || matchesBeanType(Lifecycle.class, beanNameToCheck, beanFactory))) ||
				matchesBeanType(SmartLifecycle.class, beanNameToCheck, beanFactory)) {
			Object bean = beanFactory.getBean(beanNameToCheck);
			if (bean != this && bean instanceof Lifecycle) {
				beans.put(beanNameToRegister, (Lifecycle) bean);
			}
		}
	}
	return beans;
}
 
Example 3
Source File: SimpleConditionService.java    From spring-init with Apache License 2.0 5 votes vote down vote up
public SimpleConditionService(BeanDefinitionRegistry registry, ConfigurableListableBeanFactory beanFactory,
		Environment environment, ResourceLoader resourceLoader) {
	this.beanFactory = beanFactory;
	this.evaluator = new ConditionEvaluator(registry, environment, resourceLoader);
	this.classLoader = resourceLoader.getClassLoader();
	String metadataFactory = MetadataReaderFactory.class.getName();
	this.metadataReaderFactory = beanFactory.containsSingleton(metadataFactory)
			? (MetadataReaderFactory) beanFactory.getSingleton(metadataFactory)
			: new CachingMetadataReaderFactory(this.classLoader);
}
 
Example 4
Source File: NacosConfigApplicationContextInitializer.java    From nacos-spring-boot-project with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(ConfigurableApplicationContext context) {
	singleton.setApplicationContext(context);
	environment = context.getEnvironment();
	nacosConfigProperties = NacosConfigPropertiesUtils
			.buildNacosConfigProperties(environment);
	final NacosConfigLoader configLoader = new NacosConfigLoader(
			nacosConfigProperties, environment, builder);
	if (!enable()) {
		logger.info("[Nacos Config Boot] : The preload configuration is not enabled");
	}
	else {

		// If it opens the log level loading directly will cache
		// DeferNacosPropertySource release

		if (processor.enable()) {
			processor.publishDeferService(context);
			configLoader
					.addListenerIfAutoRefreshed(processor.getDeferPropertySources());
		}
		else {
			configLoader.loadConfig();
			configLoader.addListenerIfAutoRefreshed();
		}
	}

	final ConfigurableListableBeanFactory factory = context.getBeanFactory();
	if (!factory
			.containsSingleton(NacosBeanUtils.GLOBAL_NACOS_PROPERTIES_BEAN_NAME)) {
		factory.registerSingleton(NacosBeanUtils.GLOBAL_NACOS_PROPERTIES_BEAN_NAME,
				configLoader.buildGlobalNacosProperties());
	}
}
 
Example 5
Source File: LocBaseAutoConfiguration.java    From loc-framework with MIT License 5 votes vote down vote up
protected void register(ConfigurableListableBeanFactory beanFactory, Object bean, String name,
    String alias) {
  beanFactory.registerSingleton(name, bean);
  if (!beanFactory.containsSingleton(alias)) {
    beanFactory.registerAlias(name, alias);
  }
}
 
Example 6
Source File: BusWiringBeanFactoryPostProcessor.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) {
    Object inject = bus;
    if (inject == null) {
        inject = getBusForName(Bus.DEFAULT_BUS_ID, factory, true, null);
    } else {
        if (!factory.containsBeanDefinition(Bus.DEFAULT_BUS_ID)
            && !factory.containsSingleton(Bus.DEFAULT_BUS_ID)) {
            factory.registerSingleton(Bus.DEFAULT_BUS_ID, bus);
        }
    }
    for (String beanName : factory.getBeanDefinitionNames()) {
        BeanDefinition beanDefinition = factory.getBeanDefinition(beanName);
        BusWiringType type
            = (BusWiringType)beanDefinition.getAttribute(AbstractBeanDefinitionParser.WIRE_BUS_ATTRIBUTE);
        if (type == null) {
            continue;
        }
        String busname = (String)beanDefinition.getAttribute(AbstractBeanDefinitionParser.WIRE_BUS_NAME);
        String create = (String)beanDefinition
            .getAttribute(AbstractBeanDefinitionParser.WIRE_BUS_CREATE);
        Object inj = inject;
        if (busname != null) {
            if (bus != null) {
                continue;
            }
            inj = getBusForName(busname, factory, create != null, create);
        }
        beanDefinition.removeAttribute(AbstractBeanDefinitionParser.WIRE_BUS_NAME);
        beanDefinition.removeAttribute(AbstractBeanDefinitionParser.WIRE_BUS_ATTRIBUTE);
        beanDefinition.removeAttribute(AbstractBeanDefinitionParser.WIRE_BUS_CREATE);
        if (create == null) {
            if (BusWiringType.PROPERTY == type) {
                beanDefinition.getPropertyValues()
                    .addPropertyValue("bus", inj);
            } else if (BusWiringType.CONSTRUCTOR == type) {
                ConstructorArgumentValues constructorArgs = beanDefinition.getConstructorArgumentValues();
                insertConstructorArg(constructorArgs, inj);
            }
        }
    }
}