@Override public BeanDefinition parse(Element element, ParserContext parserContext) { RootBeanDefinition definition = new RootBeanDefinition(); definition.setBeanClass(TestBean.class); MutablePropertyValues mpvs = new MutablePropertyValues(); mpvs.add("name", element.getAttribute("name")); mpvs.add("age", element.getAttribute("age")); definition.setPropertyValues(mpvs); parserContext.getRegistry().registerBeanDefinition(element.getAttribute("id"), definition); return null; } }
@Test public void testSelfReference() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("spouse", new RuntimeBeanReference("self")); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("self", bd); TestBean self = (TestBean) lbf.getBean("self"); assertEquals(self, self.getSpouse()); }
@Test public void testCustomEditorConfigurerWithEditorAsClass() throws ParseException { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); CustomEditorConfigurer cec = new CustomEditorConfigurer(); Map<Class<?>, Class<? extends PropertyEditor>> editors = new HashMap<>(); editors.put(Date.class, MyDateEditor.class); cec.setCustomEditors(editors); cec.postProcessBeanFactory(bf); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("date", "2.12.1975"); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); bf.registerBeanDefinition("tb", bd); TestBean tb = (TestBean) bf.getBean("tb"); DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN); assertEquals(df.parse("2.12.1975"), tb.getDate()); }
@Test public void testExtensiveCircularReference() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); for (int i = 0; i < 1000; i++) { MutablePropertyValues pvs = new MutablePropertyValues(); pvs.addPropertyValue(new PropertyValue("spouse", new RuntimeBeanReference("bean" + (i < 99 ? i + 1 : 0)))); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("bean" + i, bd); } lbf.preInstantiateSingletons(); for (int i = 0; i < 1000; i++) { TestBean bean = (TestBean) lbf.getBean("bean" + i); TestBean otherBean = (TestBean) lbf.getBean("bean" + (i < 99 ? i + 1 : 0)); assertTrue(bean.getSpouse() == otherBean); } }
@Test public void testExpressionInStringArray() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); BeanExpressionResolver beanExpressionResolver = mock(BeanExpressionResolver.class); when(beanExpressionResolver.evaluate(eq("#{foo}"), ArgumentMatchers.any(BeanExpressionContext.class))) .thenReturn("classpath:/org/springframework/beans/factory/xml/util.properties"); bf.setBeanExpressionResolver(beanExpressionResolver); RootBeanDefinition rbd = new RootBeanDefinition(PropertiesFactoryBean.class); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("locations", new String[]{"#{foo}"}); rbd.setPropertyValues(pvs); bf.registerBeanDefinition("myProperties", rbd); Properties properties = (Properties) bf.getBean("myProperties"); assertEquals("bar", properties.getProperty("foo")); }
@Test public void testCustomEditor() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); lbf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() { @Override public void registerCustomEditors(PropertyEditorRegistry registry) { NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN); registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true)); } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("myFloat", "1,1"); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("testBean", bd); TestBean testBean = (TestBean) lbf.getBean("testBean"); assertTrue(testBean.getMyFloat().floatValue() == 1.1f); }
@Test public void testCustomEditorWithBeanReference() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); lbf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() { @Override public void registerCustomEditors(PropertyEditorRegistry registry) { NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN); registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true)); } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("myFloat", new RuntimeBeanReference("myFloat")); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("testBean", bd); lbf.registerSingleton("myFloat", "1,1"); TestBean testBean = (TestBean) lbf.getBean("testBean"); assertTrue(testBean.getMyFloat().floatValue() == 1.1f); }
@Test public void testAutowireWithUnsatisfiedConstructorDependency() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.addPropertyValue(new PropertyValue("name", "Rod")); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("rod", bd); assertEquals(1, lbf.getBeanDefinitionCount()); try { lbf.autowire(UnsatisfiedConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, true); fail("Should have unsatisfied constructor dependency on SideEffectBean"); } catch (UnsatisfiedDependencyException ex) { // expected } }
@Test public void testSerializability() throws Exception { MutablePropertyValues tsPvs = new MutablePropertyValues(); tsPvs.add("targetBeanName", "person"); RootBeanDefinition tsBd = new RootBeanDefinition(TestTargetSource.class); tsBd.setPropertyValues(tsPvs); MutablePropertyValues pvs = new MutablePropertyValues(); RootBeanDefinition bd = new RootBeanDefinition(SerializablePerson.class); bd.setPropertyValues(pvs); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("ts", tsBd); bf.registerBeanDefinition("person", bd); TestTargetSource cpts = (TestTargetSource) bf.getBean("ts"); TargetSource serialized = (TargetSource) SerializationTestUtils.serializeAndDeserialize(cpts); assertTrue("Changed to SingletonTargetSource on deserialization", serialized instanceof SingletonTargetSource); SingletonTargetSource sts = (SingletonTargetSource) serialized; assertNotNull(sts.getTarget()); }
@Test public void testCustomEditorConfigurerWithPropertyEditorRegistrar() throws ParseException { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); CustomEditorConfigurer cec = new CustomEditorConfigurer(); final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN); cec.setPropertyEditorRegistrars(new PropertyEditorRegistrar[] { new PropertyEditorRegistrar() { @Override public void registerCustomEditors(PropertyEditorRegistry registry) { registry.registerCustomEditor(Date.class, new CustomDateEditor(df, true)); } }}); cec.postProcessBeanFactory(bf); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("date", "2.12.1975"); RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class); bd1.setPropertyValues(pvs); bf.registerBeanDefinition("tb1", bd1); pvs = new MutablePropertyValues(); pvs.add("someMap[myKey]", new TypedStringValue("2.12.1975", Date.class)); RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class); bd2.setPropertyValues(pvs); bf.registerBeanDefinition("tb2", bd2); TestBean tb1 = (TestBean) bf.getBean("tb1"); assertEquals(df.parse("2.12.1975"), tb1.getDate()); TestBean tb2 = (TestBean) bf.getBean("tb2"); assertEquals(df.parse("2.12.1975"), tb2.getSomeMap().get("myKey")); }
@Test public void testCustomConverter() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); GenericConversionService conversionService = new DefaultConversionService(); conversionService.addConverter(new Converter<String, Float>() { @Override public Float convert(String source) { try { NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN); return nf.parse(source).floatValue(); } catch (ParseException ex) { throw new IllegalArgumentException(ex); } } }); lbf.setConversionService(conversionService); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("myFloat", "1,1"); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("testBean", bd); TestBean testBean = (TestBean) lbf.getBean("testBean"); assertTrue(testBean.getMyFloat().floatValue() == 1.1f); }
@Test public void testAutowireWithParent() throws Exception { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(AUTOWIRE_CONTEXT); DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "kerry"); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("spouse", bd); xbf.setParentBeanFactory(lbf); doTestAutowire(xbf); }
@Test public void testCustomEditorConfigurerWithRequiredTypeArray() throws ParseException { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); CustomEditorConfigurer cec = new CustomEditorConfigurer(); Map<Class<?>, Class<? extends PropertyEditor>> editors = new HashMap<>(); editors.put(String[].class, MyTestEditor.class); cec.setCustomEditors(editors); cec.postProcessBeanFactory(bf); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("stringArray", "xxx"); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); bf.registerBeanDefinition("tb", bd); TestBean tb = (TestBean) bf.getBean("tb"); assertTrue(tb.getStringArray() != null && tb.getStringArray().length == 1); assertEquals("test", tb.getStringArray()[0]); }
@Test public void testApplyBeanPropertyValues() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("age", "99"); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("test", bd); TestBean tb = new TestBean(); assertEquals(0, tb.getAge()); lbf.applyBeanPropertyValues(tb, "test"); assertEquals(99, tb.getAge()); }
@Test public void testAutowireWithSatisfiedConstructorDependency() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "Rod"); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("rod", bd); assertEquals(1, lbf.getBeanDefinitionCount()); Object registered = lbf.autowire(ConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false); assertEquals(1, lbf.getBeanDefinitionCount()); ConstructorDependency kerry = (ConstructorDependency) registered; TestBean rod = (TestBean) lbf.getBean("rod"); assertSame(rod, kerry.spouse); }
@Test public void testAutowireWithSatisfiedJavaBeanDependency() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "Rod"); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("rod", bd); assertEquals(1, lbf.getBeanDefinitionCount()); // Depends on age, name and spouse (TestBean) Object registered = lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); assertEquals(1, lbf.getBeanDefinitionCount()); DependenciesBean kerry = (DependenciesBean) registered; TestBean rod = (TestBean) lbf.getBean("rod"); assertSame(rod, kerry.getSpouse()); }
@Test public void testApplyBeanPropertyValuesWithIncompleteDefinition() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("age", "99"); RootBeanDefinition bd = new RootBeanDefinition(); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("test", bd); TestBean tb = new TestBean(); assertEquals(0, tb.getAge()); lbf.applyBeanPropertyValues(tb, "test"); assertEquals(99, tb.getAge()); assertNull(tb.getBeanFactory()); assertNull(tb.getSpouse()); }
@Test public void testPossibleMatches() { try { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("ag", "foobar"); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("tb", bd); lbf.getBean("tb"); fail("Should throw exception on invalid property"); } catch (BeanCreationException ex) { assertTrue(ex.getCause() instanceof NotWritablePropertyException); NotWritablePropertyException cause = (NotWritablePropertyException) ex.getCause(); // expected assertEquals(1, cause.getPossibleMatches().length); assertEquals("age", cause.getPossibleMatches()[0]); } }
@Test public void testConfigureBean() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("age", "99"); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs); lbf.registerBeanDefinition("test", bd); TestBean tb = new TestBean(); assertEquals(0, tb.getAge()); lbf.configureBean(tb, "test"); assertEquals(99, tb.getAge()); assertSame(lbf, tb.getBeanFactory()); assertNull(tb.getSpouse()); }
@Test public void testRegisterExistingSingletonWithAutowire() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "Tony"); pvs.add("age", "48"); RootBeanDefinition bd = new RootBeanDefinition(DependenciesBean.class); bd.setPropertyValues(pvs); bd.setDependencyCheck(RootBeanDefinition.DEPENDENCY_CHECK_OBJECTS); bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE); lbf.registerBeanDefinition("test", bd); Object singletonObject = new TestBean(); lbf.registerSingleton("singletonObject", singletonObject); assertTrue(lbf.containsBean("singletonObject")); assertTrue(lbf.isSingleton("singletonObject")); assertEquals(TestBean.class, lbf.getType("singletonObject")); assertEquals(0, lbf.getAliases("singletonObject").length); DependenciesBean test = (DependenciesBean) lbf.getBean("test"); assertEquals(singletonObject, lbf.getBean("singletonObject")); assertEquals(singletonObject, test.getSpouse()); }