Search in sources :

Example 1 with CompositeComponentDefinition

use of cn.taketoday.beans.factory.parsing.CompositeComponentDefinition in project today-infrastructure by TAKETODAY.

the class AnnotationDrivenBeanDefinitionParser method parse.

@Override
@Nullable
public BeanDefinition parse(Element element, ParserContext parserContext) {
    Object source = parserContext.extractSource(element);
    // Register component for the surrounding <task:annotation-driven> element.
    CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
    parserContext.pushContainingComponent(compDefinition);
    // Nest the concrete post-processor bean in the surrounding component.
    BeanDefinitionRegistry registry = parserContext.getRegistry();
    String mode = element.getAttribute("mode");
    if ("aspectj".equals(mode)) {
        // mode="aspectj"
        registerAsyncExecutionAspect(element, parserContext);
    } else {
        // mode="proxy"
        if (registry.containsBeanDefinition(TaskManagementConfigUtils.ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME)) {
            parserContext.getReaderContext().error("Only one AsyncAnnotationBeanPostProcessor may exist within the context.", source);
        } else {
            BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition("cn.taketoday.scheduling.annotation.AsyncAnnotationBeanPostProcessor");
            builder.getRawBeanDefinition().setSource(source);
            String executor = element.getAttribute("executor");
            if (StringUtils.hasText(executor)) {
                builder.addPropertyReference("executor", executor);
            }
            String exceptionHandler = element.getAttribute("exception-handler");
            if (StringUtils.hasText(exceptionHandler)) {
                builder.addPropertyReference("exceptionHandler", exceptionHandler);
            }
            if (Boolean.parseBoolean(element.getAttribute(AopNamespaceUtils.PROXY_TARGET_CLASS_ATTRIBUTE))) {
                builder.addPropertyValue("proxyTargetClass", true);
            }
            registerPostProcessor(parserContext, builder, TaskManagementConfigUtils.ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME);
        }
    }
    if (registry.containsBeanDefinition(TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
        parserContext.getReaderContext().error("Only one ScheduledAnnotationBeanPostProcessor may exist within the context.", source);
    } else {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition("cn.taketoday.scheduling.annotation.ScheduledAnnotationBeanPostProcessor");
        builder.getRawBeanDefinition().setSource(source);
        String scheduler = element.getAttribute("scheduler");
        if (StringUtils.hasText(scheduler)) {
            builder.addPropertyReference("scheduler", scheduler);
        }
        registerPostProcessor(parserContext, builder, TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME);
    }
    // Finally register the composite component.
    parserContext.popAndRegisterContainingComponent();
    return null;
}
Also used : BeanDefinitionBuilder(cn.taketoday.beans.factory.support.BeanDefinitionBuilder) CompositeComponentDefinition(cn.taketoday.beans.factory.parsing.CompositeComponentDefinition) BeanDefinitionRegistry(cn.taketoday.beans.factory.support.BeanDefinitionRegistry) Nullable(cn.taketoday.lang.Nullable)

Example 2 with CompositeComponentDefinition

use of cn.taketoday.beans.factory.parsing.CompositeComponentDefinition in project today-infrastructure by TAKETODAY.

the class AopNamespaceHandlerEventTests method pointcutEvents.

@Test
void pointcutEvents() {
    this.reader.loadBeanDefinitions(POINTCUT_EVENTS_CONTEXT);
    ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions();
    assertThat(componentDefinitions).as("Incorrect number of events fired").hasSize(1);
    assertThat(componentDefinitions[0]).as("No holder with nested components").isInstanceOf(CompositeComponentDefinition.class);
    CompositeComponentDefinition compositeDef = (CompositeComponentDefinition) componentDefinitions[0];
    assertThat(compositeDef.getName()).isEqualTo("aop:config");
    ComponentDefinition[] nestedComponentDefs = compositeDef.getNestedComponents();
    assertThat(nestedComponentDefs).as("Incorrect number of inner components").hasSize(2);
    PointcutComponentDefinition pcd = null;
    for (ComponentDefinition componentDefinition : nestedComponentDefs) {
        if (componentDefinition instanceof PointcutComponentDefinition) {
            pcd = (PointcutComponentDefinition) componentDefinition;
            break;
        }
    }
    assertThat(pcd).as("PointcutComponentDefinition not found").isNotNull();
    assertThat(pcd.getBeanDefinitions()).as("Incorrect number of BeanDefinitions").hasSize(1);
}
Also used : CompositeComponentDefinition(cn.taketoday.beans.factory.parsing.CompositeComponentDefinition) CompositeComponentDefinition(cn.taketoday.beans.factory.parsing.CompositeComponentDefinition) ComponentDefinition(cn.taketoday.beans.factory.parsing.ComponentDefinition) BeanComponentDefinition(cn.taketoday.beans.factory.parsing.BeanComponentDefinition) Test(org.junit.jupiter.api.Test)

Example 3 with CompositeComponentDefinition

use of cn.taketoday.beans.factory.parsing.CompositeComponentDefinition in project today-framework by TAKETODAY.

the class AopNamespaceHandlerEventTests method advisorEventsWithDirectPointcut.

@Test
void advisorEventsWithDirectPointcut() {
    this.reader.loadBeanDefinitions(DIRECT_POINTCUT_EVENTS_CONTEXT);
    ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions();
    assertThat(componentDefinitions).as("Incorrect number of events fired").hasSize(2);
    assertThat(componentDefinitions[0]).as("No holder with nested components").isInstanceOf(CompositeComponentDefinition.class);
    CompositeComponentDefinition compositeDef = (CompositeComponentDefinition) componentDefinitions[0];
    assertThat(compositeDef.getName()).isEqualTo("aop:config");
    ComponentDefinition[] nestedComponentDefs = compositeDef.getNestedComponents();
    assertThat(nestedComponentDefs).as("Incorrect number of inner components").hasSize(2);
    AdvisorComponentDefinition acd = null;
    for (ComponentDefinition componentDefinition : nestedComponentDefs) {
        if (componentDefinition instanceof AdvisorComponentDefinition) {
            acd = (AdvisorComponentDefinition) componentDefinition;
            break;
        }
    }
    assertThat(acd).as("AdvisorComponentDefinition not found").isNotNull();
    assertThat(acd.getBeanDefinitions()).hasSize(2);
    assertThat(acd.getBeanReferences()).hasSize(1);
    assertThat(componentDefinitions[1]).as("No advice bean found").isInstanceOf(BeanComponentDefinition.class);
    BeanComponentDefinition adviceDef = (BeanComponentDefinition) componentDefinitions[1];
    assertThat(adviceDef.getBeanName()).isEqualTo("countingAdvice");
}
Also used : CompositeComponentDefinition(cn.taketoday.beans.factory.parsing.CompositeComponentDefinition) BeanComponentDefinition(cn.taketoday.beans.factory.parsing.BeanComponentDefinition) CompositeComponentDefinition(cn.taketoday.beans.factory.parsing.CompositeComponentDefinition) ComponentDefinition(cn.taketoday.beans.factory.parsing.ComponentDefinition) BeanComponentDefinition(cn.taketoday.beans.factory.parsing.BeanComponentDefinition) Test(org.junit.jupiter.api.Test)

Example 4 with CompositeComponentDefinition

use of cn.taketoday.beans.factory.parsing.CompositeComponentDefinition in project today-framework by TAKETODAY.

the class AopNamespaceHandlerEventTests method pointcutEvents.

@Test
void pointcutEvents() {
    this.reader.loadBeanDefinitions(POINTCUT_EVENTS_CONTEXT);
    ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions();
    assertThat(componentDefinitions).as("Incorrect number of events fired").hasSize(1);
    assertThat(componentDefinitions[0]).as("No holder with nested components").isInstanceOf(CompositeComponentDefinition.class);
    CompositeComponentDefinition compositeDef = (CompositeComponentDefinition) componentDefinitions[0];
    assertThat(compositeDef.getName()).isEqualTo("aop:config");
    ComponentDefinition[] nestedComponentDefs = compositeDef.getNestedComponents();
    assertThat(nestedComponentDefs).as("Incorrect number of inner components").hasSize(2);
    PointcutComponentDefinition pcd = null;
    for (ComponentDefinition componentDefinition : nestedComponentDefs) {
        if (componentDefinition instanceof PointcutComponentDefinition) {
            pcd = (PointcutComponentDefinition) componentDefinition;
            break;
        }
    }
    assertThat(pcd).as("PointcutComponentDefinition not found").isNotNull();
    assertThat(pcd.getBeanDefinitions()).as("Incorrect number of BeanDefinitions").hasSize(1);
}
Also used : CompositeComponentDefinition(cn.taketoday.beans.factory.parsing.CompositeComponentDefinition) CompositeComponentDefinition(cn.taketoday.beans.factory.parsing.CompositeComponentDefinition) ComponentDefinition(cn.taketoday.beans.factory.parsing.ComponentDefinition) BeanComponentDefinition(cn.taketoday.beans.factory.parsing.BeanComponentDefinition) Test(org.junit.jupiter.api.Test)

Example 5 with CompositeComponentDefinition

use of cn.taketoday.beans.factory.parsing.CompositeComponentDefinition in project today-framework by TAKETODAY.

the class AnnotationConfigBeanDefinitionParser method parse.

@Override
@Nullable
public BeanDefinition parse(Element element, ParserContext parserContext) {
    Object source = parserContext.extractSource(element);
    // Obtain bean definitions for all relevant BeanPostProcessors.
    AnnotationConfigUtils.registerAnnotationConfigProcessors(parserContext.getRegistry(), holder -> {
        holder.setSource(source);
        // Nest the concrete beans in the surrounding component.
        parserContext.registerComponent(new BeanComponentDefinition(holder));
    });
    // Register component for the surrounding <context:annotation-config> element.
    CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
    parserContext.pushContainingComponent(compDefinition);
    // Finally register the composite component.
    parserContext.popAndRegisterContainingComponent();
    return null;
}
Also used : CompositeComponentDefinition(cn.taketoday.beans.factory.parsing.CompositeComponentDefinition) BeanComponentDefinition(cn.taketoday.beans.factory.parsing.BeanComponentDefinition) Nullable(cn.taketoday.lang.Nullable)

Aggregations

CompositeComponentDefinition (cn.taketoday.beans.factory.parsing.CompositeComponentDefinition)12 BeanComponentDefinition (cn.taketoday.beans.factory.parsing.BeanComponentDefinition)10 ComponentDefinition (cn.taketoday.beans.factory.parsing.ComponentDefinition)8 Test (org.junit.jupiter.api.Test)8 Nullable (cn.taketoday.lang.Nullable)4 BeanDefinition (cn.taketoday.beans.factory.config.BeanDefinition)2 BeanReference (cn.taketoday.beans.factory.config.BeanReference)2 BeanDefinitionBuilder (cn.taketoday.beans.factory.support.BeanDefinitionBuilder)2 BeanDefinitionRegistry (cn.taketoday.beans.factory.support.BeanDefinitionRegistry)2 HashSet (java.util.HashSet)2