Java Code Examples for org.springframework.web.servlet.DispatcherServlet#getWebApplicationContext()

The following examples show how to use org.springframework.web.servlet.DispatcherServlet#getWebApplicationContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnnotationConfigDispatcherServletInitializerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void rootContextOnly() throws ServletException {
	initializer = new MyAnnotationConfigDispatcherServletInitializer() {
		@Override
		protected Class<?>[] getRootConfigClasses() {
			return new Class<?>[] {MyConfiguration.class};
		}
		@Override
		protected Class<?>[] getServletConfigClasses() {
			return null;
		}
	};

	initializer.onStartup(servletContext);

	DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME);
	servlet.init(new MockServletConfig(this.servletContext));

	WebApplicationContext wac = servlet.getWebApplicationContext();
	((AnnotationConfigWebApplicationContext) wac).refresh();

	assertTrue(wac.containsBean("bean"));
	assertTrue(wac.getBean("bean") instanceof MyBean);
}
 
Example 2
Source File: AnnotationConfigDispatcherServletInitializerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void rootContextOnly() throws ServletException {
	initializer = new MyAnnotationConfigDispatcherServletInitializer() {
		@Override
		protected Class<?>[] getRootConfigClasses() {
			return new Class<?>[] {MyConfiguration.class};
		}
		@Override
		protected Class<?>[] getServletConfigClasses() {
			return null;
		}
	};

	initializer.onStartup(servletContext);

	DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME);
	servlet.init(new MockServletConfig(this.servletContext));

	WebApplicationContext wac = servlet.getWebApplicationContext();
	((AnnotationConfigWebApplicationContext) wac).refresh();

	assertTrue(wac.containsBean("bean"));
	assertTrue(wac.getBean("bean") instanceof MyBean);
}
 
Example 3
Source File: AnnotationConfigDispatcherServletInitializerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void rootContextOnly() throws ServletException {
	initializer = new MyAnnotationConfigDispatcherServletInitializer() {
		@Override
		protected Class<?>[] getRootConfigClasses() {
			return new Class<?>[] {MyConfiguration.class};
		}
		@Override
		protected Class<?>[] getServletConfigClasses() {
			return null;
		}
	};

	initializer.onStartup(servletContext);

	DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME);
	servlet.init(new MockServletConfig(this.servletContext));

	WebApplicationContext wac = servlet.getWebApplicationContext();
	((AnnotationConfigWebApplicationContext) wac).refresh();

	assertTrue(wac.containsBean("bean"));
	assertTrue(wac.getBean("bean") instanceof MyBean);
}
 
Example 4
Source File: AnnotationConfigDispatcherServletInitializerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void register() throws ServletException {
	initializer.onStartup(servletContext);

	assertEquals(1, servlets.size());
	assertNotNull(servlets.get(SERVLET_NAME));

	DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME);
	WebApplicationContext wac = servlet.getWebApplicationContext();
	((AnnotationConfigWebApplicationContext) wac).refresh();

	assertTrue(wac.containsBean("bean"));
	assertTrue(wac.getBean("bean") instanceof MyBean);

	assertEquals(1, servletRegistrations.size());
	assertNotNull(servletRegistrations.get(SERVLET_NAME));

	MockServletRegistration servletRegistration = servletRegistrations.get(SERVLET_NAME);

	assertEquals(Collections.singleton(SERVLET_MAPPING), servletRegistration.getMappings());
	assertEquals(1, servletRegistration.getLoadOnStartup());
	assertEquals(ROLE_NAME, servletRegistration.getRunAsRole());
	assertTrue(servletRegistration.isAsyncSupported());

	assertEquals(4, filterRegistrations.size());
	assertNotNull(filterRegistrations.get("hiddenHttpMethodFilter"));
	assertNotNull(filterRegistrations.get("delegatingFilterProxy"));
	assertNotNull(filterRegistrations.get("delegatingFilterProxy#0"));
	assertNotNull(filterRegistrations.get("delegatingFilterProxy#1"));

	for (MockFilterRegistration filterRegistration : filterRegistrations.values()) {
		assertTrue(filterRegistration.isAsyncSupported());
		EnumSet<DispatcherType> enumSet = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD,
				DispatcherType.INCLUDE, DispatcherType.ASYNC);
		assertEquals(enumSet, filterRegistration.getMappings().get(SERVLET_NAME));
	}

}
 
Example 5
Source File: AnnotationConfigDispatcherServletInitializerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void register() throws ServletException {
	initializer.onStartup(servletContext);

	assertEquals(1, servlets.size());
	assertNotNull(servlets.get(SERVLET_NAME));

	DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME);
	WebApplicationContext wac = servlet.getWebApplicationContext();
	((AnnotationConfigWebApplicationContext) wac).refresh();

	assertTrue(wac.containsBean("bean"));
	assertTrue(wac.getBean("bean") instanceof MyBean);

	assertEquals(1, servletRegistrations.size());
	assertNotNull(servletRegistrations.get(SERVLET_NAME));

	MockServletRegistration servletRegistration = servletRegistrations.get(SERVLET_NAME);

	assertEquals(Collections.singleton(SERVLET_MAPPING), servletRegistration.getMappings());
	assertEquals(1, servletRegistration.getLoadOnStartup());
	assertEquals(ROLE_NAME, servletRegistration.getRunAsRole());
	assertTrue(servletRegistration.isAsyncSupported());

	assertEquals(4, filterRegistrations.size());
	assertNotNull(filterRegistrations.get("hiddenHttpMethodFilter"));
	assertNotNull(filterRegistrations.get("delegatingFilterProxy"));
	assertNotNull(filterRegistrations.get("delegatingFilterProxy#0"));
	assertNotNull(filterRegistrations.get("delegatingFilterProxy#1"));

	for (MockFilterRegistration filterRegistration : filterRegistrations.values()) {
		assertTrue(filterRegistration.isAsyncSupported());
		EnumSet<DispatcherType> enumSet = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD,
				DispatcherType.INCLUDE, DispatcherType.ASYNC);
		assertEquals(enumSet, filterRegistration.getMappings().get(SERVLET_NAME));
	}

}
 
Example 6
Source File: AnnotationConfigDispatcherServletInitializerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void register() throws ServletException {
	initializer.onStartup(servletContext);

	assertEquals(1, servlets.size());
	assertNotNull(servlets.get(SERVLET_NAME));

	DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME);
	WebApplicationContext wac = servlet.getWebApplicationContext();
	((AnnotationConfigWebApplicationContext) wac).refresh();

	assertTrue(wac.containsBean("bean"));
	assertTrue(wac.getBean("bean") instanceof MyBean);

	assertEquals(1, servletRegistrations.size());
	assertNotNull(servletRegistrations.get(SERVLET_NAME));

	MockServletRegistration servletRegistration = servletRegistrations.get(SERVLET_NAME);

	assertEquals(Collections.singleton(SERVLET_MAPPING), servletRegistration.getMappings());
	assertEquals(1, servletRegistration.getLoadOnStartup());
	assertEquals(ROLE_NAME, servletRegistration.getRunAsRole());
	assertTrue(servletRegistration.isAsyncSupported());

	assertEquals(4, filterRegistrations.size());
	assertNotNull(filterRegistrations.get("hiddenHttpMethodFilter"));
	assertNotNull(filterRegistrations.get("delegatingFilterProxy"));
	assertNotNull(filterRegistrations.get("delegatingFilterProxy#0"));
	assertNotNull(filterRegistrations.get("delegatingFilterProxy#1"));

	for (MockFilterRegistration filterRegistration : filterRegistrations.values()) {
		assertTrue(filterRegistration.isAsyncSupported());
		assertEquals(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.ASYNC),
				filterRegistration.getMappings().get(SERVLET_NAME));
	}

}
 
Example 7
Source File: WebAdminComponentScanRegistrar.java    From wallride with Apache License 2.0 5 votes vote down vote up
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
	if ("adminDispatcherServlet".equals(beanName)) {
		DispatcherServlet dispatcherServlet = (DispatcherServlet) bean;
		AnnotationConfigServletWebServerApplicationContext context = (AnnotationConfigServletWebServerApplicationContext) dispatcherServlet.getWebApplicationContext();
		context.scan(packagesToScan);
		this.processed = true;
	}
	return bean;
}
 
Example 8
Source File: WebGuestComponentScanRegistrar.java    From wallride with Apache License 2.0 5 votes vote down vote up
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
	if (DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME.equals(beanName)) {
		DispatcherServlet dispatcherServlet = (DispatcherServlet) bean;
		AnnotationConfigServletWebServerApplicationContext context = (AnnotationConfigServletWebServerApplicationContext) dispatcherServlet.getWebApplicationContext();
		context.scan(packagesToScan);
		this.processed = true;
	}
	return bean;
}