Search in sources :

Example 1 with ServletContextInitializer

use of org.springframework.boot.web.servlet.ServletContextInitializer in project spring-boot by spring-projects.

the class AbstractServletWebServerFactoryTests method rootServletContextResource.

@Test
public void rootServletContextResource() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    final AtomicReference<URL> rootResource = new AtomicReference<>();
    this.webServer = factory.getWebServer(new ServletContextInitializer() {

        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            try {
                rootResource.set(servletContext.getResource("/"));
            } catch (MalformedURLException ex) {
                throw new ServletException(ex);
            }
        }
    });
    this.webServer.start();
    assertThat(rootResource.get()).isNotNull();
}
Also used : ServletException(javax.servlet.ServletException) MalformedURLException(java.net.MalformedURLException) ServletContext(javax.servlet.ServletContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) URL(java.net.URL) ServletContextInitializer(org.springframework.boot.web.servlet.ServletContextInitializer) Test(org.junit.Test)

Example 2 with ServletContextInitializer

use of org.springframework.boot.web.servlet.ServletContextInitializer in project spring-boot by spring-projects.

the class TomcatServletWebServerFactory method prepareContext.

protected void prepareContext(Host host, ServletContextInitializer[] initializers) {
    File docBase = getValidDocumentRoot();
    docBase = (docBase != null ? docBase : createTempDir("tomcat-docbase"));
    final TomcatEmbeddedContext context = new TomcatEmbeddedContext();
    context.setName(getContextPath());
    context.setDisplayName(getDisplayName());
    context.setPath(getContextPath());
    context.setDocBase(docBase.getAbsolutePath());
    context.addLifecycleListener(new FixContextListener());
    context.setParentClassLoader(this.resourceLoader != null ? this.resourceLoader.getClassLoader() : ClassUtils.getDefaultClassLoader());
    resetDefaultLocaleMapping(context);
    addLocaleMappings(context);
    try {
        context.setUseRelativeRedirects(false);
    } catch (NoSuchMethodError ex) {
    // Tomcat is < 8.0.30. Continue
    }
    SkipPatternJarScanner.apply(context, this.tldSkipPatterns);
    WebappLoader loader = new WebappLoader(context.getParentClassLoader());
    loader.setLoaderClass(TomcatEmbeddedWebappClassLoader.class.getName());
    loader.setDelegate(true);
    context.setLoader(loader);
    if (isRegisterDefaultServlet()) {
        addDefaultServlet(context);
    }
    if (shouldRegisterJspServlet()) {
        addJspServlet(context);
        addJasperInitializer(context);
    }
    context.addLifecycleListener(new StaticResourceConfigurer(context));
    ServletContextInitializer[] initializersToUse = mergeInitializers(initializers);
    configureContext(context, initializersToUse);
    host.addChild(context);
    postProcessContext(context);
}
Also used : WebappLoader(org.apache.catalina.loader.WebappLoader) File(java.io.File) FixContextListener(org.apache.catalina.startup.Tomcat.FixContextListener) ServletContextInitializer(org.springframework.boot.web.servlet.ServletContextInitializer)

Example 3 with ServletContextInitializer

use of org.springframework.boot.web.servlet.ServletContextInitializer in project spring-boot by spring-projects.

the class ServletWebServerApplicationContextTests method unorderedServletContextInitializerBeans.

@Test
public void unorderedServletContextInitializerBeans() throws Exception {
    addWebServerFactoryBean();
    ServletContextInitializer initializer1 = mock(ServletContextInitializer.class);
    ServletContextInitializer initializer2 = mock(ServletContextInitializer.class);
    this.context.registerBeanDefinition("initializerBean2", beanDefinition(initializer2));
    this.context.registerBeanDefinition("initializerBean1", beanDefinition(initializer1));
    this.context.refresh();
    ServletContext servletContext = getWebServerFactory().getServletContext();
    verify(initializer1).onStartup(servletContext);
    verify(initializer2).onStartup(servletContext);
}
Also used : ServletContext(javax.servlet.ServletContext) ServletContextInitializer(org.springframework.boot.web.servlet.ServletContextInitializer) Test(org.junit.Test)

Example 4 with ServletContextInitializer

use of org.springframework.boot.web.servlet.ServletContextInitializer in project spring-boot by spring-projects.

the class UndertowServletWebServerFactory method registerServletContainerInitializerToDriveServletContextInitializers.

private void registerServletContainerInitializerToDriveServletContextInitializers(DeploymentInfo deployment, ServletContextInitializer... initializers) {
    ServletContextInitializer[] mergedInitializers = mergeInitializers(initializers);
    Initializer initializer = new Initializer(mergedInitializers);
    deployment.addServletContainerInitalizer(new ServletContainerInitializerInfo(Initializer.class, new ImmediateInstanceFactory<ServletContainerInitializer>(initializer), NO_CLASSES));
}
Also used : ServletContainerInitializerInfo(io.undertow.servlet.api.ServletContainerInitializerInfo) ServletContextInitializer(org.springframework.boot.web.servlet.ServletContextInitializer) ServletContainerInitializer(javax.servlet.ServletContainerInitializer) ImmediateInstanceFactory(io.undertow.servlet.util.ImmediateInstanceFactory) ServletContextInitializer(org.springframework.boot.web.servlet.ServletContextInitializer)

Example 5 with ServletContextInitializer

use of org.springframework.boot.web.servlet.ServletContextInitializer in project spring-boot by spring-projects.

the class ServletWebServerApplicationContext method selfInitialize.

private void selfInitialize(ServletContext servletContext) throws ServletException {
    prepareWebApplicationContext(servletContext);
    ConfigurableListableBeanFactory beanFactory = getBeanFactory();
    ExistingWebApplicationScopes existingScopes = new ExistingWebApplicationScopes(beanFactory);
    WebApplicationContextUtils.registerWebApplicationScopes(beanFactory, getServletContext());
    existingScopes.restore();
    WebApplicationContextUtils.registerEnvironmentBeans(beanFactory, getServletContext());
    for (ServletContextInitializer beans : getServletContextInitializerBeans()) {
        beans.onStartup(servletContext);
    }
}
Also used : ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) ServletContextInitializer(org.springframework.boot.web.servlet.ServletContextInitializer)

Aggregations

ServletContextInitializer (org.springframework.boot.web.servlet.ServletContextInitializer)9 Test (org.junit.Test)6 ServletContext (javax.servlet.ServletContext)5 ServletException (javax.servlet.ServletException)2 InOrder (org.mockito.InOrder)2 ServletContainerInitializerInfo (io.undertow.servlet.api.ServletContainerInitializerInfo)1 ImmediateInstanceFactory (io.undertow.servlet.util.ImmediateInstanceFactory)1 File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Date (java.util.Date)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Filter (javax.servlet.Filter)1 Servlet (javax.servlet.Servlet)1 ServletContainerInitializer (javax.servlet.ServletContainerInitializer)1 WebappLoader (org.apache.catalina.loader.WebappLoader)1 FixContextListener (org.apache.catalina.startup.Tomcat.FixContextListener)1 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)1 Ordered (org.springframework.core.Ordered)1