Search in sources :

Example 1 with ChildBeanDefinition

use of cn.taketoday.beans.factory.support.ChildBeanDefinition in project today-framework by TAKETODAY.

the class PropertyResourceConfigurerTests method doTestPropertyPlaceholderConfigurer.

private void doTestPropertyPlaceholderConfigurer(boolean parentChildSeparation) {
    Map<String, String> singletonMap = Collections.singletonMap("myKey", "myValue");
    if (parentChildSeparation) {
        PropertyValues pvs1 = new PropertyValues();
        pvs1.add("age", "${age}");
        PropertyValues pvs2 = new PropertyValues();
        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 {
        PropertyValues pvs = new PropertyValues();
        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}");
    PropertyValues pvs = new PropertyValues();
    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"));
    PropertyValues innerPvs = new PropertyValues();
    innerPvs.add("country", "${os.name}");
    RootBeanDefinition innerBd = new RootBeanDefinition(TestBean.class);
    innerBd.setPropertyValues(innerPvs);
    someMap.put("key3", innerBd);
    PropertyValues innerPvs2 = new PropertyValues(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 : PropertyPlaceholderConfigurer(cn.taketoday.beans.factory.config.PropertyPlaceholderConfigurer) PropertyValues(cn.taketoday.beans.PropertyValues) ChildBeanDefinition(cn.taketoday.beans.factory.support.ChildBeanDefinition) Properties(java.util.Properties) ConstructorArgumentValues(cn.taketoday.beans.factory.config.ConstructorArgumentValues) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) BeanDefinitionHolder(cn.taketoday.beans.factory.config.BeanDefinitionHolder) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) TypedStringValue(cn.taketoday.beans.factory.config.TypedStringValue) RuntimeBeanReference(cn.taketoday.beans.factory.config.RuntimeBeanReference)

Example 2 with ChildBeanDefinition

use of cn.taketoday.beans.factory.support.ChildBeanDefinition in project today-framework by TAKETODAY.

the class ConfigurationClassPostProcessorTests method postProcessorIntrospectsInheritedDefinitionsCorrectly.

/**
 * Tests whether a bean definition without a specified bean class is handled correctly.
 */
@Test
void postProcessorIntrospectsInheritedDefinitionsCorrectly() {
    beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class));
    beanFactory.registerBeanDefinition("parent", new RootBeanDefinition(TestBean.class));
    beanFactory.registerBeanDefinition("child", new ChildBeanDefinition("parent"));
    ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
    pp.postProcessBeanFactory(beanFactory);
    Foo foo = beanFactory.getBean("foo", Foo.class);
    Bar bar = beanFactory.getBean("bar", Bar.class);
    assertThat(bar.foo).isSameAs(foo);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) ChildBeanDefinition(cn.taketoday.beans.factory.support.ChildBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 3 with ChildBeanDefinition

use of cn.taketoday.beans.factory.support.ChildBeanDefinition in project today-framework by TAKETODAY.

the class PropertyResourceConfigurerTests method doTestPropertyPlaceholderConfigurer.

private void doTestPropertyPlaceholderConfigurer(boolean parentChildSeparation) {
    Map<String, String> singletonMap = Collections.singletonMap("myKey", "myValue");
    if (parentChildSeparation) {
        PropertyValues pvs1 = new PropertyValues();
        pvs1.add("age", "${age}");
        PropertyValues pvs2 = new PropertyValues();
        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 {
        PropertyValues pvs = new PropertyValues();
        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}");
    PropertyValues pvs = new PropertyValues();
    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"));
    PropertyValues innerPvs = new PropertyValues();
    innerPvs.add("country", "${os.name}");
    RootBeanDefinition innerBd = new RootBeanDefinition(TestBean.class);
    innerBd.setPropertyValues(innerPvs);
    someMap.put("key3", innerBd);
    PropertyValues innerPvs2 = new PropertyValues(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 : PropertyValues(cn.taketoday.beans.PropertyValues) ChildBeanDefinition(cn.taketoday.beans.factory.support.ChildBeanDefinition) Properties(java.util.Properties) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition)

Example 4 with ChildBeanDefinition

use of cn.taketoday.beans.factory.support.ChildBeanDefinition in project today-infrastructure by TAKETODAY.

the class PropertyResourceConfigurerTests method doTestPropertyPlaceholderConfigurer.

private void doTestPropertyPlaceholderConfigurer(boolean parentChildSeparation) {
    Map<String, String> singletonMap = Collections.singletonMap("myKey", "myValue");
    if (parentChildSeparation) {
        PropertyValues pvs1 = new PropertyValues();
        pvs1.add("age", "${age}");
        PropertyValues pvs2 = new PropertyValues();
        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 {
        PropertyValues pvs = new PropertyValues();
        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}");
    PropertyValues pvs = new PropertyValues();
    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"));
    PropertyValues innerPvs = new PropertyValues();
    innerPvs.add("country", "${os.name}");
    RootBeanDefinition innerBd = new RootBeanDefinition(TestBean.class);
    innerBd.setPropertyValues(innerPvs);
    someMap.put("key3", innerBd);
    PropertyValues innerPvs2 = new PropertyValues(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 : PropertyValues(cn.taketoday.beans.PropertyValues) ChildBeanDefinition(cn.taketoday.beans.factory.support.ChildBeanDefinition) Properties(java.util.Properties) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition)

Example 5 with ChildBeanDefinition

use of cn.taketoday.beans.factory.support.ChildBeanDefinition in project today-infrastructure by TAKETODAY.

the class ConfigurationClassPostProcessorTests method postProcessorIntrospectsInheritedDefinitionsCorrectly.

/**
 * Tests whether a bean definition without a specified bean class is handled correctly.
 */
@Test
void postProcessorIntrospectsInheritedDefinitionsCorrectly() {
    beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class));
    beanFactory.registerBeanDefinition("parent", new RootBeanDefinition(TestBean.class));
    beanFactory.registerBeanDefinition("child", new ChildBeanDefinition("parent"));
    ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
    pp.postProcessBeanFactory(beanFactory);
    Foo foo = beanFactory.getBean("foo", Foo.class);
    Bar bar = beanFactory.getBean("bar", Bar.class);
    assertThat(bar.foo).isSameAs(foo);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) ChildBeanDefinition(cn.taketoday.beans.factory.support.ChildBeanDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

ChildBeanDefinition (cn.taketoday.beans.factory.support.ChildBeanDefinition)5 RootBeanDefinition (cn.taketoday.beans.factory.support.RootBeanDefinition)5 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)5 PropertyValues (cn.taketoday.beans.PropertyValues)3 IndexedTestBean (cn.taketoday.beans.testfixture.beans.IndexedTestBean)3 Properties (java.util.Properties)3 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)2 Test (org.junit.jupiter.api.Test)2 BeanDefinitionHolder (cn.taketoday.beans.factory.config.BeanDefinitionHolder)1 ConstructorArgumentValues (cn.taketoday.beans.factory.config.ConstructorArgumentValues)1 PropertyPlaceholderConfigurer (cn.taketoday.beans.factory.config.PropertyPlaceholderConfigurer)1 RuntimeBeanReference (cn.taketoday.beans.factory.config.RuntimeBeanReference)1 TypedStringValue (cn.taketoday.beans.factory.config.TypedStringValue)1