Example usage for org.springframework.beans.factory.support RootBeanDefinition getDependsOn

List of usage examples for org.springframework.beans.factory.support RootBeanDefinition getDependsOn

Introduction

In this page you can find the example usage for org.springframework.beans.factory.support RootBeanDefinition getDependsOn.

Prototype

@Override
@Nullable
public String[] getDependsOn() 

Source Link

Document

Return the bean names that this bean depends on.

Usage

From source file:be.hikage.springtemplate.TemplateTest.java

@Test
public void testGood() {

    ApplicationContext context = new ClassPathXmlApplicationContext("test-config-good.xml", this.getClass());
    assertTrue("Bean simple-dev must be defined", context.containsBean("simple-dev"));
    assertEquals(SimpleBean.class, context.getBean("simple-dev").getClass());
    SimpleBean simpleBean = (SimpleBean) context.getBean("simple-dev");

    assertEquals("constructorData.dev", simpleBean.getConstructorValue());
    assertEquals("ExternalizedConstructor", simpleBean.getExternalizedConstructorValue());

    assertEquals("propertyData.dev", simpleBean.getPropertyValue());
    assertEquals("ExternalizedProperty", simpleBean.getExternalizedPropertyValue());

    assertTrue("Bean container-dev must be defined", context.containsBean("container-dev"));
    assertEquals(ContainerBean.class, context.getBean("container-dev").getClass());

    ConfigurableApplicationContext listableBeanFactory = (ConfigurableApplicationContext) context;
    RootBeanDefinition beanDefinition = (RootBeanDefinition) listableBeanFactory.getBeanFactory()
            .getBeanDefinition("container-dev");
    assertArrayEquals(new String[] { "simple-dev" }, beanDefinition.getDependsOn());

}

From source file:org.ehoffman.aopalliance.extensions.scope.SpringTestScope.java

@Override
public Object get(final String name, final ObjectFactory<?> objectFactory) {
    final Map<String, Object> scope = values.get();
    try {/*w  ww.  jav a  2 s . c  o  m*/
        final Field field = objectFactory.getClass().getDeclaredField("val$mbd");
        field.setAccessible(true);
        final RootBeanDefinition def = (RootBeanDefinition) field.get(objectFactory);
        if (def != null && def.getDependsOn() != null) {
            dependencies.get().put(name, Arrays.asList(def.getDependsOn()));
            LOGGER.warn("Adding dependencies for " + name + " which are" + Arrays.asList(def.getDependsOn()));
        }
    } catch (final Throwable t) {
        LOGGER.error("Spring has changed, time to update this method");
    }
    Object object = scope.get(name);
    if (object == null) {
        object = objectFactory.getObject();
        scope.put(name, object);
    }
    return object;
}

From source file:org.springframework.beans.factory.support.AbstractBeanFactory.java

/**
 * Add the given bean to the list of disposable beans in this factory,
 * registering its DisposableBean interface and/or the given destroy method
 * to be called on factory shutdown (if applicable). Only applies to singletons.
 * <p>Also registers bean as dependent on other beans, according to the
 * "depends-on" configuration in the bean definition.
 * @param beanName the name of the bean//from   w  w  w.j  av  a2 s . c om
 * @param bean the bean instance
 * @param mergedBeanDefinition the bean definition for the bean
 * @see RootBeanDefinition#isSingleton
 * @see RootBeanDefinition#getDependsOn
 * @see #registerDisposableBean
 * @see #registerDependentBean
 */
protected void registerDisposableBeanIfNecessary(final String beanName, final Object bean,
        final RootBeanDefinition mergedBeanDefinition) {

    if (mergedBeanDefinition.isSingleton()) {
        final boolean isDisposableBean = (bean instanceof DisposableBean);
        final boolean hasDestroyMethod = (mergedBeanDefinition.getDestroyMethodName() != null);

        if (isDisposableBean || hasDestroyMethod || hasDestructionAwareBeanPostProcessors()) {
            // Determine unique key for registration of disposable bean
            int counter = 1;
            String id = beanName;
            synchronized (this.disposableBeans) {
                while (this.disposableBeans.containsKey(id)) {
                    counter++;
                    id = beanName + "#" + counter;
                }
            }

            // Register a DisposableBean implementation that performs all destruction
            // work for the given bean: DestructionAwareBeanPostProcessors,
            // DisposableBean interface, custom destroy method.

            registerDisposableBean(id, new DisposableBean() {
                public void destroy() throws Exception {

                    if (hasDestructionAwareBeanPostProcessors()) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("Applying DestructionAwareBeanPostProcessors to bean with name '"
                                    + beanName + "'");
                        }
                        for (int i = getBeanPostProcessors().size() - 1; i >= 0; i--) {
                            Object beanProcessor = getBeanPostProcessors().get(i);
                            if (beanProcessor instanceof DestructionAwareBeanPostProcessor) {
                                ((DestructionAwareBeanPostProcessor) beanProcessor)
                                        .postProcessBeforeDestruction(bean, beanName);
                            }
                        }
                    }

                    if (isDisposableBean) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("Invoking destroy() on bean with name '" + beanName + "'");
                        }
                        ((DisposableBean) bean).destroy();
                    }

                    if (hasDestroyMethod) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("Invoking custom destroy method on bean with name '" + beanName + "'");
                        }
                        invokeCustomDestroyMethod(beanName, bean, mergedBeanDefinition.getDestroyMethodName(),
                                mergedBeanDefinition.isEnforceDestroyMethod());
                    }
                }
            });
        }

        // Register bean as dependent on other beans, if necessary,
        // for correct shutdown order.
        String[] dependsOn = mergedBeanDefinition.getDependsOn();
        if (dependsOn != null) {
            for (int i = 0; i < dependsOn.length; i++) {
                registerDependentBean(dependsOn[i], beanName);
            }
        }
    }
}