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

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

Introduction

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

Prototype

@Override
    public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException 

Source Link

Usage

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@Test
public void testPrototypeFactoryBeanIgnoredByNonEagerTypeMatching() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    p.setProperty("x1.(class)", DummyFactory.class.getName());
    // Reset static state
    DummyFactory.reset();/*from   w  w  w  .  java  2  s.  c o  m*/
    p.setProperty("x1.(singleton)", "false");
    p.setProperty("x1.singleton", "false");
    (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);

    assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
    String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
    assertEquals(0, beanNames.length);
    beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
    assertEquals(0, beanNames.length);

    assertFalse(lbf.containsSingleton("x1"));
    assertTrue(lbf.containsBean("x1"));
    assertTrue(lbf.containsBean("&x1"));
    assertFalse(lbf.isSingleton("x1"));
    assertFalse(lbf.isSingleton("&x1"));
    assertTrue(lbf.isPrototype("x1"));
    assertTrue(lbf.isPrototype("&x1"));
    assertTrue(lbf.isTypeMatch("x1", TestBean.class));
    assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
    assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
    assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class)));
    assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
    assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
    assertEquals(TestBean.class, lbf.getType("x1"));
    assertEquals(DummyFactory.class, lbf.getType("&x1"));
    assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@Test
public void testSingletonFactoryBeanIgnoredByNonEagerTypeMatching() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    p.setProperty("x1.(class)", DummyFactory.class.getName());
    // Reset static state
    DummyFactory.reset();//  w  w w  .j a v  a2  s .  c  o  m
    p.setProperty("x1.(singleton)", "false");
    p.setProperty("x1.singleton", "true");
    (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);

    assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
    String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
    assertEquals(0, beanNames.length);
    beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
    assertEquals(0, beanNames.length);

    assertFalse(lbf.containsSingleton("x1"));
    assertTrue(lbf.containsBean("x1"));
    assertTrue(lbf.containsBean("&x1"));
    assertFalse(lbf.isSingleton("x1"));
    assertFalse(lbf.isSingleton("&x1"));
    assertTrue(lbf.isPrototype("x1"));
    assertTrue(lbf.isPrototype("&x1"));
    assertTrue(lbf.isTypeMatch("x1", TestBean.class));
    assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
    assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
    assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class)));
    assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
    assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
    assertEquals(TestBean.class, lbf.getType("x1"));
    assertEquals(DummyFactory.class, lbf.getType("&x1"));
    assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@Test
public void testNonInitializedFactoryBeanIgnoredByNonEagerTypeMatching() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    p.setProperty("x1.(class)", DummyFactory.class.getName());
    // Reset static state
    DummyFactory.reset();/*ww  w .  j  a v a 2s  . co m*/
    p.setProperty("x1.singleton", "false");
    (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);

    assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
    String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
    assertEquals(0, beanNames.length);
    beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
    assertEquals(0, beanNames.length);

    assertFalse(lbf.containsSingleton("x1"));
    assertTrue(lbf.containsBean("x1"));
    assertTrue(lbf.containsBean("&x1"));
    assertFalse(lbf.isSingleton("x1"));
    assertTrue(lbf.isSingleton("&x1"));
    assertTrue(lbf.isPrototype("x1"));
    assertFalse(lbf.isPrototype("&x1"));
    assertTrue(lbf.isTypeMatch("x1", TestBean.class));
    assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
    assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
    assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class)));
    assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
    assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
    assertEquals(TestBean.class, lbf.getType("x1"));
    assertEquals(DummyFactory.class, lbf.getType("&x1"));
    assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@Test
public void testInitializedFactoryBeanFoundByNonEagerTypeMatching() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    p.setProperty("x1.(class)", DummyFactory.class.getName());
    // Reset static state
    DummyFactory.reset();//w w  w .  j  a  v  a 2s  .c  om
    p.setProperty("x1.singleton", "false");
    (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
    lbf.preInstantiateSingletons();

    assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
    String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
    assertEquals(1, beanNames.length);
    assertEquals("x1", beanNames[0]);
    assertTrue(lbf.containsSingleton("x1"));
    assertTrue(lbf.containsBean("x1"));
    assertTrue(lbf.containsBean("&x1"));
    assertTrue(lbf.containsLocalBean("x1"));
    assertTrue(lbf.containsLocalBean("&x1"));
    assertFalse(lbf.isSingleton("x1"));
    assertTrue(lbf.isSingleton("&x1"));
    assertTrue(lbf.isPrototype("x1"));
    assertFalse(lbf.isPrototype("&x1"));
    assertTrue(lbf.isTypeMatch("x1", TestBean.class));
    assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
    assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
    assertTrue(lbf.isTypeMatch("x1", Object.class));
    assertTrue(lbf.isTypeMatch("&x1", Object.class));
    assertEquals(TestBean.class, lbf.getType("x1"));
    assertEquals(DummyFactory.class, lbf.getType("&x1"));
    assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());

    lbf.registerAlias("x1", "x2");
    assertTrue(lbf.containsBean("x2"));
    assertTrue(lbf.containsBean("&x2"));
    assertTrue(lbf.containsLocalBean("x2"));
    assertTrue(lbf.containsLocalBean("&x2"));
    assertFalse(lbf.isSingleton("x2"));
    assertTrue(lbf.isSingleton("&x2"));
    assertTrue(lbf.isPrototype("x2"));
    assertFalse(lbf.isPrototype("&x2"));
    assertTrue(lbf.isTypeMatch("x2", TestBean.class));
    assertFalse(lbf.isTypeMatch("&x2", TestBean.class));
    assertTrue(lbf.isTypeMatch("&x2", DummyFactory.class));
    assertTrue(lbf.isTypeMatch("x2", Object.class));
    assertTrue(lbf.isTypeMatch("&x2", Object.class));
    assertEquals(TestBean.class, lbf.getType("x2"));
    assertEquals(DummyFactory.class, lbf.getType("&x2"));
    assertEquals(1, lbf.getAliases("x1").length);
    assertEquals("x2", lbf.getAliases("x1")[0]);
    assertEquals(1, lbf.getAliases("&x1").length);
    assertEquals("&x2", lbf.getAliases("&x1")[0]);
    assertEquals(1, lbf.getAliases("x2").length);
    assertEquals("x1", lbf.getAliases("x2")[0]);
    assertEquals(1, lbf.getAliases("&x2").length);
    assertEquals("&x1", lbf.getAliases("&x2")[0]);
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@Test
public void testStaticFactoryMethodFoundByNonEagerTypeMatching() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition rbd = new RootBeanDefinition(TestBeanFactory.class);
    rbd.setFactoryMethodName("createTestBean");
    lbf.registerBeanDefinition("x1", rbd);

    TestBeanFactory.initialized = false;
    String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
    assertEquals(1, beanNames.length);//from w  w  w.j  av a2  s  .  c  o m
    assertEquals("x1", beanNames[0]);
    assertFalse(lbf.containsSingleton("x1"));
    assertTrue(lbf.containsBean("x1"));
    assertFalse(lbf.containsBean("&x1"));
    assertTrue(lbf.isSingleton("x1"));
    assertFalse(lbf.isSingleton("&x1"));
    assertFalse(lbf.isPrototype("x1"));
    assertFalse(lbf.isPrototype("&x1"));
    assertTrue(lbf.isTypeMatch("x1", TestBean.class));
    assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
    assertEquals(TestBean.class, lbf.getType("x1"));
    assertEquals(null, lbf.getType("&x1"));
    assertFalse(TestBeanFactory.initialized);
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@Test
public void testStaticPrototypeFactoryMethodFoundByNonEagerTypeMatching() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition rbd = new RootBeanDefinition(TestBeanFactory.class);
    rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    rbd.setFactoryMethodName("createTestBean");
    lbf.registerBeanDefinition("x1", rbd);

    TestBeanFactory.initialized = false;
    String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
    assertEquals(1, beanNames.length);/*from  ww  w  .ja v  a2  s.com*/
    assertEquals("x1", beanNames[0]);
    assertFalse(lbf.containsSingleton("x1"));
    assertTrue(lbf.containsBean("x1"));
    assertFalse(lbf.containsBean("&x1"));
    assertFalse(lbf.isSingleton("x1"));
    assertFalse(lbf.isSingleton("&x1"));
    assertTrue(lbf.isPrototype("x1"));
    assertFalse(lbf.isPrototype("&x1"));
    assertTrue(lbf.isTypeMatch("x1", TestBean.class));
    assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
    assertEquals(TestBean.class, lbf.getType("x1"));
    assertEquals(null, lbf.getType("&x1"));
    assertFalse(TestBeanFactory.initialized);
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@Test
public void testNonStaticFactoryMethodFoundByNonEagerTypeMatching() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition factoryBd = new RootBeanDefinition(TestBeanFactory.class);
    lbf.registerBeanDefinition("factory", factoryBd);
    RootBeanDefinition rbd = new RootBeanDefinition(TestBeanFactory.class);
    rbd.setFactoryBeanName("factory");
    rbd.setFactoryMethodName("createTestBeanNonStatic");
    lbf.registerBeanDefinition("x1", rbd);

    TestBeanFactory.initialized = false;
    String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
    assertEquals(1, beanNames.length);//from   w  ww .ja  v  a 2s. com
    assertEquals("x1", beanNames[0]);
    assertFalse(lbf.containsSingleton("x1"));
    assertTrue(lbf.containsBean("x1"));
    assertFalse(lbf.containsBean("&x1"));
    assertTrue(lbf.isSingleton("x1"));
    assertFalse(lbf.isSingleton("&x1"));
    assertFalse(lbf.isPrototype("x1"));
    assertFalse(lbf.isPrototype("&x1"));
    assertTrue(lbf.isTypeMatch("x1", TestBean.class));
    assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
    assertEquals(TestBean.class, lbf.getType("x1"));
    assertEquals(null, lbf.getType("&x1"));
    assertFalse(TestBeanFactory.initialized);
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@Test
public void testNonStaticPrototypeFactoryMethodFoundByNonEagerTypeMatching() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition factoryBd = new RootBeanDefinition(TestBeanFactory.class);
    lbf.registerBeanDefinition("factory", factoryBd);
    RootBeanDefinition rbd = new RootBeanDefinition();
    rbd.setFactoryBeanName("factory");
    rbd.setFactoryMethodName("createTestBeanNonStatic");
    rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    lbf.registerBeanDefinition("x1", rbd);

    TestBeanFactory.initialized = false;
    String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
    assertEquals(1, beanNames.length);// w  w w.  j a  va  2  s.com
    assertEquals("x1", beanNames[0]);
    assertFalse(lbf.containsSingleton("x1"));
    assertTrue(lbf.containsBean("x1"));
    assertFalse(lbf.containsBean("&x1"));
    assertTrue(lbf.containsLocalBean("x1"));
    assertFalse(lbf.containsLocalBean("&x1"));
    assertFalse(lbf.isSingleton("x1"));
    assertFalse(lbf.isSingleton("&x1"));
    assertTrue(lbf.isPrototype("x1"));
    assertFalse(lbf.isPrototype("&x1"));
    assertTrue(lbf.isTypeMatch("x1", TestBean.class));
    assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
    assertTrue(lbf.isTypeMatch("x1", Object.class));
    assertFalse(lbf.isTypeMatch("&x1", Object.class));
    assertEquals(TestBean.class, lbf.getType("x1"));
    assertEquals(null, lbf.getType("&x1"));
    assertFalse(TestBeanFactory.initialized);

    lbf.registerAlias("x1", "x2");
    assertTrue(lbf.containsBean("x2"));
    assertFalse(lbf.containsBean("&x2"));
    assertTrue(lbf.containsLocalBean("x2"));
    assertFalse(lbf.containsLocalBean("&x2"));
    assertFalse(lbf.isSingleton("x2"));
    assertFalse(lbf.isSingleton("&x2"));
    assertTrue(lbf.isPrototype("x2"));
    assertFalse(lbf.isPrototype("&x2"));
    assertTrue(lbf.isTypeMatch("x2", TestBean.class));
    assertFalse(lbf.isTypeMatch("&x2", TestBean.class));
    assertTrue(lbf.isTypeMatch("x2", Object.class));
    assertFalse(lbf.isTypeMatch("&x2", Object.class));
    assertEquals(TestBean.class, lbf.getType("x2"));
    assertEquals(null, lbf.getType("&x2"));
    assertEquals(1, lbf.getAliases("x1").length);
    assertEquals("x2", lbf.getAliases("x1")[0]);
    assertEquals(1, lbf.getAliases("&x1").length);
    assertEquals("&x2", lbf.getAliases("&x1")[0]);
    assertEquals(1, lbf.getAliases("x2").length);
    assertEquals("x1", lbf.getAliases("x2")[0]);
    assertEquals(1, lbf.getAliases("&x2").length);
    assertEquals("&x1", lbf.getAliases("&x2")[0]);
}