@Test public void eventBusElement() { BeanDefinition beanDefinition = beanFactory.getBeanDefinition("eventBus"); assertNotNull("Bean definition not created", beanDefinition); assertEquals( "Wrong bean class", SimpleEventBus.class.getName(), beanDefinition.getBeanClassName()); SimpleEventBus eventBus = beanFactory.getBean("eventBus", SimpleEventBus.class); assertNotNull(eventBus); }
/** @see DATAMONGO-1218 */ @Test public void setsUpMongoDbFactoryUsingAMongoClientUri() { reader.loadBeanDefinitions(new ClassPathResource("namespace/mongo-client-uri.xml")); BeanDefinition definition = factory.getBeanDefinition("mongoDbFactory"); ConstructorArgumentValues constructorArguments = definition.getConstructorArgumentValues(); assertThat(constructorArguments.getArgumentCount(), is(1)); ValueHolder argument = constructorArguments.getArgumentValue(0, MongoClientURI.class); assertThat(argument, is(notNullValue())); }
@Test public void logBeansInContext() { DefaultListableBeanFactory factory = (DefaultListableBeanFactory) beanFactory; if (factory != null) { logger.debug("Bean Factory: '{}'", factory); } if (applicationContext.getParent() != null) { logger.debug("Bean Factory: '{}'", applicationContext.getParentBeanFactory()); } logger.debug("******************************************************************************"); String[] beans = applicationContext.getBeanDefinitionNames(); for (String o : beans) { logger.debug("________________________"); logger.debug("BEAN id: '{}'", o); logger.debug("\tType: '{}'", applicationContext.getType(o)); String[] aliases = applicationContext.getAliases(o); if (factory.isFactoryBean(o)) logger.debug("\tFACTORY"); if (aliases != null && aliases.length > 0) { for (String a : aliases) { logger.debug("\tAliased as: '{}'", a); } } if (factory.getBeanDefinition(o).isAbstract()) { logger.debug("\tABSTRACT"); } else { if (applicationContext.isPrototype(o)) logger.debug("\tScope: 'Prototype'"); if (applicationContext.isSingleton(o)) logger.debug("\tScope: 'Singleton'"); Annotation[] annotations = applicationContext.getBean(o).getClass().getAnnotations(); if (annotations != null && annotations.length > 0) { logger.debug("\tAnnotations:"); for (Annotation annotation : annotations) { logger.debug("\t\t'{}'", annotation.annotationType()); } } if (!applicationContext .getBean(o) .toString() .startsWith(applicationContext.getType(o).toString() + "@")) { logger.debug("\tContents: {}", applicationContext.getBean(o).toString()); } } } logger.debug("******************************************************************************"); logger.debug("*** Number of Beans={} ***", applicationContext.getBeanDefinitionCount()); logger.debug("*** Number of Bean Post Processors={} ***", factory.getBeanPostProcessorCount()); logger.debug("******************************************************************************"); }
/** * Load the template BeanDefinition and call {@link #transform(ConfigurableListableBeanFactory, * BeanDefinition, Element, ParserContext)} to apply runtime configuration value to it. <code> * builder</code> will be configured to instantiate the bean in the Spring context that we are * parsing. * * <p>During parsing, an instance of {@link * TemplateBeanDefinitionParser.TemplateComponentDefinition} is pushed onto <code>ParserContext * </code> so that nested tags can access the enclosing template configuration with a call to * {@link #findEnclosingTemplateFactory(ParserContext)}. Subclasses can override {@link * #newComponentDefinition(String, Object, DefaultListableBeanFactory)} to provide a subclass of * {@link TemplateBeanDefinitionParser.TemplateComponentDefinition} to the parser context if * necessary. */ @Override protected final void doParse( Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { // if we have multiple nested bean definitions, we only parse the template factory // once. this allows configuration changes made by enclosing bean parsers to be inherited // by contained beans, which is quite useful. DefaultListableBeanFactory templateFactory = findEnclosingTemplateFactory(parserContext); TemplateComponentDefinition tcd = null; if (templateFactory == null) { // no nesting -- load the template XML configuration from the classpath. final BeanFactory parentFactory = (BeanFactory) parserContext.getRegistry(); templateFactory = new DefaultListableBeanFactory(parentFactory); // load template bean definitions DefaultResourceLoader loader = new DefaultResourceLoader(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(templateFactory); reader.setResourceLoader(loader); reader.setEntityResolver(new ResourceEntityResolver(loader)); reader.loadBeanDefinitions(templateResource); // propagate factory post-processors from the source factory into the template // factory. BeanDefinition ppChain = new RootBeanDefinition(ChainingBeanFactoryPostProcessor.class); ppChain.getPropertyValues().addPropertyValue("targetFactory", templateFactory); parserContext.getReaderContext().registerWithGeneratedName(ppChain); // push component definition onto the parser stack for the benefit of // nested bean definitions. tcd = newComponentDefinition( element.getNodeName(), parserContext.extractSource(element), templateFactory); parserContext.pushContainingComponent(tcd); } try { // allow subclasses to apply overrides to the template bean definition. BeanDefinition def = templateFactory.getBeanDefinition(templateId); transform(templateFactory, def, element, parserContext); // setup our factory bean to instantiate the modified bean definition upon request. builder.addPropertyValue("beanFactory", templateFactory); builder.addPropertyValue("beanName", templateId); builder.getRawBeanDefinition().setAttribute("id", def.getAttribute("id")); } finally { if (tcd != null) parserContext.popContainingComponent(); } }
@Test public void eventBusElement() { BeanDefinition beanDefinition = beanFactory.getBeanDefinition("eventBus"); assertNotNull("Bean definition not created", beanDefinition); assertEquals( "Wrong bean class", SimpleEventBus.class.getName(), beanDefinition.getBeanClassName()); assertEquals( "wrong amount of constructor arguments", 0, beanDefinition.getConstructorArgumentValues().getArgumentCount()); EventBus eventBus = beanFactory.getBean("eventBus", EventBus.class); assertNotNull(eventBus); }
/** @see DATAMONGO-306 */ @Test public void setsUpMongoDbFactoryUsingAMongoUriWithoutCredentials() { reader.loadBeanDefinitions(new ClassPathResource("namespace/mongo-uri-no-credentials.xml")); BeanDefinition definition = factory.getBeanDefinition("mongoDbFactory"); ConstructorArgumentValues constructorArguments = definition.getConstructorArgumentValues(); assertThat(constructorArguments.getArgumentCount(), is(1)); ValueHolder argument = constructorArguments.getArgumentValue(0, MongoURI.class); assertThat(argument, is(notNullValue())); MongoDbFactory dbFactory = factory.getBean("mongoDbFactory", MongoDbFactory.class); DB db = dbFactory.getDb(); assertThat(db.getName(), is("database")); }
@Test public void eventBusElementTrueMBeans() { BeanDefinition beanDefinition = beanFactory.getBeanDefinition("eventBusTrueMBeans"); assertNotNull("Bean definition not created", beanDefinition); assertEquals( "Wrong bean class", SimpleEventBus.class.getName(), beanDefinition.getBeanClassName()); assertEquals( "wrong amount of constructor arguments", 1, beanDefinition.getConstructorArgumentValues().getArgumentCount()); Object constructorValue = beanDefinition.getConstructorArgumentValues().getArgumentValue(0, Boolean.class).getValue(); assertTrue("constructor value is wrong", Boolean.parseBoolean((String) constructorValue)); SimpleEventBus eventBus = beanFactory.getBean("eventBusTrueMBeans", SimpleEventBus.class); assertNotNull(eventBus); }
@Test public void eventBusElementWithTerminal() { BeanDefinition beanDefinition = beanFactory.getBeanDefinition("eventBusTerminal"); assertNotNull("Bean definition not created", beanDefinition); assertEquals( "Wrong bean class", SimpleEventBus.class.getName(), beanDefinition.getBeanClassName()); assertEquals( "wrong amount of constructor arguments", 1, beanDefinition.getConstructorArgumentValues().getArgumentCount()); BeanReference terminalRef = (BeanReference) beanDefinition .getConstructorArgumentValues() .getArgumentValue(0, BeanReference.class) .getValue(); assertEquals("constructor value is wrong", "terminal", terminalRef.getBeanName()); assertNotNull(beanFactory.getBean("eventBusTerminal", EventBus.class)); }