@Test public void testContextLoaderListenerWithProgrammaticAndGlobalInitializers() { MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml"); sc.addInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM, TestWebContextInitializer.class.getName()); ContextLoaderListener listener = new ContextLoaderListener(); listener.setContextInitializers(new TestContextInitializer()); listener.contextInitialized(new ServletContextEvent(sc)); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc); TestBean testBean = wac.getBean(TestBean.class); assertThat(testBean.getName(), equalTo("testName")); assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue()); }
Object attr = ctx.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); if (attr != null) { logger.warn("Exception {}", e); } finally { super.contextDestroyed(sce); if (ctx != null) { ctx.removeAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
/** * Register a {@link ContextLoaderListener} against the given servlet context. The * {@code ContextLoaderListener} is initialized with the application context returned * from the {@link #createRootApplicationContext()} template method. * @param servletContext the servlet context to register the listener against */ protected void registerContextLoaderListener(ServletContext servletContext) { WebApplicationContext rootAppContext = createRootApplicationContext(); if (rootAppContext != null) { ContextLoaderListener listener = new ContextLoaderListener(rootAppContext); listener.setContextInitializers(getRootApplicationContextInitializers()); servletContext.addListener(listener); } else { logger.debug("No ContextLoaderListener registered, as " + "createRootApplicationContext() did not return an application context"); } }
/** * Ensure that ContextLoaderListener and AnnotationConfigApplicationContext interact nicely. */ @Test public void annotationConfigWAC() { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.scan("does.not.matter"); ContextLoaderListener cll = new ContextLoaderListener(ctx); cll.contextInitialized(new ServletContextEvent(new MockServletContext())); } }
String prefix = servletContext.getRealPath("/"); super.contextInitialized(sce); applicationContext = (ConfigurableWebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); logger.debug("Root context path: {}", applicationContext.getServletContext().getContextPath());
@Test public void testContextLoaderListenerWithMixedContextInitializers() { MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml"); sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, TestContextInitializer.class.getName()); sc.addInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM, TestWebContextInitializer.class.getName()); ContextLoaderListener listener = new ContextLoaderListener(); listener.contextInitialized(new ServletContextEvent(sc)); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc); TestBean testBean = wac.getBean(TestBean.class); assertThat(testBean.getName(), equalTo("testName")); assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue()); }
@Override public void contextInitialized(ServletContextEvent event) { try { cloudStackContext = new CloudStackSpringContext(); cloudStackContext.registerShutdownHook(); event.getServletContext().setAttribute(CloudStackSpringContext.CLOUDSTACK_CONTEXT_SERVLET_KEY, cloudStackContext); } catch (IOException e) { log.error("Failed to start CloudStack", e); throw new RuntimeException("Failed to initialize CloudStack Spring modules", e); } configuredParentName = event.getServletContext().getInitParameter(WEB_PARENT_MODULE); if (configuredParentName == null) { configuredParentName = WEB_PARENT_MODULE_DEFAULT; } super.contextInitialized(event); }
public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext(); appContext.setDisplayName(getDisplayName()); appContext.register(clz); appContext.setServletContext(servletContext); servletContext.addListener(new ContextLoaderListener(appContext)); Dynamic servlet = servletContext.addServlet(DISPATCHER, new DispatcherServlet(appContext)); servlet.addMapping("/"); servlet.setLoadOnStartup(1); FilterRegistration.Dynamic openSQLSessionInViewFilterChain = servletContext.addFilter(OPEN_SQL_SESSION_IN_VIEW_FILTER, OpenSQLSessionInViewFilter.class); openSQLSessionInViewFilterChain.addMappingForUrlPatterns(null, false, "/*");
public final void onStartup(final ServletContext servletContext) throws ServletException { final AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); servletContext.addListener(new ContextLoaderListener(context)); propertySources(servletContext.getContextPath(), context); MutablePropertySources propertySources = propertyConfig.getLeft(); Set<String> namespace = Sets.newHashSet(namespace(context.getEnvironment())); ServletRegistration.Dynamic dispatcher = servletContext.addServlet( "spring-dispatcher", new DispatcherServlet(context)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping(dispatcherMapping());
@Override public void contextInitialized(ServletContextEvent event) { super.contextInitialized(event); // expose the application's properties to the servlet (and consequently to the JSP layer) WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext()); OverridablePropertyPlaceholderConfigurer configurer = (OverridablePropertyPlaceholderConfigurer) context.getBean(PORTAL_PROPERTY_PLACEHOLDER_BEAN_NAME); event.getServletContext().setAttribute(APPLICATION_PROPERTIES_KEY, configurer.getResolvedProps()); } }
@Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setServletContext(servletContext); registerConfiguration(context); servletContext.addListener(new ContextLoaderListener(context)); context.refresh(); Environment env = context.getBean(Environment.class); Dynamic registration = servletContext.addServlet( ClassUtils.getShortNameAsProperty(SpringServlet.class), new SpringServlet(context)); String mapping = env .getProperty(RootMappedCondition.URL_MAPPING_PROPERTY, "/*"); Map<String, String> initParameters = new HashMap<>(); if (RootMappedCondition.isRootMapping(mapping)) { Dynamic dispatcherRegistration = servletContext .addServlet("dispatcher", new DispatcherServlet(context)); dispatcherRegistration.addMapping("/*"); mapping = VaadinServletConfiguration.VAADIN_SERVLET_MAPPING; initParameters.put(Constants.SERVLET_PARAMETER_PUSH_URL, mapping.replace("*", "")); } registration.setInitParameters(initParameters); registration.addMapping(mapping); registration.setAsyncSupported( Boolean.TRUE.toString().equals(env.getProperty( "vaadin.asyncSupported", Boolean.TRUE.toString()))); }
new AnnotationConfigWebApplicationContext()) { rootContext.register(ProbeConfig.class); servletContext.setInitParameter("attribute.value.roles", "ROLE_MANAGER,ROLE_MANAGER-GUI"); servletContext.addListener(new ContextLoaderListener(rootContext)); ServletRegistration.Dynamic probe = servletContext.addServlet("probe", ProbeServlet.class); probe.setInitParameters(initParameters); probe.setLoadOnStartup(0); probe.addMapping("*.htm"); probe.addMapping("*.ajax"); probe.addMapping("/logs/*"); servletContext.addFilter("sitemesh", SiteMeshFilter.class); sitemesh.addMappingForUrlPatterns( EnumSet.of(DispatcherType.FORWARD, DispatcherType.REQUEST, DispatcherType.ERROR), false, "/*"); servletContext.addFilter("filterChainProxy", DelegatingFilterProxy.class); security.addMappingForUrlPatterns( EnumSet.of(DispatcherType.FORWARD, DispatcherType.REQUEST, DispatcherType.ERROR), false, "/*");
@Override public void contextInitialized(ServletContextEvent event) { boolean scanProviders = false; boolean scanResources = false; String sProviders = event.getServletContext().getInitParameter("resteasy.scan.providers"); if (sProviders != null) { scanProviders = Boolean.valueOf(sProviders.trim()); } String scanAll = event.getServletContext().getInitParameter("resteasy.scan"); if (scanAll != null) { boolean tmp = Boolean.valueOf(scanAll.trim()); scanProviders = tmp || scanProviders; scanResources = tmp; } String sResources = event.getServletContext().getInitParameter("resteasy.scan.resources"); if (sResources != null) { scanResources = Boolean.valueOf(sResources.trim()); } if (scanProviders || scanResources) { throw new RuntimeException(Messages.MESSAGES.cannotUseScanParameters()); } super.contextInitialized(event); }
@Override public void onStartup(ServletContext servletContext) throws ServletException { beforeSessionRepositoryFilter(servletContext); if (this.configurationClasses != null) { AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext(); rootAppContext.register(this.configurationClasses); servletContext.addListener(new ContextLoaderListener(rootAppContext)); } insertSessionRepositoryFilter(servletContext); afterSessionRepositoryFilter(servletContext); }
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(appConfig); servletContext.addListener(new ContextLoaderListener(rootContext)); DispatcherServlet dispatcherServlet = new DispatcherServlet(rootContext); dispatcherServlet.setDispatchOptionsRequest(true); dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); servletContext.addServlet("dispatcher", dispatcherServlet); if (dispatcherServletRegistration == null) { LOG.warn( } else { final long maxSize = (long) maxFileSize * MB; dispatcherServletRegistration.addMapping("/"); dispatcherServletRegistration.setMultipartConfig( new MultipartConfigElement(null, maxSize, maxSize, FILE_SIZE_THRESHOLD)); servletContext.addFilter("browserDetectionFilter", BrowserDetectionFilter.class); browserDetectionFiler.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "*"); Dynamic etagFilter = servletContext.addFilter("etagFilter", ShallowEtagHeaderFilter.class); etagFilter.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST), true, "dispatcher");
protected void addListeners(ServletContext servletContext, WebApplicationContext appContext) { // Add context listener with refreshed appContext servletContext.addListener(new ContextLoaderListener(appContext)); }
@Override public void contextInitialized(ServletContextEvent event) { super.contextInitialized(event); ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext()); String path = event.getServletContext().getRealPath(""); generateHessianServlet(applicationContext, path); }
AnnotationConfigWebApplicationContext rootWebContext = new AnnotationConfigWebApplicationContext(); rootWebContext.setId(ROOT_WEB_CONTEXT_ID); rootWebContext.setParent(MangoRuntimeContextConfiguration.getRuntimeContext()); rootWebContext.register(MangoRootWebContextConfiguration.class); context.addListener(new ContextLoaderListener(rootWebContext)); context.addServlet(JSP_DISPATCHER_NAME, new DispatcherServlet(jspDispatcherContext)); jspDispatcher.setLoadOnStartup(1); jspDispatcher.addMapping("*.htm", "*.shtm"); context.addServlet(ROOT_REST_DISPATCHER_NAME, new DispatcherServlet(rootRestContext)); rootRestDispatcher.setLoadOnStartup(2); rootRestDispatcher.addMapping("/rest/*");
public void onStartup(final ServletContext servletContext) { servletContext.setInitParameter("contextConfigLocation", "<NONE>"); AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); root.register(SpringWebConfig.class); root.setConfigLocation("com.jtbdevelopment"); servletContext.addListener(new ContextLoaderListener(root)); Dynamic jersey = servletContext.addServlet("REST", ServletContainer.class); jersey.setLoadOnStartup(0); Map<String, String> map = new HashMap<>(); map.put("jersey.config.server.provider.packages", "com.jtbdevelopment"); map.put("jersey.config.server.provider.classnames", "org.glassfish.jersey.filter.LoggingFilter;org.glassfish.jersey.message.DeflateEncoder;org.glassfish.jersey.message.GZipEncoder;org.glassfish.jersey.server.filter.EncodingFilter"); map.put("jersey.config.server.tracing", "ALL"); jersey.setInitParameters(map); jersey.addMapping("/api/*"); }
@Override public void onInitialize(ServletContext servletContext, AnnotationConfigWebApplicationContext applicationContext) { ServletRegistration.Dynamic dispatcherServlet = servletContext .addServlet("Spring MVC Dispatcher Servlet", new DispatcherServlet(applicationContext)); dispatcherServlet.setLoadOnStartup(1); dispatcherServlet.addMapping("/*"); .addFilter("springSecurityFilterChain", new DelegatingFilterProxy( "springSecurityFilterChain", applicationContext)) .addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*"); .addFilter("opensessioninview", OpenSessionInViewFilter.class) .addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*"); .addServlet(SimplePostFilesStrategy.class.getName(), FileUploadServlet.class) .addMapping(SimplePostFilesStrategy.URL_PATTERN); .addListener(new ContextLoaderListener(applicationContext));