Search in sources :

Example 61 with BeanComponentDefinition

use of org.springframework.beans.factory.parsing.BeanComponentDefinition in project spring-security by spring-projects.

the class AbstractUserDetailsServiceBeanDefinitionParser method parse.

public BeanDefinition parse(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(getBeanClassName(element));
    doParse(element, parserContext, builder);
    RootBeanDefinition userService = (RootBeanDefinition) builder.getBeanDefinition();
    final String beanId = resolveId(element, userService, parserContext);
    parserContext.registerBeanComponent(new BeanComponentDefinition(userService, beanId));
    String cacheRef = element.getAttribute(CACHE_REF);
    // Register a caching version of the user service if there's a cache-ref
    if (StringUtils.hasText(cacheRef)) {
        BeanDefinitionBuilder cachingUSBuilder = BeanDefinitionBuilder.rootBeanDefinition(CachingUserDetailsService.class);
        cachingUSBuilder.addConstructorArgReference(beanId);
        cachingUSBuilder.addPropertyValue("userCache", new RuntimeBeanReference(cacheRef));
        BeanDefinition cachingUserService = cachingUSBuilder.getBeanDefinition();
        parserContext.registerBeanComponent(new BeanComponentDefinition(cachingUserService, beanId + CACHING_SUFFIX));
    }
    return null;
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition)

Example 62 with BeanComponentDefinition

use of org.springframework.beans.factory.parsing.BeanComponentDefinition in project spring-security by spring-projects.

the class AuthenticationManagerBeanDefinitionParser method parse.

public BeanDefinition parse(Element element, ParserContext pc) {
    String id = element.getAttribute("id");
    if (!StringUtils.hasText(id)) {
        if (pc.getRegistry().containsBeanDefinition(BeanIds.AUTHENTICATION_MANAGER)) {
            pc.getReaderContext().warning("Overriding globally registered AuthenticationManager", pc.extractSource(element));
        }
        id = BeanIds.AUTHENTICATION_MANAGER;
    }
    pc.pushContainingComponent(new CompositeComponentDefinition(element.getTagName(), pc.extractSource(element)));
    BeanDefinitionBuilder providerManagerBldr = BeanDefinitionBuilder.rootBeanDefinition(ProviderManager.class);
    String alias = element.getAttribute(ATT_ALIAS);
    List<BeanMetadataElement> providers = new ManagedList<BeanMetadataElement>();
    NamespaceHandlerResolver resolver = pc.getReaderContext().getNamespaceHandlerResolver();
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node instanceof Element) {
            Element providerElt = (Element) node;
            if (StringUtils.hasText(providerElt.getAttribute(ATT_REF))) {
                if (providerElt.getAttributes().getLength() > 1) {
                    pc.getReaderContext().error("authentication-provider element cannot be used with other attributes " + "when using 'ref' attribute", pc.extractSource(element));
                }
                NodeList providerChildren = providerElt.getChildNodes();
                for (int j = 0; j < providerChildren.getLength(); j++) {
                    if (providerChildren.item(j) instanceof Element) {
                        pc.getReaderContext().error("authentication-provider element cannot have child elements when used " + "with 'ref' attribute", pc.extractSource(element));
                    }
                }
                providers.add(new RuntimeBeanReference(providerElt.getAttribute(ATT_REF)));
            } else {
                BeanDefinition provider = resolver.resolve(providerElt.getNamespaceURI()).parse(providerElt, pc);
                Assert.notNull(provider, "Parser for " + providerElt.getNodeName() + " returned a null bean definition");
                String providerId = pc.getReaderContext().generateBeanName(provider);
                pc.registerBeanComponent(new BeanComponentDefinition(provider, providerId));
                providers.add(new RuntimeBeanReference(providerId));
            }
        }
    }
    if (providers.isEmpty()) {
        providers.add(new RootBeanDefinition(NullAuthenticationProvider.class));
    }
    providerManagerBldr.addConstructorArgValue(providers);
    if ("false".equals(element.getAttribute(ATT_ERASE_CREDENTIALS))) {
        providerManagerBldr.addPropertyValue("eraseCredentialsAfterAuthentication", false);
    }
    // Add the default event publisher
    BeanDefinition publisher = new RootBeanDefinition(DefaultAuthenticationEventPublisher.class);
    String pubId = pc.getReaderContext().generateBeanName(publisher);
    pc.registerBeanComponent(new BeanComponentDefinition(publisher, pubId));
    providerManagerBldr.addPropertyReference("authenticationEventPublisher", pubId);
    pc.registerBeanComponent(new BeanComponentDefinition(providerManagerBldr.getBeanDefinition(), id));
    if (StringUtils.hasText(alias)) {
        pc.getRegistry().registerAlias(id, alias);
        pc.getReaderContext().fireAliasRegistered(id, alias, pc.extractSource(element));
    }
    if (!BeanIds.AUTHENTICATION_MANAGER.equals(id)) {
        pc.getRegistry().registerAlias(id, BeanIds.AUTHENTICATION_MANAGER);
        pc.getReaderContext().fireAliasRegistered(id, BeanIds.AUTHENTICATION_MANAGER, pc.extractSource(element));
    }
    pc.popAndRegisterContainingComponent();
    return null;
}
Also used : NamespaceHandlerResolver(org.springframework.beans.factory.xml.NamespaceHandlerResolver) CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) BeanMetadataElement(org.springframework.beans.BeanMetadataElement) 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) BeanMetadataElement(org.springframework.beans.BeanMetadataElement) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 63 with BeanComponentDefinition

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

the class ViewControllerBeanDefinitionParser method registerHandlerMapping.

private BeanDefinition registerHandlerMapping(ParserContext context, Object source) {
    if (context.getRegistry().containsBeanDefinition(HANDLER_MAPPING_BEAN_NAME)) {
        return context.getRegistry().getBeanDefinition(HANDLER_MAPPING_BEAN_NAME);
    }
    RootBeanDefinition beanDef = new RootBeanDefinition(SimpleUrlHandlerMapping.class);
    beanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    context.getRegistry().registerBeanDefinition(HANDLER_MAPPING_BEAN_NAME, beanDef);
    context.registerComponent(new BeanComponentDefinition(beanDef, HANDLER_MAPPING_BEAN_NAME));
    beanDef.setSource(source);
    beanDef.getPropertyValues().add("order", "1");
    beanDef.getPropertyValues().add("pathMatcher", MvcNamespaceUtils.registerPathMatcher(null, context, source));
    beanDef.getPropertyValues().add("urlPathHelper", MvcNamespaceUtils.registerUrlPathHelper(null, context, source));
    RuntimeBeanReference corsConfigurationsRef = MvcNamespaceUtils.registerCorsConfigurations(null, context, source);
    beanDef.getPropertyValues().add("corsConfigurations", corsConfigurationsRef);
    return beanDef;
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 64 with BeanComponentDefinition

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

the class HandlersBeanDefinitionParser 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);
    String orderAttribute = element.getAttribute("order");
    int order = orderAttribute.isEmpty() ? DEFAULT_MAPPING_ORDER : Integer.valueOf(orderAttribute);
    RootBeanDefinition handlerMappingDef = new RootBeanDefinition(WebSocketHandlerMapping.class);
    handlerMappingDef.setSource(source);
    handlerMappingDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    handlerMappingDef.getPropertyValues().add("order", order);
    String handlerMappingName = context.getReaderContext().registerWithGeneratedName(handlerMappingDef);
    RuntimeBeanReference sockJsService = WebSocketNamespaceUtils.registerSockJsService(element, SOCK_JS_SCHEDULER_NAME, context, source);
    HandlerMappingStrategy strategy;
    if (sockJsService != null) {
        strategy = new SockJsHandlerMappingStrategy(sockJsService);
    } else {
        RuntimeBeanReference handshakeHandler = WebSocketNamespaceUtils.registerHandshakeHandler(element, context, source);
        Element interceptorsElement = DomUtils.getChildElementByTagName(element, "handshake-interceptors");
        ManagedList<? super Object> interceptors = WebSocketNamespaceUtils.parseBeanSubElements(interceptorsElement, context);
        String allowedOriginsAttribute = element.getAttribute("allowed-origins");
        List<String> allowedOrigins = Arrays.asList(StringUtils.tokenizeToStringArray(allowedOriginsAttribute, ","));
        interceptors.add(new OriginHandshakeInterceptor(allowedOrigins));
        strategy = new WebSocketHandlerMappingStrategy(handshakeHandler, interceptors);
    }
    ManagedMap<String, Object> urlMap = new ManagedMap<>();
    urlMap.setSource(source);
    for (Element mappingElement : DomUtils.getChildElementsByTagName(element, "mapping")) {
        strategy.addMapping(mappingElement, urlMap, context);
    }
    handlerMappingDef.getPropertyValues().add("urlMap", urlMap);
    context.registerComponent(new BeanComponentDefinition(handlerMappingDef, handlerMappingName));
    context.popAndRegisterContainingComponent();
    return null;
}
Also used : CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) Element(org.w3c.dom.Element) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) ManagedMap(org.springframework.beans.factory.support.ManagedMap)

Example 65 with BeanComponentDefinition

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

the class ScopedProxyBeanDefinitionDecorator method decorate.

@Override
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) {
    boolean proxyTargetClass = true;
    if (node instanceof Element) {
        Element ele = (Element) node;
        if (ele.hasAttribute(PROXY_TARGET_CLASS)) {
            proxyTargetClass = Boolean.valueOf(ele.getAttribute(PROXY_TARGET_CLASS));
        }
    }
    // Register the original bean definition as it will be referenced by the scoped proxy
    // and is relevant for tooling (validation, navigation).
    BeanDefinitionHolder holder = ScopedProxyUtils.createScopedProxy(definition, parserContext.getRegistry(), proxyTargetClass);
    String targetBeanName = ScopedProxyUtils.getTargetBeanName(definition.getBeanName());
    parserContext.getReaderContext().fireComponentRegistered(new BeanComponentDefinition(definition.getBeanDefinition(), targetBeanName));
    return holder;
}
Also used : Element(org.w3c.dom.Element) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition)

Aggregations

BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)82 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)53 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)33 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)28 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)24 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)17 Element (org.w3c.dom.Element)16 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)11 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)11 ManagedList (org.springframework.beans.factory.support.ManagedList)10 Test (org.junit.Test)7 ComponentDefinition (org.springframework.beans.factory.parsing.ComponentDefinition)7 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)6 ManagedMap (org.springframework.beans.factory.support.ManagedMap)4 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)3 BeanComponentDefinitionBuilder (org.springframework.data.config.BeanComponentDefinitionBuilder)3 Method (java.lang.reflect.Method)2 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 PropertyValue (org.springframework.beans.PropertyValue)2 BeanReference (org.springframework.beans.factory.config.BeanReference)2