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"));
}
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);
}
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"));
}
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"));
}
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);
}
Aggregations