@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")); }
@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", new Integer(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")); }
/** * Try getting the beanClassName from the definition and if that fails try to get it from * the parent (and even parent BeanFactory if we have to). * * @param definition * @param registry * @return class name or null if not found */ private String resolveBeanClassname(BeanDefinition definition, BeanDefinitionRegistry registry) { String beanClassName = definition.getBeanClassName(); if (!StringUtils.hasText(beanClassName)) { while (definition instanceof ChildBeanDefinition ) { String parentName = ((ChildBeanDefinition)definition).getParentName(); BeanDefinition parentDefinition = findParentDefinition(parentName, registry); if (parentDefinition == null) { if (log.isDebugEnabled()) { log.debug("No parent bean named '" + parentName + "' could be found in the " + "hierarchy of BeanFactorys. Check you've defined a bean called '" + parentName + "'"); } break; } beanClassName = parentDefinition.getBeanClassName(); if (StringUtils.hasText(beanClassName )) { // found the class name we were looking for break; } definition = parentDefinition; } } return beanClassName; }
/** * Tests whether a bean definition without a specified bean class is handled * correctly. */ @Test public 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); assertSame(foo, bar.foo); }
@Test public void testGetTypeWorksAfterParentChildMerging() { RootBeanDefinition parentDefinition = new RootBeanDefinition(TestBean.class); ChildBeanDefinition childDefinition = new ChildBeanDefinition("parent", DerivedTestBean.class, null, null); DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); factory.registerBeanDefinition("parent", parentDefinition); factory.registerBeanDefinition("child", childDefinition); factory.freezeConfiguration(); assertEquals(TestBean.class, factory.getType("parent")); assertEquals(DerivedTestBean.class, factory.getType("child")); }
@Test public void testScopeInheritanceForChildBeanDefinitions() throws Exception { RootBeanDefinition parent = new RootBeanDefinition(); parent.setScope("bonanza!"); 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"); assertEquals("Child 'scope' not inherited", "bonanza!", def.getScope()); }
protected void emitCustomStoreBeanDefinition(final BeanDefinitionRegistry registry, final String storeName) { if (registry.containsBeanDefinition(storeName)) { throw new AlfrescoRuntimeException( storeName + " (custom content store) cannot be defined - a bean with same name already exists"); } final MessageFormat mf = new MessageFormat("{0}.{1}.", Locale.ENGLISH); final String prefix = mf.format(new Object[] { PROP_CUSTOM_STORE_PREFIX, storeName }); final String typeProperty = prefix + "type"; final String typeValue = this.propertiesSource.getProperty(typeProperty); if (typeValue != null && !typeValue.isEmpty()) { LOGGER.debug("Emitting bean definition for custom store {} based on template {}", storeName, typeValue); final BeanDefinition storeBeanDefinition = new ChildBeanDefinition(STORE_TEMPLATE_PREFIX + typeValue); storeBeanDefinition.setScope(BeanDefinition.SCOPE_SINGLETON); final Set<String> propertyNames = this.propertiesSource.stringPropertyNames(); for (final String propertyName : propertyNames) { if (propertyName.startsWith(prefix) && !typeProperty.equals(propertyName)) { this.handleBeanProperty(storeBeanDefinition, propertyName, this.propertiesSource.getProperty(propertyName)); } } registry.registerBeanDefinition(storeName, storeBeanDefinition); } else { LOGGER.warn("Custom store {} does not define a type", storeName); throw new AlfrescoRuntimeException(storeName + " (custom content store) has not been given a type"); } }
protected void generateMissingLookupDefinitions() { Collection<LookupView> lookupViewBeans = ddBeans.getBeansOfType(LookupView.class).values(); // Index all the inquiry views by the data object class so we can find them easily below Map<Class<?>,LookupView> defaultViewsByDataObjectClass = new HashMap<>(); for ( LookupView view : lookupViewBeans ) { if ( view.getViewName().equals(UifConstants.DEFAULT_VIEW_NAME) ) { defaultViewsByDataObjectClass.put(view.getDataObjectClass(), view); } } for (DataObjectEntry entry : ddBeans.getBeansOfType(DataObjectEntry.class).values()) { // if an inquiry already exists, just ignore - we only default if none exist if ( defaultViewsByDataObjectClass.containsKey(entry.getDataObjectClass())) { continue; } // We only generate the inquiry if the metadata says to if ( entry.getDataObjectMetadata() == null ) { continue; } if ( !entry.getDataObjectMetadata().shouldAutoCreateUifViewOfType(UifAutoCreateViewType.LOOKUP)) { continue; } // no inquiry exists and we want one to, create one if ( LOG.isInfoEnabled() ) { LOG.info( "Generating Lookup View for : " + entry.getDataObjectClass() ); } String lookupBeanName = entry.getDataObjectClass().getSimpleName()+"-LookupView-default"; LookupView lookupView = KRADServiceLocatorWeb.getUifDefaultingService().deriveLookupViewFromMetadata(entry); lookupView.setId(lookupBeanName); lookupView.setViewName(UifConstants.DEFAULT_VIEW_NAME); ChildBeanDefinition lookupBean = new ChildBeanDefinition(ComponentFactory.LOOKUP_VIEW); lookupBean.setScope(BeanDefinition.SCOPE_SINGLETON); lookupBean.setAttribute("dataObjectClassName", lookupView.getDataObjectClass()); lookupBean.getPropertyValues().add("dataObjectClassName", lookupView.getDataObjectClass().getName()); lookupBean.setResourceDescription("Autogenerated From Metadata"); ddBeans.registerBeanDefinition(lookupBeanName, lookupBean); ddBeans.registerSingleton(lookupBeanName, lookupView); } }
/** * Tests whether a bean definition without a specified bean class is handled * correctly. */ @Test public void testPostProcessorIntrospectsInheritedDefinitionsCorrectly() { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); 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); assertSame(foo, bar.foo); }
/** * {@inheritDoc} */ @Override protected AbstractBeanDefinition parseInternal(@Nullable Element element, @Nullable ParserContext parserContext) { // Here we parse the Spring elements such as < property> if (parserContext==null || element == null ) throw GlobalUtils.createNotInitializedException("element and parserContext"); // Here we parse the Spring elements such as < property> BeanDefinitionHolder holder = parserContext.getDelegate().parseBeanDefinitionElement(element); BeanDefinition bDef = holder.getBeanDefinition(); bDef.setBeanClassName(PagePropertyCorrectnessAsserter.class.getName()); String resultPage = element .getAttribute(XsdElementConstants.ATTR_ABSTRACTEXPECTEDRESULTASSERTER_RESULTPAGE); if (StringUtils.hasText(resultPage)) { bDef.getConstructorArgumentValues().addGenericArgumentValue( new RuntimeBeanReference(resultPage)); } String stepERValue = element .getAttribute(XsdElementConstants.ATTR_ABSTRACTEXPECTEDRESULTASSERTER_STEPERVALUE); if (StringUtils.hasText(stepERValue)) { ConstructorArgumentValues erValueDefConstrs = new ConstructorArgumentValues(); erValueDefConstrs.addGenericArgumentValue(stepERValue); BeanDefinition erValueDef = new ChildBeanDefinition( XsdElementConstants.ELEMENT_ID_BASEERVALUE, StepErPagePropertyValue.class, erValueDefConstrs, null); parserContext.getRegistry().registerBeanDefinition(element.getAttribute("id") + "_ASSERTER_STEPERVALUE_ID", erValueDef); bDef.getConstructorArgumentValues().addGenericArgumentValue( new RuntimeBeanReference(element.getAttribute("id") + "_ASSERTER_STEPERVALUE_ID")); } parserContext.getRegistry().registerBeanDefinition(element.getAttribute("id"), bDef); return (AbstractBeanDefinition) bDef; }
protected void generateMissingLookupDefinitions() { Collection<LookupView> lookupViewBeans = ddBeans.getBeansOfType(LookupView.class).values(); // Index all the inquiry views by the data object class so we can find them easily below Map<Class<?>,LookupView> defaultViewsByDataObjectClass = new HashMap<Class<?>, LookupView>(); for ( LookupView view : lookupViewBeans ) { if ( view.getViewName().equals(UifConstants.DEFAULT_VIEW_NAME) ) { defaultViewsByDataObjectClass.put(view.getDataObjectClass(), view); } } for (DataObjectEntry entry : ddBeans.getBeansOfType(DataObjectEntry.class).values()) { // if an inquiry already exists, just ignore - we only default if none exist if ( defaultViewsByDataObjectClass.containsKey(entry.getDataObjectClass())) { continue; } // We only generate the inquiry if the metadata says to if ( entry.getDataObjectMetadata() == null ) { continue; } if ( !entry.getDataObjectMetadata().shouldAutoCreateUifViewOfType(UifAutoCreateViewType.LOOKUP)) { continue; } // no inquiry exists and we want one to, create one if ( LOG.isInfoEnabled() ) { LOG.info( "Generating Lookup View for : " + entry.getDataObjectClass() ); } String lookupBeanName = entry.getDataObjectClass().getSimpleName()+"-LookupView-default"; LookupView lookupView = KRADServiceLocatorWeb.getUifDefaultingService().deriveLookupViewFromMetadata(entry); lookupView.setId(lookupBeanName); lookupView.setViewName(UifConstants.DEFAULT_VIEW_NAME); ChildBeanDefinition lookupBean = new ChildBeanDefinition(ComponentFactory.LOOKUP_VIEW); lookupBean.setScope(BeanDefinition.SCOPE_SINGLETON); lookupBean.setAttribute("dataObjectClassName", lookupView.getDataObjectClass()); lookupBean.getPropertyValues().add("dataObjectClassName", lookupView.getDataObjectClass().getName()); lookupBean.setResourceDescription("Autogenerated From Metadata"); ddBeans.registerBeanDefinition(lookupBeanName, lookupBean); ddBeans.registerSingleton(lookupBeanName, lookupView); } }
protected void generateMissingInquiryDefinitions() { Collection<InquiryView> inquiryViewBeans = ddBeans.getBeansOfType(InquiryView.class).values(); // Index all the inquiry views by the data object class so we can find them easily below Map<Class<?>,InquiryView> defaultViewsByDataObjectClass = new HashMap<>(); for ( InquiryView view : inquiryViewBeans ) { if ( view.getViewName().equals(UifConstants.DEFAULT_VIEW_NAME) ) { defaultViewsByDataObjectClass.put(view.getDataObjectClassName(), view); } } for (DataObjectEntry entry : ddBeans.getBeansOfType(DataObjectEntry.class).values()) { // if an inquiry already exists, just ignore - we only default if none exist if ( defaultViewsByDataObjectClass.containsKey(entry.getDataObjectClass())) { continue; } // We only generate the inquiry if the metadata says to if ( entry.getDataObjectMetadata() == null ) { continue; } if ( !entry.getDataObjectMetadata().shouldAutoCreateUifViewOfType(UifAutoCreateViewType.INQUIRY)) { continue; } // no inquiry exists and we want one to, create one if ( LOG.isInfoEnabled() ) { LOG.info( "Generating Inquiry View for : " + entry.getDataObjectClass() ); } String inquiryBeanName = entry.getDataObjectClass().getSimpleName()+"-InquiryView-default"; InquiryView inquiryView = KRADServiceLocatorWeb.getUifDefaultingService().deriveInquiryViewFromMetadata(entry); inquiryView.setId(inquiryBeanName); inquiryView.setViewName(UifConstants.DEFAULT_VIEW_NAME); ChildBeanDefinition inquiryBean = new ChildBeanDefinition("Uif-InquiryView"); inquiryBean.setScope(BeanDefinition.SCOPE_SINGLETON); inquiryBean.setAttribute("dataObjectClassName", inquiryView.getDataObjectClassName()); inquiryBean.getPropertyValues().add("dataObjectClassName", inquiryView.getDataObjectClassName().getName()); inquiryBean.setResourceDescription("Autogenerated From Metadata"); ddBeans.registerBeanDefinition(inquiryBeanName, inquiryBean); ddBeans.registerSingleton(inquiryBeanName, inquiryView); } }
@Test @Deprecated public void testServletContextPropertyPlaceholderConfigurerWithAttributes() { MockServletContext sc = new MockServletContext(); sc.addInitParameter("key4", "mykey4"); StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(sc); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("age", "${age}"); pvs.add("name", "name${var}${var}${"); pvs.add("spouse", new RuntimeBeanReference("${ref}")); wac.registerSingleton("tb1", TestBean.class, pvs); ConstructorArgumentValues cas = new ConstructorArgumentValues(); cas.addIndexedArgumentValue(1, "${age}"); cas.addGenericArgumentValue("${var}name${age}"); pvs = new MutablePropertyValues(); List<Object> friends = new ManagedList<Object>(); friends.add("na${age}me"); friends.add(new RuntimeBeanReference("${ref}")); pvs.add("friends", friends); Set<Object> someSet = new ManagedSet<Object>(); someSet.add("na${age}me"); someSet.add(new RuntimeBeanReference("${ref}")); pvs.add("someSet", someSet); Map<String, Object> someMap = new ManagedMap<String, Object>(); someMap.put("key1", new RuntimeBeanReference("${ref}")); someMap.put("key2", "${age}name"); MutablePropertyValues innerPvs = new MutablePropertyValues(); innerPvs.add("touchy", "${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); wac.getDefaultListableBeanFactory().registerBeanDefinition("tb2", bd); pvs = new MutablePropertyValues(); pvs.add("properties", "var=${m}var\nref=tb2\nm=my"); pvs.add("searchContextAttributes", Boolean.TRUE); wac.registerSingleton("configurer", ServletContextPropertyPlaceholderConfigurer.class, pvs); sc.setAttribute("age", new Integer(98)); wac.refresh(); TestBean tb1 = (TestBean) wac.getBean("tb1"); TestBean tb2 = (TestBean) wac.getBean("tb2"); assertEquals(98, tb1.getAge()); assertEquals(98, tb2.getAge()); assertEquals("namemyvarmyvar${", tb1.getName()); assertEquals("myvarname98", tb2.getName()); assertEquals(tb2, tb1.getSpouse()); assertEquals(2, tb2.getFriends().size()); assertEquals("na98me", tb2.getFriends().iterator().next()); assertEquals(tb2, tb2.getFriends().toArray()[1]); assertEquals(2, tb2.getSomeSet().size()); assertTrue(tb2.getSomeSet().contains("na98me")); assertTrue(tb2.getSomeSet().contains(tb2)); assertEquals(4, tb2.getSomeMap().size()); assertEquals(tb2, tb2.getSomeMap().get("key1")); assertEquals("98name", tb2.getSomeMap().get("key2")); TestBean inner1 = (TestBean) tb2.getSomeMap().get("key3"); TestBean inner2 = (TestBean) tb2.getSomeMap().get("mykey4"); assertEquals(0, inner1.getAge()); assertEquals(null, inner1.getName()); assertEquals(System.getProperty("os.name"), inner1.getTouchy()); assertEquals(98, inner2.getAge()); assertEquals("namemyvarmyvar${", inner2.getName()); assertEquals(System.getProperty("os.name"), inner2.getTouchy()); }
/** * {@inheritDoc} */ @Override protected AbstractBeanDefinition parseInternal(@Nullable Element element, @Nullable ParserContext parserContext) { // Here we parse the Spring elements such as < property> if (parserContext == null || element == null) throw GlobalUtils .createNotInitializedException("element and parserContext"); // Here we parse the Spring elements such as < property> BeanDefinitionHolder holder = parserContext.getDelegate() .parseBeanDefinitionElement(element); BeanDefinition bDef = holder.getBeanDefinition(); bDef.setBeanClassName(PageElementExistenceAsserter.class.getName()); String resultPage = element .getAttribute(XsdElementConstants.ATTR_ABSTRACTEXPECTEDRESULTASSERTER_RESULTPAGE); if (StringUtils.hasText(resultPage)) { bDef.getConstructorArgumentValues().addGenericArgumentValue( new RuntimeBeanReference(resultPage)); } String stepERValue = element .getAttribute(XsdElementConstants.ATTR_ABSTRACTEXPECTEDRESULTASSERTER_STEPERVALUE); if (StringUtils.hasText(stepERValue)) { ConstructorArgumentValues erValueDefConstrs = new ConstructorArgumentValues(); erValueDefConstrs.addGenericArgumentValue(stepERValue); BeanDefinition erValueDef = new ChildBeanDefinition( XsdElementConstants.ELEMENT_ID_BASEERVALUE, StepErElementExistenceValue.class, erValueDefConstrs, null); parserContext.getRegistry().registerBeanDefinition(element.getAttribute("id") + "_ASSERTER_STEPERVALUE_ID", erValueDef); bDef.getConstructorArgumentValues().addGenericArgumentValue( new RuntimeBeanReference(element.getAttribute("id") + "_ASSERTER_STEPERVALUE_ID")); } parserContext.getRegistry().registerBeanDefinition( element.getAttribute("id"), bDef); return (AbstractBeanDefinition) bDef; }
protected void generateMissingInquiryDefinitions() { Collection<InquiryView> inquiryViewBeans = ddBeans.getBeansOfType(InquiryView.class).values(); // Index all the inquiry views by the data object class so we can find them easily below Map<Class<?>,InquiryView> defaultViewsByDataObjectClass = new HashMap<Class<?>, InquiryView>(); for ( InquiryView view : inquiryViewBeans ) { if ( view.getViewName().equals(UifConstants.DEFAULT_VIEW_NAME) ) { defaultViewsByDataObjectClass.put(view.getDataObjectClassName(), view); } } for (DataObjectEntry entry : ddBeans.getBeansOfType(DataObjectEntry.class).values()) { // if an inquiry already exists, just ignore - we only default if none exist if ( defaultViewsByDataObjectClass.containsKey(entry.getDataObjectClass())) { continue; } // We only generate the inquiry if the metadata says to if ( entry.getDataObjectMetadata() == null ) { continue; } if ( !entry.getDataObjectMetadata().shouldAutoCreateUifViewOfType(UifAutoCreateViewType.INQUIRY)) { continue; } // no inquiry exists and we want one to, create one if ( LOG.isInfoEnabled() ) { LOG.info( "Generating Inquiry View for : " + entry.getDataObjectClass() ); } String inquiryBeanName = entry.getDataObjectClass().getSimpleName()+"-InquiryView-default"; InquiryView inquiryView = KRADServiceLocatorWeb.getUifDefaultingService().deriveInquiryViewFromMetadata(entry); inquiryView.setId(inquiryBeanName); inquiryView.setViewName(UifConstants.DEFAULT_VIEW_NAME); ChildBeanDefinition inquiryBean = new ChildBeanDefinition("Uif-InquiryView"); inquiryBean.setScope(BeanDefinition.SCOPE_SINGLETON); inquiryBean.setAttribute("dataObjectClassName", inquiryView.getDataObjectClassName()); inquiryBean.getPropertyValues().add("dataObjectClassName", inquiryView.getDataObjectClassName().getName()); inquiryBean.setResourceDescription("Autogenerated From Metadata"); ddBeans.registerBeanDefinition(inquiryBeanName, inquiryBean); ddBeans.registerSingleton(inquiryBeanName, inquiryView); } }