예제 #1
0
  /** Tests where the static factory method is on a different class. */
  @Test
  public void testFactoryMethodsOnExternalClass() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
    reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));

    assertEquals(TestBean.class, xbf.getType("externalFactoryMethodWithoutArgs"));
    assertEquals(TestBean.class, xbf.getType("externalFactoryMethodWithArgs"));
    String[] names = xbf.getBeanNamesForType(TestBean.class);
    assertTrue(Arrays.asList(names).contains("externalFactoryMethodWithoutArgs"));
    assertTrue(Arrays.asList(names).contains("externalFactoryMethodWithArgs"));

    TestBean tb = (TestBean) xbf.getBean("externalFactoryMethodWithoutArgs");
    assertEquals(2, tb.getAge());
    assertEquals("Tristan", tb.getName());
    tb = (TestBean) xbf.getBean("externalFactoryMethodWithArgs");
    assertEquals(33, tb.getAge());
    assertEquals("Rod", tb.getName());

    assertEquals(TestBean.class, xbf.getType("externalFactoryMethodWithoutArgs"));
    assertEquals(TestBean.class, xbf.getType("externalFactoryMethodWithArgs"));
    names = xbf.getBeanNamesForType(TestBean.class);
    assertTrue(Arrays.asList(names).contains("externalFactoryMethodWithoutArgs"));
    assertTrue(Arrays.asList(names).contains("externalFactoryMethodWithArgs"));
  }
예제 #2
0
  @Test
  public void testFactoryMethodsSingletonOnTargetClass() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
    reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));

    TestBean tb = (TestBean) xbf.getBean("defaultTestBean");
    assertEquals("defaultInstance", tb.getName());
    assertEquals(1, tb.getAge());

    FactoryMethods fm = (FactoryMethods) xbf.getBean("default");
    assertEquals(0, fm.getNum());
    assertEquals("default", fm.getName());
    assertEquals("defaultInstance", fm.getTestBean().getName());
    assertEquals("setterString", fm.getStringValue());

    fm = (FactoryMethods) xbf.getBean("testBeanOnly");
    assertEquals(0, fm.getNum());
    assertEquals("default", fm.getName());
    // This comes from the test bean
    assertEquals("Juergen", fm.getTestBean().getName());

    fm = (FactoryMethods) xbf.getBean("full");
    assertEquals(27, fm.getNum());
    assertEquals("gotcha", fm.getName());
    assertEquals("Juergen", fm.getTestBean().getName());

    FactoryMethods fm2 = (FactoryMethods) xbf.getBean("full");
    assertSame(fm, fm2);

    xbf.destroySingletons();
    assertTrue(tb.wasDestroyed());
  }