Search in sources :

Example 36 with GenericWebApplicationContext

use of org.springframework.web.context.support.GenericWebApplicationContext in project spring-framework by spring-projects.

the class RequestContextTests method init.

@BeforeEach
public void init() {
    GenericWebApplicationContext applicationContext = new GenericWebApplicationContext();
    applicationContext.refresh();
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);
}
Also used : GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 37 with GenericWebApplicationContext

use of org.springframework.web.context.support.GenericWebApplicationContext in project spring-boot by spring-projects.

the class ClassLoaderFilesResourcePatternResolverTests method customProtocolResolverIsUsedInWebApplication.

@Test
void customProtocolResolverIsUsedInWebApplication() {
    GenericWebApplicationContext context = new GenericWebApplicationContext(new MockServletContext());
    Resource resource = mock(Resource.class);
    ProtocolResolver resolver = mockProtocolResolver("foo:some-file.txt", resource);
    context.addProtocolResolver(resolver);
    this.resolver = new ClassLoaderFilesResourcePatternResolver(context, this.files);
    Resource actual = this.resolver.getResource("foo:some-file.txt");
    assertThat(actual).isSameAs(resource);
    then(resolver).should().resolve(eq("foo:some-file.txt"), any(ResourceLoader.class));
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) DeletedClassLoaderFileResource(org.springframework.boot.devtools.restart.ClassLoaderFilesResourcePatternResolver.DeletedClassLoaderFileResource) ClassPathResource(org.springframework.core.io.ClassPathResource) ServletContextResource(org.springframework.web.context.support.ServletContextResource) Resource(org.springframework.core.io.Resource) ProtocolResolver(org.springframework.core.io.ProtocolResolver) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 38 with GenericWebApplicationContext

use of org.springframework.web.context.support.GenericWebApplicationContext in project spring-framework by spring-projects.

the class AbstractGenericWebContextLoader method loadContext.

// SmartContextLoader
/**
 * Load a Spring {@link WebApplicationContext} from the supplied
 * {@link MergedContextConfiguration}.
 * <p>Implementation details:
 * <ul>
 * <li>Calls {@link #validateMergedContextConfiguration(WebMergedContextConfiguration)}
 * to allow subclasses to validate the supplied configuration before proceeding.</li>
 * <li>Creates a {@link GenericWebApplicationContext} instance.</li>
 * <li>If the supplied {@code MergedContextConfiguration} references a
 * {@linkplain MergedContextConfiguration#getParent() parent configuration},
 * the corresponding {@link MergedContextConfiguration#getParentApplicationContext()
 * ApplicationContext} will be retrieved and
 * {@linkplain GenericWebApplicationContext#setParent(ApplicationContext) set as the parent}
 * for the context created by this method.</li>
 * <li>Delegates to {@link #configureWebResources} to create the
 * {@link MockServletContext} and set it in the {@code WebApplicationContext}.</li>
 * <li>Calls {@link #prepareContext} to allow for customizing the context
 * before bean definitions are loaded.</li>
 * <li>Calls {@link #customizeBeanFactory} to allow for customizing the
 * context's {@code DefaultListableBeanFactory}.</li>
 * <li>Delegates to {@link #loadBeanDefinitions} to populate the context
 * from the locations or classes in the supplied {@code MergedContextConfiguration}.</li>
 * <li>Delegates to {@link AnnotationConfigUtils} for
 * {@linkplain AnnotationConfigUtils#registerAnnotationConfigProcessors registering}
 * annotation configuration processors.</li>
 * <li>Calls {@link #customizeContext} to allow for customizing the context
 * before it is refreshed.</li>
 * <li>{@link ConfigurableApplicationContext#refresh Refreshes} the
 * context and registers a JVM shutdown hook for it.</li>
 * </ul>
 * @return a new web application context
 * @see org.springframework.test.context.SmartContextLoader#loadContext(MergedContextConfiguration)
 * @see GenericWebApplicationContext
 */
@Override
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
    Assert.isTrue(mergedConfig instanceof WebMergedContextConfiguration, () -> String.format("Cannot load WebApplicationContext from non-web merged context configuration %s. " + "Consider annotating your test class with @WebAppConfiguration.", mergedConfig));
    WebMergedContextConfiguration webMergedConfig = (WebMergedContextConfiguration) mergedConfig;
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Loading WebApplicationContext for merged context configuration %s.", webMergedConfig));
    }
    validateMergedContextConfiguration(webMergedConfig);
    GenericWebApplicationContext context = new GenericWebApplicationContext();
    ApplicationContext parent = mergedConfig.getParentApplicationContext();
    if (parent != null) {
        context.setParent(parent);
    }
    configureWebResources(context, webMergedConfig);
    prepareContext(context, webMergedConfig);
    customizeBeanFactory(context.getDefaultListableBeanFactory(), webMergedConfig);
    loadBeanDefinitions(context, webMergedConfig);
    AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
    customizeContext(context, webMergedConfig);
    context.refresh();
    context.registerShutdownHook();
    return context;
}
Also used : WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext)

Example 39 with GenericWebApplicationContext

use of org.springframework.web.context.support.GenericWebApplicationContext in project dubbo by alibaba.

the class SpringStatusChecker method check.

@Override
public Status check() {
    ApplicationContext context = null;
    for (ApplicationContext c : SpringExtensionFactory.getContexts()) {
        // issue : https://github.com/apache/dubbo/issues/3615
        if (c instanceof GenericWebApplicationContext) {
            // ignore GenericXmlApplicationContext
            continue;
        }
        if (c != null) {
            context = c;
            break;
        }
    }
    if (context == null) {
        return new Status(Status.Level.UNKNOWN);
    }
    Status.Level level;
    if (context instanceof Lifecycle) {
        if (((Lifecycle) context).isRunning()) {
            level = Status.Level.OK;
        } else {
            level = Status.Level.ERROR;
        }
    } else {
        level = Status.Level.UNKNOWN;
    }
    StringBuilder buf = new StringBuilder();
    try {
        Class<?> cls = context.getClass();
        Method method = null;
        while (cls != null && method == null) {
            try {
                method = cls.getDeclaredMethod("getConfigLocations", new Class<?>[0]);
            } catch (NoSuchMethodException t) {
                cls = cls.getSuperclass();
            }
        }
        if (method != null) {
            ReflectUtils.makeAccessible(method);
            String[] configs = (String[]) method.invoke(context, new Object[0]);
            if (configs != null && configs.length > 0) {
                for (String config : configs) {
                    if (buf.length() > 0) {
                        buf.append(",");
                    }
                    buf.append(config);
                }
            }
        }
    } catch (Throwable t) {
        logger.warn(t.getMessage(), t);
    }
    return new Status(level, buf.toString());
}
Also used : Status(org.apache.dubbo.common.status.Status) Lifecycle(org.springframework.context.Lifecycle) Method(java.lang.reflect.Method) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext)

Example 40 with GenericWebApplicationContext

use of org.springframework.web.context.support.GenericWebApplicationContext in project dubbo by alibaba.

the class SpringStatusCheckerTest method testGenericWebApplicationContext.

@Test
public void testGenericWebApplicationContext() {
    SpringExtensionFactory.clearContexts();
    GenericWebApplicationContext context = new GenericWebApplicationContext();
    SpringExtensionFactory.addApplicationContext(context);
    SpringStatusChecker checker = new SpringStatusChecker();
    Status status = checker.check();
    Assertions.assertEquals(Status.Level.UNKNOWN, status.getLevel());
}
Also used : Status(org.apache.dubbo.common.status.Status) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) Test(org.junit.jupiter.api.Test)

Aggregations

GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)93 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)33 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)32 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)31 Test (org.junit.Test)30 Test (org.junit.jupiter.api.Test)30 DispatcherServlet (org.springframework.web.servlet.DispatcherServlet)9 MockServletContext (org.springframework.mock.web.MockServletContext)8 BeforeEach (org.junit.jupiter.api.BeforeEach)6 DefaultAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator)6 ApplicationContext (org.springframework.context.ApplicationContext)6 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)6 WebApplicationContext (org.springframework.web.context.WebApplicationContext)6 Method (java.lang.reflect.Method)5 HttpSession (javax.servlet.http.HttpSession)5 SimpleTraceInterceptor (org.springframework.aop.interceptor.SimpleTraceInterceptor)5 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)5 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4