Search in sources :

Example 1 with BeanReference

use of org.springframework.beans.factory.config.BeanReference 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 2 with BeanReference

use of org.springframework.beans.factory.config.BeanReference in project spring-framework by spring-projects.

the class AnnotationDrivenBeanDefinitionParser method extractBeanRefSubElements.

private ManagedList<BeanReference> extractBeanRefSubElements(Element parentElement, ParserContext parserContext) {
    ManagedList<BeanReference> list = new ManagedList<>();
    list.setSource(parserContext.extractSource(parentElement));
    for (Element refElement : DomUtils.getChildElementsByTagName(parentElement, "ref")) {
        BeanReference reference;
        if (StringUtils.hasText("bean")) {
            reference = new RuntimeBeanReference(refElement.getAttribute("bean"), false);
            list.add(reference);
        } else if (StringUtils.hasText("parent")) {
            reference = new RuntimeBeanReference(refElement.getAttribute("parent"), true);
            list.add(reference);
        } else {
            parserContext.getReaderContext().error("'bean' or 'parent' attribute is required for <ref> element", parserContext.extractSource(parentElement));
        }
    }
    return list;
}
Also used : Element(org.w3c.dom.Element) ManagedList(org.springframework.beans.factory.support.ManagedList) BeanReference(org.springframework.beans.factory.config.BeanReference) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 3 with BeanReference

use of org.springframework.beans.factory.config.BeanReference in project spring-security-oauth by spring-projects.

the class ConfigUtils method findDefaultFilterChainBeanId.

@SuppressWarnings({ "unchecked" })
protected static String findDefaultFilterChainBeanId(ParserContext parserContext) {
    BeanDefinition filterChainList = parserContext.getRegistry().getBeanDefinition(BeanIds.FILTER_CHAINS);
    // Get the list of SecurityFilterChain beans
    List<BeanReference> filterChains = (List<BeanReference>) filterChainList.getPropertyValues().getPropertyValue("sourceList").getValue();
    return filterChains.get(filterChains.size() - 1).getBeanName();
}
Also used : List(java.util.List) BeanReference(org.springframework.beans.factory.config.BeanReference) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 4 with BeanReference

use of org.springframework.beans.factory.config.BeanReference in project spring-security by spring-projects.

the class ClearCredentialsMethodInvokingFactoryBean method parse.

/**
	 * The aim of this method is to build the list of filters which have been defined by
	 * the namespace elements and attributes within the &lt;http&gt; configuration, along
	 * with any custom-filter's linked to user-defined filter beans.
	 * <p>
	 * By the end of this method, the default <tt>FilterChainProxy</tt> bean should have
	 * been registered and will have the map of filter chains defined, with the
	 * "universal" match pattern mapped to the list of beans which have been parsed here.
	 */
@SuppressWarnings({ "unchecked" })
public BeanDefinition parse(Element element, ParserContext pc) {
    CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), pc.extractSource(element));
    pc.pushContainingComponent(compositeDef);
    registerFilterChainProxyIfNecessary(pc, pc.extractSource(element));
    // Obtain the filter chains and add the new chain to it
    BeanDefinition listFactoryBean = pc.getRegistry().getBeanDefinition(BeanIds.FILTER_CHAINS);
    List<BeanReference> filterChains = (List<BeanReference>) listFactoryBean.getPropertyValues().getPropertyValue("sourceList").getValue();
    filterChains.add(createFilterChain(element, pc));
    pc.popAndRegisterContainingComponent();
    return null;
}
Also used : CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) ManagedList(org.springframework.beans.factory.support.ManagedList) BeanReference(org.springframework.beans.factory.config.BeanReference) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 5 with BeanReference

use of org.springframework.beans.factory.config.BeanReference in project spring-security by spring-projects.

the class ClearCredentialsMethodInvokingFactoryBean method createFilterChain.

/**
	 * Creates the {@code SecurityFilterChain} bean from an &lt;http&gt; element.
	 */
private BeanReference createFilterChain(Element element, ParserContext pc) {
    boolean secured = !OPT_SECURITY_NONE.equals(element.getAttribute(ATT_SECURED));
    if (!secured) {
        if (!StringUtils.hasText(element.getAttribute(ATT_PATH_PATTERN)) && !StringUtils.hasText(ATT_REQUEST_MATCHER_REF)) {
            pc.getReaderContext().error("The '" + ATT_SECURED + "' attribute must be used in combination with" + " the '" + ATT_PATH_PATTERN + "' or '" + ATT_REQUEST_MATCHER_REF + "' attributes.", pc.extractSource(element));
        }
        for (int n = 0; n < element.getChildNodes().getLength(); n++) {
            if (element.getChildNodes().item(n) instanceof Element) {
                pc.getReaderContext().error("If you are using <http> to define an unsecured pattern, " + "it cannot contain child elements.", pc.extractSource(element));
            }
        }
        return createSecurityFilterChainBean(element, pc, Collections.emptyList());
    }
    final BeanReference portMapper = createPortMapper(element, pc);
    final BeanReference portResolver = createPortResolver(portMapper, pc);
    ManagedList<BeanReference> authenticationProviders = new ManagedList<BeanReference>();
    BeanReference authenticationManager = createAuthenticationManager(element, pc, authenticationProviders);
    boolean forceAutoConfig = isDefaultHttpConfig(element);
    HttpConfigurationBuilder httpBldr = new HttpConfigurationBuilder(element, forceAutoConfig, pc, portMapper, portResolver, authenticationManager);
    AuthenticationConfigBuilder authBldr = new AuthenticationConfigBuilder(element, forceAutoConfig, pc, httpBldr.getSessionCreationPolicy(), httpBldr.getRequestCache(), authenticationManager, httpBldr.getSessionStrategy(), portMapper, portResolver, httpBldr.getCsrfLogoutHandler());
    httpBldr.setLogoutHandlers(authBldr.getLogoutHandlers());
    httpBldr.setEntryPoint(authBldr.getEntryPointBean());
    httpBldr.setAccessDeniedHandler(authBldr.getAccessDeniedHandlerBean());
    authenticationProviders.addAll(authBldr.getProviders());
    List<OrderDecorator> unorderedFilterChain = new ArrayList<OrderDecorator>();
    unorderedFilterChain.addAll(httpBldr.getFilters());
    unorderedFilterChain.addAll(authBldr.getFilters());
    unorderedFilterChain.addAll(buildCustomFilterList(element, pc));
    Collections.sort(unorderedFilterChain, new OrderComparator());
    checkFilterChainOrder(unorderedFilterChain, pc, pc.extractSource(element));
    // The list of filter beans
    List<BeanMetadataElement> filterChain = new ManagedList<BeanMetadataElement>();
    for (OrderDecorator od : unorderedFilterChain) {
        filterChain.add(od.bean);
    }
    return createSecurityFilterChainBean(element, pc, filterChain);
}
Also used : BeanMetadataElement(org.springframework.beans.BeanMetadataElement) Element(org.w3c.dom.Element) ManagedList(org.springframework.beans.factory.support.ManagedList) OrderComparator(org.springframework.core.OrderComparator) BeanMetadataElement(org.springframework.beans.BeanMetadataElement) BeanReference(org.springframework.beans.factory.config.BeanReference) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Aggregations

BeanReference (org.springframework.beans.factory.config.BeanReference)10 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)7 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)5 ManagedList (org.springframework.beans.factory.support.ManagedList)4 Element (org.w3c.dom.Element)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)3 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)3 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)2 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)2 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1 PropertyValue (org.springframework.beans.PropertyValue)1 PropertyValues (org.springframework.beans.PropertyValues)1 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)1 ComponentDefinition (org.springframework.beans.factory.parsing.ComponentDefinition)1 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)1 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)1