Search in sources :

Example 1 with CompositeComponentDefinition

use of org.springframework.beans.factory.parsing.CompositeComponentDefinition in project spring-framework by spring-projects.

the class AopNamespaceHandlerEventTests method testAdvisorEventsWithPointcutRef.

@Test
public void testAdvisorEventsWithPointcutRef() throws Exception {
    this.reader.loadBeanDefinitions(POINTCUT_REF_CONTEXT);
    ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions();
    assertEquals("Incorrect number of events fired", 2, componentDefinitions.length);
    assertTrue("No holder with nested components", componentDefinitions[0] instanceof CompositeComponentDefinition);
    CompositeComponentDefinition compositeDef = (CompositeComponentDefinition) componentDefinitions[0];
    assertEquals("aop:config", compositeDef.getName());
    ComponentDefinition[] nestedComponentDefs = compositeDef.getNestedComponents();
    assertEquals("Incorrect number of inner components", 3, nestedComponentDefs.length);
    AdvisorComponentDefinition acd = null;
    for (int i = 0; i < nestedComponentDefs.length; i++) {
        ComponentDefinition componentDefinition = nestedComponentDefs[i];
        if (componentDefinition instanceof AdvisorComponentDefinition) {
            acd = (AdvisorComponentDefinition) componentDefinition;
            break;
        }
    }
    assertNotNull("AdvisorComponentDefinition not found", acd);
    assertEquals(1, acd.getBeanDefinitions().length);
    assertEquals(2, acd.getBeanReferences().length);
    assertTrue("No advice bean found", componentDefinitions[1] instanceof BeanComponentDefinition);
    BeanComponentDefinition adviceDef = (BeanComponentDefinition) componentDefinitions[1];
    assertEquals("countingAdvice", adviceDef.getBeanName());
}
Also used : CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) ComponentDefinition(org.springframework.beans.factory.parsing.ComponentDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) Test(org.junit.Test)

Example 2 with CompositeComponentDefinition

use of org.springframework.beans.factory.parsing.CompositeComponentDefinition in project spring-framework by spring-projects.

the class AopNamespaceHandlerEventTests method testPointcutEvents.

@Test
public void testPointcutEvents() throws Exception {
    this.reader.loadBeanDefinitions(POINTCUT_EVENTS_CONTEXT);
    ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions();
    assertEquals("Incorrect number of events fired", 1, componentDefinitions.length);
    assertTrue("No holder with nested components", componentDefinitions[0] instanceof CompositeComponentDefinition);
    CompositeComponentDefinition compositeDef = (CompositeComponentDefinition) componentDefinitions[0];
    assertEquals("aop:config", compositeDef.getName());
    ComponentDefinition[] nestedComponentDefs = compositeDef.getNestedComponents();
    assertEquals("Incorrect number of inner components", 2, nestedComponentDefs.length);
    PointcutComponentDefinition pcd = null;
    for (int i = 0; i < nestedComponentDefs.length; i++) {
        ComponentDefinition componentDefinition = nestedComponentDefs[i];
        if (componentDefinition instanceof PointcutComponentDefinition) {
            pcd = (PointcutComponentDefinition) componentDefinition;
            break;
        }
    }
    assertNotNull("PointcutComponentDefinition not found", pcd);
    assertEquals("Incorrect number of BeanDefinitions", 1, pcd.getBeanDefinitions().length);
}
Also used : CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) ComponentDefinition(org.springframework.beans.factory.parsing.ComponentDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) Test(org.junit.Test)

Example 3 with CompositeComponentDefinition

use of org.springframework.beans.factory.parsing.CompositeComponentDefinition in project spring-framework by spring-projects.

the class AopNamespaceHandlerEventTests method testAspectEvent.

@Test
public void testAspectEvent() throws Exception {
    this.reader.loadBeanDefinitions(CONTEXT);
    ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions();
    assertEquals("Incorrect number of events fired", 5, componentDefinitions.length);
    assertTrue("No holder with nested components", componentDefinitions[0] instanceof CompositeComponentDefinition);
    CompositeComponentDefinition compositeDef = (CompositeComponentDefinition) componentDefinitions[0];
    assertEquals("aop:config", compositeDef.getName());
    ComponentDefinition[] nestedComponentDefs = compositeDef.getNestedComponents();
    assertEquals("Incorrect number of inner components", 2, nestedComponentDefs.length);
    AspectComponentDefinition acd = null;
    for (int i = 0; i < nestedComponentDefs.length; i++) {
        ComponentDefinition componentDefinition = nestedComponentDefs[i];
        if (componentDefinition instanceof AspectComponentDefinition) {
            acd = (AspectComponentDefinition) componentDefinition;
            break;
        }
    }
    assertNotNull("AspectComponentDefinition not found", acd);
    BeanDefinition[] beanDefinitions = acd.getBeanDefinitions();
    assertEquals(5, beanDefinitions.length);
    BeanReference[] beanReferences = acd.getBeanReferences();
    assertEquals(6, beanReferences.length);
    Set<String> expectedReferences = new HashSet<>();
    expectedReferences.add("pc");
    expectedReferences.add("countingAdvice");
    for (int i = 0; i < beanReferences.length; i++) {
        BeanReference beanReference = beanReferences[i];
        expectedReferences.remove(beanReference.getBeanName());
    }
    assertEquals("Incorrect references found", 0, expectedReferences.size());
    for (int i = 1; i < componentDefinitions.length; i++) {
        assertTrue(componentDefinitions[i] instanceof BeanComponentDefinition);
    }
    ComponentDefinition[] nestedComponentDefs2 = acd.getNestedComponents();
    assertEquals("Inner PointcutComponentDefinition not found", 1, nestedComponentDefs2.length);
    assertTrue(nestedComponentDefs2[0] instanceof PointcutComponentDefinition);
    PointcutComponentDefinition pcd = (PointcutComponentDefinition) nestedComponentDefs2[0];
    assertEquals("Incorrect number of BeanDefinitions", 1, pcd.getBeanDefinitions().length);
}
Also used : CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) BeanReference(org.springframework.beans.factory.config.BeanReference) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) ComponentDefinition(org.springframework.beans.factory.parsing.ComponentDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with CompositeComponentDefinition

use of org.springframework.beans.factory.parsing.CompositeComponentDefinition in project spring-framework by spring-projects.

the class ConfigBeanDefinitionParser method parse.

@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
    CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), parserContext.extractSource(element));
    parserContext.pushContainingComponent(compositeDef);
    configureAutoProxyCreator(parserContext, element);
    List<Element> childElts = DomUtils.getChildElements(element);
    for (Element elt : childElts) {
        String localName = parserContext.getDelegate().getLocalName(elt);
        if (POINTCUT.equals(localName)) {
            parsePointcut(elt, parserContext);
        } else if (ADVISOR.equals(localName)) {
            parseAdvisor(elt, parserContext);
        } else if (ASPECT.equals(localName)) {
            parseAspect(elt, parserContext);
        }
    }
    parserContext.popAndRegisterContainingComponent();
    return null;
}
Also used : CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) Element(org.w3c.dom.Element)

Example 5 with CompositeComponentDefinition

use of org.springframework.beans.factory.parsing.CompositeComponentDefinition in project spring-framework by spring-projects.

the class ComponentScanBeanDefinitionParser method registerComponents.

protected void registerComponents(XmlReaderContext readerContext, Set<BeanDefinitionHolder> beanDefinitions, Element element) {
    Object source = readerContext.extractSource(element);
    CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), source);
    for (BeanDefinitionHolder beanDefHolder : beanDefinitions) {
        compositeDef.addNestedComponent(new BeanComponentDefinition(beanDefHolder));
    }
    // Register annotation config processors, if necessary.
    boolean annotationConfig = true;
    if (element.hasAttribute(ANNOTATION_CONFIG_ATTRIBUTE)) {
        annotationConfig = Boolean.valueOf(element.getAttribute(ANNOTATION_CONFIG_ATTRIBUTE));
    }
    if (annotationConfig) {
        Set<BeanDefinitionHolder> processorDefinitions = AnnotationConfigUtils.registerAnnotationConfigProcessors(readerContext.getRegistry(), source);
        for (BeanDefinitionHolder processorDefinition : processorDefinitions) {
            compositeDef.addNestedComponent(new BeanComponentDefinition(processorDefinition));
        }
    }
    readerContext.fireComponentRegistered(compositeDef);
}
Also used : CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition)

Aggregations

CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)24 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)16 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)12 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)10 Element (org.w3c.dom.Element)8 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)7 Test (org.junit.Test)5 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)5 ComponentDefinition (org.springframework.beans.factory.parsing.ComponentDefinition)5 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)4 ManagedList (org.springframework.beans.factory.support.ManagedList)4 BeanReference (org.springframework.beans.factory.config.BeanReference)3 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)2 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1