Search in sources :

Example 11 with CompositeComponentDefinition

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

the class ViewResolversBeanDefinitionParser method parse.

public BeanDefinition parse(Element element, ParserContext context) {
    Object source = context.extractSource(element);
    context.pushContainingComponent(new CompositeComponentDefinition(element.getTagName(), source));
    ManagedList<Object> resolvers = new ManagedList<>(4);
    resolvers.setSource(context.extractSource(element));
    String[] names = new String[] { "jsp", "tiles", "bean-name", "freemarker", "groovy", "script-template", "bean", "ref" };
    for (Element resolverElement : DomUtils.getChildElementsByTagName(element, names)) {
        String name = resolverElement.getLocalName();
        if ("bean".equals(name) || "ref".equals(name)) {
            resolvers.add(context.getDelegate().parsePropertySubElement(resolverElement, null));
            continue;
        }
        RootBeanDefinition resolverBeanDef;
        if ("jsp".equals(name)) {
            resolverBeanDef = new RootBeanDefinition(InternalResourceViewResolver.class);
            resolverBeanDef.getPropertyValues().add("prefix", "/WEB-INF/");
            resolverBeanDef.getPropertyValues().add("suffix", ".jsp");
            addUrlBasedViewResolverProperties(resolverElement, resolverBeanDef);
        } else if ("tiles".equals(name)) {
            resolverBeanDef = new RootBeanDefinition(TilesViewResolver.class);
            addUrlBasedViewResolverProperties(resolverElement, resolverBeanDef);
        } else if ("freemarker".equals(name)) {
            resolverBeanDef = new RootBeanDefinition(FreeMarkerViewResolver.class);
            resolverBeanDef.getPropertyValues().add("suffix", ".ftl");
            addUrlBasedViewResolverProperties(resolverElement, resolverBeanDef);
        } else if ("groovy".equals(name)) {
            resolverBeanDef = new RootBeanDefinition(GroovyMarkupViewResolver.class);
            resolverBeanDef.getPropertyValues().add("suffix", ".tpl");
            addUrlBasedViewResolverProperties(resolverElement, resolverBeanDef);
        } else if ("script-template".equals(name)) {
            resolverBeanDef = new RootBeanDefinition(ScriptTemplateViewResolver.class);
            addUrlBasedViewResolverProperties(resolverElement, resolverBeanDef);
        } else if ("bean-name".equals(name)) {
            resolverBeanDef = new RootBeanDefinition(BeanNameViewResolver.class);
        } else {
            // Should never happen
            throw new IllegalStateException("Unexpected element name: " + name);
        }
        resolverBeanDef.setSource(source);
        resolverBeanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        resolvers.add(resolverBeanDef);
    }
    String beanName = VIEW_RESOLVER_BEAN_NAME;
    RootBeanDefinition compositeResolverBeanDef = new RootBeanDefinition(ViewResolverComposite.class);
    compositeResolverBeanDef.setSource(source);
    compositeResolverBeanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    names = new String[] { "content-negotiation" };
    List<Element> contentnNegotiationElements = DomUtils.getChildElementsByTagName(element, names);
    if (contentnNegotiationElements.isEmpty()) {
        compositeResolverBeanDef.getPropertyValues().add("viewResolvers", resolvers);
    } else if (contentnNegotiationElements.size() == 1) {
        BeanDefinition beanDef = createContentNegotiatingViewResolver(contentnNegotiationElements.get(0), context);
        beanDef.getPropertyValues().add("viewResolvers", resolvers);
        ManagedList<Object> list = new ManagedList<>(1);
        list.add(beanDef);
        compositeResolverBeanDef.getPropertyValues().add("order", Ordered.HIGHEST_PRECEDENCE);
        compositeResolverBeanDef.getPropertyValues().add("viewResolvers", list);
    } else if (contentnNegotiationElements.size() > 1) {
        throw new IllegalArgumentException("Only one <content-negotiation> element is allowed.");
    }
    if (element.hasAttribute("order")) {
        compositeResolverBeanDef.getPropertyValues().add("order", element.getAttribute("order"));
    }
    context.getReaderContext().getRegistry().registerBeanDefinition(beanName, compositeResolverBeanDef);
    context.registerComponent(new BeanComponentDefinition(compositeResolverBeanDef, beanName));
    context.popAndRegisterContainingComponent();
    return null;
}
Also used : CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) Element(org.w3c.dom.Element) ManagedList(org.springframework.beans.factory.support.ManagedList) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ScriptTemplateViewResolver(org.springframework.web.servlet.view.script.ScriptTemplateViewResolver) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) FreeMarkerViewResolver(org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver)

Example 12 with CompositeComponentDefinition

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

the class InterceptorsBeanDefinitionParser method parse.

@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
    CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), parserContext.extractSource(element));
    parserContext.pushContainingComponent(compDefinition);
    RuntimeBeanReference pathMatcherRef = null;
    if (element.hasAttribute("path-matcher")) {
        pathMatcherRef = new RuntimeBeanReference(element.getAttribute("path-matcher"));
    }
    List<Element> interceptors = DomUtils.getChildElementsByTagName(element, "bean", "ref", "interceptor");
    for (Element interceptor : interceptors) {
        RootBeanDefinition mappedInterceptorDef = new RootBeanDefinition(MappedInterceptor.class);
        mappedInterceptorDef.setSource(parserContext.extractSource(interceptor));
        mappedInterceptorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        ManagedList<String> includePatterns = null;
        ManagedList<String> excludePatterns = null;
        Object interceptorBean;
        if ("interceptor".equals(interceptor.getLocalName())) {
            includePatterns = getIncludePatterns(interceptor, "mapping");
            excludePatterns = getIncludePatterns(interceptor, "exclude-mapping");
            Element beanElem = DomUtils.getChildElementsByTagName(interceptor, "bean", "ref").get(0);
            interceptorBean = parserContext.getDelegate().parsePropertySubElement(beanElem, null);
        } else {
            interceptorBean = parserContext.getDelegate().parsePropertySubElement(interceptor, null);
        }
        mappedInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, includePatterns);
        mappedInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(1, excludePatterns);
        mappedInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(2, interceptorBean);
        if (pathMatcherRef != null) {
            mappedInterceptorDef.getPropertyValues().add("pathMatcher", pathMatcherRef);
        }
        String beanName = parserContext.getReaderContext().registerWithGeneratedName(mappedInterceptorDef);
        parserContext.registerComponent(new BeanComponentDefinition(mappedInterceptorDef, beanName));
    }
    parserContext.popAndRegisterContainingComponent();
    return null;
}
Also used : CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) Element(org.w3c.dom.Element) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 13 with CompositeComponentDefinition

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

the class MessageBrokerBeanDefinitionParser method parse.

@Override
public BeanDefinition parse(Element element, ParserContext context) {
    Object source = context.extractSource(element);
    CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
    context.pushContainingComponent(compDefinition);
    Element channelElem = DomUtils.getChildElementByTagName(element, "client-inbound-channel");
    RuntimeBeanReference inChannel = getMessageChannel("clientInboundChannel", channelElem, context, source);
    channelElem = DomUtils.getChildElementByTagName(element, "client-outbound-channel");
    RuntimeBeanReference outChannel = getMessageChannel("clientOutboundChannel", channelElem, context, source);
    channelElem = DomUtils.getChildElementByTagName(element, "broker-channel");
    RuntimeBeanReference brokerChannel = getMessageChannel("brokerChannel", channelElem, context, source);
    RuntimeBeanReference userRegistry = registerUserRegistry(element, context, source);
    Object userDestHandler = registerUserDestHandler(element, userRegistry, inChannel, brokerChannel, context, source);
    RuntimeBeanReference converter = registerMessageConverter(element, context, source);
    RuntimeBeanReference template = registerMessagingTemplate(element, brokerChannel, converter, context, source);
    registerAnnotationMethodMessageHandler(element, inChannel, outChannel, converter, template, context, source);
    RootBeanDefinition broker = registerMessageBroker(element, inChannel, outChannel, brokerChannel, userDestHandler, template, userRegistry, context, source);
    // WebSocket and sub-protocol handling
    ManagedMap<String, Object> urlMap = registerHandlerMapping(element, context, source);
    RuntimeBeanReference stompHandler = registerStompHandler(element, inChannel, outChannel, context, source);
    for (Element endpointElem : DomUtils.getChildElementsByTagName(element, "stomp-endpoint")) {
        RuntimeBeanReference requestHandler = registerRequestHandler(endpointElem, stompHandler, context, source);
        String pathAttribute = endpointElem.getAttribute("path");
        Assert.state(StringUtils.hasText(pathAttribute), "Invalid <stomp-endpoint> (no path mapping)");
        List<String> paths = Arrays.asList(StringUtils.tokenizeToStringArray(pathAttribute, ","));
        for (String path : paths) {
            path = path.trim();
            Assert.state(StringUtils.hasText(path), "Invalid <stomp-endpoint> path attribute: " + pathAttribute);
            if (DomUtils.getChildElementByTagName(endpointElem, "sockjs") != null) {
                path = path.endsWith("/") ? path + "**" : path + "/**";
            }
            urlMap.put(path, requestHandler);
        }
    }
    Map<String, Object> scopeMap = Collections.<String, Object>singletonMap("websocket", new SimpSessionScope());
    RootBeanDefinition scopeConfigurer = new RootBeanDefinition(CustomScopeConfigurer.class);
    scopeConfigurer.getPropertyValues().add("scopes", scopeMap);
    registerBeanDefByName("webSocketScopeConfigurer", scopeConfigurer, context, source);
    registerWebSocketMessageBrokerStats(broker, inChannel, outChannel, context, source);
    context.popAndRegisterContainingComponent();
    return null;
}
Also used : CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) Element(org.w3c.dom.Element) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) SimpSessionScope(org.springframework.messaging.simp.SimpSessionScope) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 14 with CompositeComponentDefinition

use of org.springframework.beans.factory.parsing.CompositeComponentDefinition in project redisson by redisson.

the class RedissonDefinitionParser method parseChildElements.

private void parseChildElements(Element element, String parentId, String redissonRef, BeanDefinitionBuilder redissonDef, ParserContext parserContext) {
    if (element.hasChildNodes()) {
        CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(parentId, parserContext.extractSource(element));
        parserContext.pushContainingComponent(compositeDef);
        List<Element> childElts = DomUtils.getChildElements(element);
        for (Element elt : childElts) {
            if (BeanDefinitionParserDelegate.QUALIFIER_ELEMENT.equals(elt.getLocalName())) {
                //parsed elsewhere
                continue;
            }
            String localName = parserContext.getDelegate().getLocalName(elt);
            localName = Conventions.attributeNameToPropertyName(localName);
            if (ConfigType.contains(localName)) {
                parseConfigTypes(elt, localName, redissonDef, parserContext);
            } else if (AddressType.contains(localName)) {
                parseAddressTypes(elt, localName, redissonDef, parserContext);
            } else if (helper.isRedissonNS(elt)) {
                elt.setAttribute(REDISSON_REF, redissonRef);
                parserContext.getDelegate().parseCustomElement(elt);
            }
        }
        parserContext.popContainingComponent();
    }
}
Also used : CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) Element(org.w3c.dom.Element)

Example 15 with CompositeComponentDefinition

use of org.springframework.beans.factory.parsing.CompositeComponentDefinition in project druid by alibaba.

the class DruidStatBeanDefinitionParser method parse.

@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
    Object source = parserContext.extractSource(element);
    CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
    parserContext.pushContainingComponent(compDefinition);
    BeanDefinitionRegistry registry = parserContext.getRegistry();
    if (registry.containsBeanDefinition(STAT_ANNOTATION_PROCESSOR_BEAN_NAME)) {
        parserContext.getReaderContext().error("Only one DruidStatBeanDefinitionParser may exist within the context.", source);
    } else {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(STAT_ANNOTATION_PROCESSOR_BEAN_CLASS);
        builder.getRawBeanDefinition().setSource(source);
        registerComponent(parserContext, builder, STAT_ANNOTATION_PROCESSOR_BEAN_NAME);
    }
    if (!registry.containsBeanDefinition(STAT_ANNOTATION_ADVICE_BEAN_NAME)) {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(STAT_ANNOTATION_ADVICE_BEAN_CLASS);
        builder.getRawBeanDefinition().setSource(source);
        registerComponent(parserContext, builder, STAT_ANNOTATION_ADVICE_BEAN_NAME);
    }
    parserContext.popAndRegisterContainingComponent();
    return null;
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry)

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