Search in sources :

Example 16 with SmartLifecycle

use of org.springframework.context.SmartLifecycle in project spring-framework by spring-projects.

the class DefaultLifecycleProcessor method getLifecycleBeans.

// overridable hooks
/**
 * 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;
}
Also used : Lifecycle(org.springframework.context.Lifecycle) SmartLifecycle(org.springframework.context.SmartLifecycle) SmartLifecycle(org.springframework.context.SmartLifecycle) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

SmartLifecycle (org.springframework.context.SmartLifecycle)16 Test (org.junit.Test)11 SmartLifecycleRoleController (org.springframework.integration.support.SmartLifecycleRoleController)8 List (java.util.List)7 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)7 Lifecycle (org.springframework.context.Lifecycle)3 LinkedHashMap (java.util.LinkedHashMap)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)2 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Log (org.apache.commons.logging.Log)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 InOrder (org.mockito.InOrder)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)1 EnableMessageHistory (org.springframework.integration.config.EnableMessageHistory)1