Search in sources :

Example 6 with ChildBeanDefinition

use of org.springframework.beans.factory.support.ChildBeanDefinition in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method testCanReferenceParentBeanFromChildViaAlias.

@Test
public void testCanReferenceParentBeanFromChildViaAlias() {
    final String EXPECTED_NAME = "Juergen";
    final int EXPECTED_AGE = 41;
    RootBeanDefinition parentDefinition = new RootBeanDefinition(TestBean.class);
    parentDefinition.setAbstract(true);
    parentDefinition.getPropertyValues().add("name", EXPECTED_NAME);
    parentDefinition.getPropertyValues().add("age", EXPECTED_AGE);
    ChildBeanDefinition childDefinition = new ChildBeanDefinition("alias");
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    factory.registerBeanDefinition("parent", parentDefinition);
    factory.registerBeanDefinition("child", childDefinition);
    factory.registerAlias("parent", "alias");
    TestBean child = (TestBean) factory.getBean("child");
    assertEquals(EXPECTED_NAME, child.getName());
    assertEquals(EXPECTED_AGE, child.getAge());
    assertEquals("Use cached merged bean definition", factory.getMergedBeanDefinition("child"), factory.getMergedBeanDefinition("child"));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ChildBeanDefinition(org.springframework.beans.factory.support.ChildBeanDefinition) Test(org.junit.Test)

Example 7 with ChildBeanDefinition

use of org.springframework.beans.factory.support.ChildBeanDefinition in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method canReferenceParentBeanFromChildViaAlias.

@Test
void canReferenceParentBeanFromChildViaAlias() {
    final String EXPECTED_NAME = "Juergen";
    final int EXPECTED_AGE = 41;
    RootBeanDefinition parentDefinition = new RootBeanDefinition(TestBean.class);
    parentDefinition.setAbstract(true);
    parentDefinition.getPropertyValues().add("name", EXPECTED_NAME);
    parentDefinition.getPropertyValues().add("age", EXPECTED_AGE);
    ChildBeanDefinition childDefinition = new ChildBeanDefinition("alias");
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    factory.registerBeanDefinition("parent", parentDefinition);
    factory.registerBeanDefinition("child", childDefinition);
    factory.registerAlias("parent", "alias");
    TestBean child = factory.getBean("child", TestBean.class);
    assertThat(child.getName()).isEqualTo(EXPECTED_NAME);
    assertThat(child.getAge()).isEqualTo(EXPECTED_AGE);
    BeanDefinition mergedBeanDefinition1 = factory.getMergedBeanDefinition("child");
    BeanDefinition mergedBeanDefinition2 = factory.getMergedBeanDefinition("child");
    assertThat(mergedBeanDefinition1).as("Use cached merged bean definition").isSameAs(mergedBeanDefinition2);
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ChildBeanDefinition(org.springframework.beans.factory.support.ChildBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ChildBeanDefinition(org.springframework.beans.factory.support.ChildBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.jupiter.api.Test)

Example 8 with ChildBeanDefinition

use of org.springframework.beans.factory.support.ChildBeanDefinition in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method scopeInheritanceForChildBeanDefinitions.

@Test
void scopeInheritanceForChildBeanDefinitions() {
    String theParentScope = "bonanza!";
    RootBeanDefinition parent = new RootBeanDefinition();
    parent.setScope(theParentScope);
    AbstractBeanDefinition child = new ChildBeanDefinition("parent");
    child.setBeanClass(TestBean.class);
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    factory.registerBeanDefinition("parent", parent);
    factory.registerBeanDefinition("child", child);
    BeanDefinition def = factory.getMergedBeanDefinition("child");
    assertThat(def.getScope()).as("Child 'scope' not inherited").isEqualTo(theParentScope);
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ChildBeanDefinition(org.springframework.beans.factory.support.ChildBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ChildBeanDefinition(org.springframework.beans.factory.support.ChildBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.jupiter.api.Test)

Example 9 with ChildBeanDefinition

use of org.springframework.beans.factory.support.ChildBeanDefinition in project spring-framework by spring-projects.

the class PropertyResourceConfigurerTests method doTestPropertyPlaceholderConfigurer.

private void doTestPropertyPlaceholderConfigurer(boolean parentChildSeparation) {
    Map<String, String> singletonMap = Collections.singletonMap("myKey", "myValue");
    if (parentChildSeparation) {
        MutablePropertyValues pvs1 = new MutablePropertyValues();
        pvs1.add("age", "${age}");
        MutablePropertyValues pvs2 = new MutablePropertyValues();
        pvs2.add("name", "name${var}${var}${");
        pvs2.add("spouse", new RuntimeBeanReference("${ref}"));
        pvs2.add("someMap", singletonMap);
        RootBeanDefinition parent = new RootBeanDefinition(TestBean.class);
        parent.setPropertyValues(pvs1);
        ChildBeanDefinition bd = new ChildBeanDefinition("${parent}", pvs2);
        factory.registerBeanDefinition("parent1", parent);
        factory.registerBeanDefinition("tb1", bd);
    } else {
        MutablePropertyValues pvs = new MutablePropertyValues();
        pvs.add("age", "${age}");
        pvs.add("name", "name${var}${var}${");
        pvs.add("spouse", new RuntimeBeanReference("${ref}"));
        pvs.add("someMap", singletonMap);
        RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
        bd.setPropertyValues(pvs);
        factory.registerBeanDefinition("tb1", bd);
    }
    ConstructorArgumentValues cas = new ConstructorArgumentValues();
    cas.addIndexedArgumentValue(1, "${age}");
    cas.addGenericArgumentValue("${var}name${age}");
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("stringArray", new String[] { "${os.name}", "${age}" });
    List<Object> friends = ManagedList.of("na${age}me", new RuntimeBeanReference("${ref}"));
    pvs.add("friends", friends);
    Set<Object> someSet = ManagedSet.of("na${age}me", new RuntimeBeanReference("${ref}"), new TypedStringValue("${age}", Integer.class));
    pvs.add("someSet", someSet);
    Map<Object, Object> someMap = ManagedMap.ofEntries(Map.entry(new TypedStringValue("key${age}"), new TypedStringValue("${age}")), Map.entry(new TypedStringValue("key${age}ref"), new RuntimeBeanReference("${ref}")), Map.entry("key1", new RuntimeBeanReference("${ref}")), Map.entry("key2", "${age}name"));
    MutablePropertyValues innerPvs = new MutablePropertyValues();
    innerPvs.add("country", "${os.name}");
    RootBeanDefinition innerBd = new RootBeanDefinition(TestBean.class);
    innerBd.setPropertyValues(innerPvs);
    someMap.put("key3", innerBd);
    MutablePropertyValues innerPvs2 = new MutablePropertyValues(innerPvs);
    someMap.put("${key4}", new BeanDefinitionHolder(new ChildBeanDefinition("tb1", innerPvs2), "child"));
    pvs.add("someMap", someMap);
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, cas, pvs);
    factory.registerBeanDefinition("tb2", bd);
    PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
    Properties props = new Properties();
    props.setProperty("age", "98");
    props.setProperty("var", "${m}var");
    props.setProperty("ref", "tb2");
    props.setProperty("m", "my");
    props.setProperty("key4", "mykey4");
    props.setProperty("parent", "parent1");
    ppc.setProperties(props);
    ppc.postProcessBeanFactory(factory);
    TestBean tb1 = (TestBean) factory.getBean("tb1");
    TestBean tb2 = (TestBean) factory.getBean("tb2");
    assertThat(tb1.getAge()).isEqualTo(98);
    assertThat(tb2.getAge()).isEqualTo(98);
    assertThat(tb1.getName()).isEqualTo("namemyvarmyvar${");
    assertThat(tb2.getName()).isEqualTo("myvarname98");
    assertThat(tb1.getSpouse()).isEqualTo(tb2);
    assertThat(tb1.getSomeMap().size()).isEqualTo(1);
    assertThat(tb1.getSomeMap().get("myKey")).isEqualTo("myValue");
    assertThat(tb2.getStringArray().length).isEqualTo(2);
    assertThat(tb2.getStringArray()[0]).isEqualTo(System.getProperty("os.name"));
    assertThat(tb2.getStringArray()[1]).isEqualTo("98");
    assertThat(tb2.getFriends().size()).isEqualTo(2);
    assertThat(tb2.getFriends().iterator().next()).isEqualTo("na98me");
    assertThat(tb2.getFriends().toArray()[1]).isEqualTo(tb2);
    assertThat(tb2.getSomeSet().size()).isEqualTo(3);
    assertThat(tb2.getSomeSet().contains("na98me")).isTrue();
    assertThat(tb2.getSomeSet().contains(tb2)).isTrue();
    assertThat(tb2.getSomeSet().contains(98)).isTrue();
    assertThat(tb2.getSomeMap().size()).isEqualTo(6);
    assertThat(tb2.getSomeMap().get("key98")).isEqualTo("98");
    assertThat(tb2.getSomeMap().get("key98ref")).isEqualTo(tb2);
    assertThat(tb2.getSomeMap().get("key1")).isEqualTo(tb2);
    assertThat(tb2.getSomeMap().get("key2")).isEqualTo("98name");
    TestBean inner1 = (TestBean) tb2.getSomeMap().get("key3");
    TestBean inner2 = (TestBean) tb2.getSomeMap().get("mykey4");
    assertThat(inner1.getAge()).isEqualTo(0);
    assertThat(inner1.getName()).isNull();
    assertThat(inner1.getCountry()).isEqualTo(System.getProperty("os.name"));
    assertThat(inner2.getAge()).isEqualTo(98);
    assertThat(inner2.getName()).isEqualTo("namemyvarmyvar${");
    assertThat(inner2.getCountry()).isEqualTo(System.getProperty("os.name"));
}
Also used : ChildBeanDefinition(org.springframework.beans.factory.support.ChildBeanDefinition) Properties(java.util.Properties) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition)

Aggregations

ChildBeanDefinition (org.springframework.beans.factory.support.ChildBeanDefinition)9 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)9 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)6 Test (org.junit.jupiter.api.Test)4 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)4 Test (org.junit.Test)3 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)3 TestBean (org.springframework.beans.testfixture.beans.TestBean)3 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)2 Properties (java.util.Properties)1 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)1 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)1 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)1 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)1 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)1 ITestBean (org.springframework.tests.sample.beans.ITestBean)1 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)1 TestBean (org.springframework.tests.sample.beans.TestBean)1