Java Code Examples for org.springframework.web.context.WebApplicationContext

Following code examples demonstrate how to use org.springframework.web.context.WebApplicationContextfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.web.context.WebApplicationContextand various code implementation of this class.

    protected WebApplicationContext createServletApplicationContext() {
        XmlWebApplicationContext cxt = new XmlWebApplicationContext();
        cxt.setConfigLocation("/WEB-INF/spring/spring-mvc.xml");
        return cxt;
    } 


	public void test() {
		ServletContext context = createStrictMock(ServletContext.class);
		WebApplicationContext webContext = createStrictMock(WebApplicationContext.class);

		expect(context.getAttribute("org.springframework.web.context.WebApplicationContext.ROOT")).andReturn(webContext);
		expect(context.getInitParameter("extension.point.start")).andReturn("lifecycle.frontend.start");
		expect(context.getInitParameter("extension.point.shutdown")).andReturn("lifecycle.frontend.shutdown");

		PluginHolder pluginHolder = new PluginHolder();
		List<Plugin> plugins = Collections.emptyList();
		pluginHolder.setPlugins(plugins);
		expect(webContext.getBean("pluginHolder")).andReturn(pluginHolder).anyTimes();

		replay(context, webContext);

		ContextStateListener listener = new ContextStateListener();
		listener.contextInitialized(new ServletContextEvent(context));
		listener.contextDestroyed(new ServletContextEvent(context));

		verify(context, webContext);
	} 

	public void testInvalidExtensionParam() {
		ServletContext context = createStrictMock(ServletContext.class);
		WebApplicationContext webContext = createStrictMock(WebApplicationContext.class);

		expect(context.getAttribute("org.springframework.web.context.WebApplicationContext.ROOT")).andReturn(webContext);
		expect(context.getInitParameter("extension.point.start")).andReturn(null);

		replay(context, webContext);

		ContextStateListener listener = new ContextStateListener();

		try {
			listener.contextInitialized(new ServletContextEvent(context));
			fail("Should catch Exception here");
		} catch (IllegalStateException e) {
			TestHelpers.assertContains(e, "Missing servlet-context param", "extension.point.start");
		}

		verify(context, webContext);
	} 

	public ViewPreparer getPreparer(String name, Request context) {
		WebApplicationContext webApplicationContext = (WebApplicationContext) context.getContext("request").get(
				DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		if (webApplicationContext == null) {
			webApplicationContext = (WebApplicationContext) context.getContext("application").get(
					WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
			if (webApplicationContext == null) {
				throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?");
			}
		}
		return getPreparer(name, webApplicationContext);
	} 

	public void contextInitialized(ServletContextEvent event) {
		super.contextInitialized(event);
		WebApplicationContext rootContext1 = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
		WebApplicationContext rootContext2 = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext(),"org.springframework.web.context.WebApplicationContext.ROOT");
		System.out.println(rootContext1==rootContext2);
	} 

Advertisement
Javadoc
Interface to provide configuration for a web application. This is read-only while the application is

running, but may be reloaded if the implementation supports this.

This interface adds a getServletContext() method to the generic ApplicationContext interface, and defines a well-known application attribute name that the root context must be bound to in the bootstrap process.

Like generic application contexts, web application contexts are hierarchical. There is a single root context per application, while each servlet in the application (including a dispatcher servlet in the MVC framework) has its own child context.

In addition to standard application context lifecycle capabilities, WebApplicationContext implementations need to detect ServletContextAware beans and invoke the setServletContext method accordingly. @author Rod Johnson @author Juergen Hoeller @since January 19, 2001 @see ServletContextAware#setServletContext

Read More
Advertisement