Search in sources :

Example 1 with Ordered

use of org.springframework.core.Ordered in project spring-boot by spring-projects.

the class DeviceDelegatingViewResolverAutoConfigurationTests method assertLiteDeviceDelegatingViewResolver.

public void assertLiteDeviceDelegatingViewResolver(ViewResolver delegate, String delegatingBeanName) {
    LiteDeviceDelegatingViewResolver deviceDelegatingViewResolver = this.context.getBean(delegatingBeanName, LiteDeviceDelegatingViewResolver.class);
    assertThat(deviceDelegatingViewResolver.getViewResolver()).isSameAs(delegate);
    assertThat(deviceDelegatingViewResolver.getOrder()).isEqualTo(((Ordered) delegate).getOrder() - 1);
}
Also used : LiteDeviceDelegatingViewResolver(org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver) Ordered(org.springframework.core.Ordered)

Example 2 with Ordered

use of org.springframework.core.Ordered in project spring-framework by spring-projects.

the class PostProcessorRegistrationDelegate method invokeBeanFactoryPostProcessors.

public static void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
    // Invoke BeanDefinitionRegistryPostProcessors first, if any.
    Set<String> processedBeans = new HashSet<>();
    if (beanFactory instanceof BeanDefinitionRegistry) {
        BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
        List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList<>();
        List<BeanDefinitionRegistryPostProcessor> registryPostProcessors = new LinkedList<>();
        for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
            if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
                BeanDefinitionRegistryPostProcessor registryPostProcessor = (BeanDefinitionRegistryPostProcessor) postProcessor;
                registryPostProcessor.postProcessBeanDefinitionRegistry(registry);
                registryPostProcessors.add(registryPostProcessor);
            } else {
                regularPostProcessors.add(postProcessor);
            }
        }
        // Do not initialize FactoryBeans here: We need to leave all regular beans
        // uninitialized to let the bean factory post-processors apply to them!
        // Separate between BeanDefinitionRegistryPostProcessors that implement
        // PriorityOrdered, Ordered, and the rest.
        String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
        // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
        List<BeanDefinitionRegistryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
        for (String ppName : postProcessorNames) {
            if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                processedBeans.add(ppName);
            }
        }
        sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
        registryPostProcessors.addAll(priorityOrderedPostProcessors);
        invokeBeanDefinitionRegistryPostProcessors(priorityOrderedPostProcessors, registry);
        // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
        postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
        List<BeanDefinitionRegistryPostProcessor> orderedPostProcessors = new ArrayList<>();
        for (String ppName : postProcessorNames) {
            if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
                orderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                processedBeans.add(ppName);
            }
        }
        sortPostProcessors(beanFactory, orderedPostProcessors);
        registryPostProcessors.addAll(orderedPostProcessors);
        invokeBeanDefinitionRegistryPostProcessors(orderedPostProcessors, registry);
        // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
        boolean reiterate = true;
        while (reiterate) {
            reiterate = false;
            postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
            for (String ppName : postProcessorNames) {
                if (!processedBeans.contains(ppName)) {
                    BeanDefinitionRegistryPostProcessor pp = beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class);
                    registryPostProcessors.add(pp);
                    processedBeans.add(ppName);
                    pp.postProcessBeanDefinitionRegistry(registry);
                    reiterate = true;
                }
            }
        }
        // Now, invoke the postProcessBeanFactory callback of all processors handled so far.
        invokeBeanFactoryPostProcessors(registryPostProcessors, beanFactory);
        invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
    } else {
        // Invoke factory processors registered with the context instance.
        invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
    }
    // Do not initialize FactoryBeans here: We need to leave all regular beans
    // uninitialized to let the bean factory post-processors apply to them!
    String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
    // Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
    // Ordered, and the rest.
    List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
    List<String> orderedPostProcessorNames = new ArrayList<>();
    List<String> nonOrderedPostProcessorNames = new ArrayList<>();
    for (String ppName : postProcessorNames) {
        if (processedBeans.contains(ppName)) {
        // skip - already processed in first phase above
        } else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
            priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
        } else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
            orderedPostProcessorNames.add(ppName);
        } else {
            nonOrderedPostProcessorNames.add(ppName);
        }
    }
    // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
    sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
    invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
    // Next, invoke the BeanFactoryPostProcessors that implement Ordered.
    List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>();
    for (String postProcessorName : orderedPostProcessorNames) {
        orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
    }
    sortPostProcessors(beanFactory, orderedPostProcessors);
    invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
    // Finally, invoke all other BeanFactoryPostProcessors.
    List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
    for (String postProcessorName : nonOrderedPostProcessorNames) {
        nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
    }
    invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
    // Clear cached merged bean definitions since the post-processors might have
    // modified the original metadata, e.g. replacing placeholders in values...
    beanFactory.clearMetadataCache();
}
Also used : ArrayList(java.util.ArrayList) Ordered(org.springframework.core.Ordered) PriorityOrdered(org.springframework.core.PriorityOrdered) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) BeanDefinitionRegistryPostProcessor(org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor) LinkedList(java.util.LinkedList) PriorityOrdered(org.springframework.core.PriorityOrdered) BeanFactoryPostProcessor(org.springframework.beans.factory.config.BeanFactoryPostProcessor) HashSet(java.util.HashSet)

Example 3 with Ordered

use of org.springframework.core.Ordered in project spring-integration by spring-projects.

the class OrderedHandlersTests method verifyOrder.

@Test
public void verifyOrder() {
    for (int i = 1; i < 14; i++) {
        Object consumer = context.getBean("endpoint" + i);
        Object handler = new DirectFieldAccessor(consumer).getPropertyValue("handler");
        assertTrue(handler instanceof Ordered);
        assertEquals(i, ((Ordered) handler).getOrder());
    }
}
Also used : DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Ordered(org.springframework.core.Ordered) Test(org.junit.Test)

Example 4 with Ordered

use of org.springframework.core.Ordered in project spring-framework by spring-projects.

the class DispatcherHandlerTests method handlerMappingOrder.

@Test
void handlerMappingOrder() {
    HandlerMapping hm1 = mock(HandlerMapping.class, withSettings().extraInterfaces(Ordered.class));
    HandlerMapping hm2 = mock(HandlerMapping.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) hm1).getOrder()).willReturn(1);
    given(((Ordered) hm2).getOrder()).willReturn(2);
    given((hm1).getHandler(any())).willReturn(Mono.just((Supplier<String>) () -> "1"));
    given((hm2).getHandler(any())).willReturn(Mono.just((Supplier<String>) () -> "2"));
    StaticApplicationContext context = new StaticApplicationContext();
    context.registerBean("b2", HandlerMapping.class, () -> hm2);
    context.registerBean("b1", HandlerMapping.class, () -> hm1);
    context.registerBean(HandlerAdapter.class, SupplierHandlerAdapter::new);
    context.registerBean(HandlerResultHandler.class, StringHandlerResultHandler::new);
    context.refresh();
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/"));
    new DispatcherHandler(context).handle(exchange).block(Duration.ofSeconds(0));
    assertThat(exchange.getResponse().getBodyAsString().block(Duration.ofSeconds(5))).isEqualTo("1");
}
Also used : StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) Ordered(org.springframework.core.Ordered) Supplier(java.util.function.Supplier) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 5 with Ordered

use of org.springframework.core.Ordered in project spring-boot by spring-projects.

the class ServletWebServerApplicationContextTests method multipleServletBeansWithMainDispatcher.

@Test
void multipleServletBeansWithMainDispatcher() {
    addWebServerFactoryBean();
    Servlet servlet1 = mock(Servlet.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) servlet1).getOrder()).willReturn(1);
    Servlet servlet2 = mock(Servlet.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) servlet2).getOrder()).willReturn(2);
    this.context.registerBeanDefinition("servletBean2", beanDefinition(servlet2));
    this.context.registerBeanDefinition("dispatcherServlet", beanDefinition(servlet1));
    this.context.refresh();
    MockServletWebServerFactory factory = getWebServerFactory();
    ServletContext servletContext = factory.getServletContext();
    InOrder ordered = inOrder(servletContext);
    then(servletContext).should(ordered).addServlet("dispatcherServlet", servlet1);
    then(servletContext).should(ordered).addServlet("servletBean2", servlet2);
    then(factory.getRegisteredServlet(0).getRegistration()).should().addMapping("/");
    then(factory.getRegisteredServlet(1).getRegistration()).should().addMapping("/servletBean2/");
}
Also used : MockServletWebServerFactory(org.springframework.boot.web.servlet.server.MockServletWebServerFactory) InOrder(org.mockito.InOrder) Ordered(org.springframework.core.Ordered) Servlet(jakarta.servlet.Servlet) ServletContext(jakarta.servlet.ServletContext) Test(org.junit.jupiter.api.Test)

Aggregations

Ordered (org.springframework.core.Ordered)18 Test (org.junit.jupiter.api.Test)7 InOrder (org.mockito.InOrder)5 ServletContext (jakarta.servlet.ServletContext)4 Servlet (jakarta.servlet.Servlet)3 ArrayList (java.util.ArrayList)3 MockServletWebServerFactory (org.springframework.boot.web.servlet.server.MockServletWebServerFactory)3 OrderedGatewayFilter (org.springframework.cloud.gateway.filter.OrderedGatewayFilter)2 ApplicationListener (org.springframework.context.ApplicationListener)2 PriorityOrdered (org.springframework.core.PriorityOrdered)2 Filter (jakarta.servlet.Filter)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Set (java.util.Set)1 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)1 Supplier (java.util.function.Supplier)1 CodecFactory (org.grails.encoder.CodecFactory)1 StreamingEncoder (org.grails.encoder.StreamingEncoder)1