Example usage for org.springframework.beans.factory.support DefaultListableBeanFactory getBean

List of usage examples for org.springframework.beans.factory.support DefaultListableBeanFactory getBean

Introduction

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

Prototype

@Override
    public <T> T getBean(Class<T> requiredType) throws BeansException 

Source Link

Usage

From source file:org.springframework.beans.factory.xml.XmlBeanFactoryTests.java

@Test
public void testStringConstructorArrayNoType() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
    ConstructorArrayTestBean bean = (ConstructorArrayTestBean) xbf.getBean("constructorArrayNoType");
    assertTrue(bean.array instanceof String[]);
    assertEquals(0, ((String[]) bean.array).length);
}

From source file:org.springframework.beans.factory.xml.XmlBeanFactoryTests.java

@Test
public void testStringConstructorArrayNoTypeNonLenient() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
    AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("constructorArrayNoType");
    bd.setLenientConstructorResolution(false);
    ConstructorArrayTestBean bean = (ConstructorArrayTestBean) xbf.getBean("constructorArrayNoType");
    assertTrue(bean.array instanceof String[]);
    assertEquals(0, ((String[]) bean.array).length);
}

From source file:org.springframework.beans.factory.xml.XmlBeanFactoryTests.java

@Test
public void testConstructorWithUnresolvableParameterName() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
    AtomicInteger bean = (AtomicInteger) xbf.getBean("constructorUnresolvableName");
    assertEquals(1, bean.get());/*from  w ww.ja v a  2 s .c  o  m*/
    bean = (AtomicInteger) xbf.getBean("constructorUnresolvableNameWithIndex");
    assertEquals(1, bean.get());
}

From source file:org.springframework.beans.factory.xml.XmlBeanFactoryTests.java

@Test
public void testOverrideMethodByArgTypeAttribute() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
    reader.loadBeanDefinitions(DELEGATION_OVERRIDES_CONTEXT);
    OverrideOneMethod oom = (OverrideOneMethod) xbf.getBean("overrideOneMethodByAttribute");
    assertEquals("should not replace", "replaceMe:1", oom.replaceMe(1));
    assertEquals("should replace", "cba", oom.replaceMe("abc"));
}

From source file:org.springframework.beans.factory.xml.XmlBeanFactoryTests.java

@Test
public void testOverrideMethodByArgTypeElement() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
    reader.loadBeanDefinitions(DELEGATION_OVERRIDES_CONTEXT);
    OverrideOneMethod oom = (OverrideOneMethod) xbf.getBean("overrideOneMethodByElement");
    assertEquals("should not replace", "replaceMe:1", oom.replaceMe(1));
    assertEquals("should replace", "cba", oom.replaceMe("abc"));
}

From source file:org.springframework.context.annotation.Spr3775InitDestroyLifecycleTests.java

@Test
public void testInitDestroyMethods() {
    final Class<?> beanClass = InitDestroyBean.class;
    final DefaultListableBeanFactory beanFactory = createBeanFactoryAndRegisterBean(beanClass,
            "afterPropertiesSet", "destroy");
    final InitDestroyBean bean = (InitDestroyBean) beanFactory.getBean(LIFECYCLE_TEST_BEAN);
    assertMethodOrdering(beanClass, "init-methods", Arrays.asList("afterPropertiesSet"), bean.initMethods);
    beanFactory.destroySingletons();/*from w w w .j a va 2  s . com*/
    assertMethodOrdering(beanClass, "destroy-methods", Arrays.asList("destroy"), bean.destroyMethods);
}

From source file:org.springframework.context.annotation.Spr3775InitDestroyLifecycleTests.java

@Test
public void testInitializingDisposableInterfaces() {
    final Class<?> beanClass = CustomInitializingDisposableBean.class;
    final DefaultListableBeanFactory beanFactory = createBeanFactoryAndRegisterBean(beanClass, "customInit",
            "customDestroy");
    final CustomInitializingDisposableBean bean = (CustomInitializingDisposableBean) beanFactory
            .getBean(LIFECYCLE_TEST_BEAN);
    assertMethodOrdering(beanClass, "init-methods", Arrays.asList("afterPropertiesSet", "customInit"),
            bean.initMethods);/*from ww w  . j a  v  a 2s  .  c  o  m*/
    beanFactory.destroySingletons();
    assertMethodOrdering(beanClass, "destroy-methods", Arrays.asList("destroy", "customDestroy"),
            bean.destroyMethods);
}

From source file:org.springframework.context.annotation.Spr3775InitDestroyLifecycleTests.java

@Test
public void testInitializingDisposableInterfacesWithShadowedMethods() {
    final Class<?> beanClass = InitializingDisposableWithShadowedMethodsBean.class;
    final DefaultListableBeanFactory beanFactory = createBeanFactoryAndRegisterBean(beanClass,
            "afterPropertiesSet", "destroy");
    final InitializingDisposableWithShadowedMethodsBean bean = (InitializingDisposableWithShadowedMethodsBean) beanFactory
            .getBean(LIFECYCLE_TEST_BEAN);
    assertMethodOrdering(beanClass, "init-methods", Arrays.asList("InitializingBean.afterPropertiesSet"),
            bean.initMethods);//from   w  ww .j a  v a2s  .  c o m
    beanFactory.destroySingletons();
    assertMethodOrdering(beanClass, "destroy-methods", Arrays.asList("DisposableBean.destroy"),
            bean.destroyMethods);
}

From source file:org.springframework.context.annotation.Spr3775InitDestroyLifecycleTests.java

@Test
public void testJsr250Annotations() {
    final Class<?> beanClass = CustomAnnotatedInitDestroyBean.class;
    final DefaultListableBeanFactory beanFactory = createBeanFactoryAndRegisterBean(beanClass, "customInit",
            "customDestroy");
    final CustomAnnotatedInitDestroyBean bean = (CustomAnnotatedInitDestroyBean) beanFactory
            .getBean(LIFECYCLE_TEST_BEAN);
    assertMethodOrdering(beanClass, "init-methods",
            Arrays.asList("postConstruct", "afterPropertiesSet", "customInit"), bean.initMethods);
    beanFactory.destroySingletons();//from   w  ww . j  a  v  a 2s . co m
    assertMethodOrdering(beanClass, "destroy-methods", Arrays.asList("preDestroy", "destroy", "customDestroy"),
            bean.destroyMethods);
}

From source file:org.springframework.context.annotation.Spr3775InitDestroyLifecycleTests.java

@Test
public void testJsr250AnnotationsWithShadowedMethods() {
    final Class<?> beanClass = CustomAnnotatedInitDestroyWithShadowedMethodsBean.class;
    final DefaultListableBeanFactory beanFactory = createBeanFactoryAndRegisterBean(beanClass, "customInit",
            "customDestroy");
    final CustomAnnotatedInitDestroyWithShadowedMethodsBean bean = (CustomAnnotatedInitDestroyWithShadowedMethodsBean) beanFactory
            .getBean(LIFECYCLE_TEST_BEAN);
    assertMethodOrdering(beanClass, "init-methods",
            Arrays.asList("@PostConstruct.afterPropertiesSet", "customInit"), bean.initMethods);
    beanFactory.destroySingletons();//from w  w  w .  j av  a 2  s .c o m
    assertMethodOrdering(beanClass, "destroy-methods", Arrays.asList("@PreDestroy.destroy", "customDestroy"),
            bean.destroyMethods);
}