Java Code Examples for org.springframework.web.context.support.AnnotationConfigWebApplicationContext

The following examples show how to use org.springframework.web.context.support.AnnotationConfigWebApplicationContext. These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: Spring-5.0-Cookbook   Source File: SpringWebinitializer.java    License: MIT License 6 votes vote down vote up
private void addDispatcherContext(ServletContext container) {
	// Create the dispatcher servlet's Spring application context
	AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
	dispatcherContext.register(SpringDispatcherConfig.class);

	// Declare <servlet> and <servlet-mapping> for the DispatcherServlet
	ServletRegistration.Dynamic dispatcher = container.addServlet("ch03-servlet",
			new DispatcherServlet(dispatcherContext));
	dispatcher.addMapping("*.html");
	dispatcher.setLoadOnStartup(1);

	FilterRegistration.Dynamic corsFilter = container.addFilter("corsFilter", new CorsFilter());
	corsFilter.setInitParameter("cors.allowed.methods", "GET, POST, HEAD, OPTIONS, PUT, DELETE");
	corsFilter.addMappingForUrlPatterns(null, true, "/*");

	FilterRegistration.Dynamic filter = container.addFilter("hiddenmethodfilter", new HiddenHttpMethodFilter());
	filter.addMappingForServletNames(null, true, "/*");

	FilterRegistration.Dynamic multipartFilter = container.addFilter("multipartFilter", new MultipartFilter());
	multipartFilter.addMappingForUrlPatterns(null, true, "/*");

}
 
Example 2
@Test
public void getCorsConfigurationPreFlight() throws Exception {
	AnnotationConfigWebApplicationContext cxt = new AnnotationConfigWebApplicationContext();
	cxt.register(TestConfig.class);
	cxt.refresh();

	// PRE-FLIGHT

	MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", "/path");
	request.addHeader("Origin", "http://localhost:9000");
	request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "POST");
	CorsConfiguration corsConfig = getIntrospector(cxt).getCorsConfiguration(request);

	assertNotNull(corsConfig);
	assertEquals(Collections.singletonList("http://localhost:9000"), corsConfig.getAllowedOrigins());
	assertEquals(Collections.singletonList("POST"), corsConfig.getAllowedMethods());
}
 
Example 3
@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
private MockHttpServletResponse runTest(Class<?> configClass) throws ServletException, IOException {
	String basePath = "org/springframework/web/servlet/config/annotation";
	MockServletContext servletContext = new MockServletContext(basePath);
	MockServletConfig servletConfig = new MockServletConfig(servletContext);
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
	MockHttpServletResponse response = new MockHttpServletResponse();

	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.register(configClass);
	context.setServletContext(servletContext);
	context.refresh();
	DispatcherServlet servlet = new DispatcherServlet(context);
	servlet.init(servletConfig);
	servlet.service(request, response);
	return response;
}
 
Example 5
@Before
public void setup() throws Exception {
	logger.debug("Setting up '" + this.testName.getMethodName() + "', client=" +
			this.webSocketClient.getClass().getSimpleName() + ", server=" +
			this.server.getClass().getSimpleName());

	this.wac = new AnnotationConfigWebApplicationContext();
	this.wac.register(getAnnotatedConfigClasses());
	this.wac.register(upgradeStrategyConfigTypes.get(this.server.getClass()));

	if (this.webSocketClient instanceof Lifecycle) {
		((Lifecycle) this.webSocketClient).start();
	}

	this.server.setup();
	this.server.deployConfig(this.wac);
	this.server.start();

	this.wac.setServletContext(this.server.getServletContext());
	this.wac.refresh();
}
 
Example 6
Source Project: product-ei   Source File: Starter.java    License: Apache License 2.0 6 votes vote down vote up
public void startPeopleService() throws Exception {
    final File base = createBaseDirectory();
    log.info("Using base folder: " + base.getAbsolutePath());

    final Tomcat tomcat = new Tomcat();
    tomcat.setPort(8080);
    tomcat.setBaseDir(base.getAbsolutePath());

    Context context = tomcat.addContext("/", base.getAbsolutePath());
    Tomcat.addServlet(context, "CXFServlet", new CXFServlet());

    context.addServletMapping("/rest/*", "CXFServlet");
    context.addApplicationListener(ContextLoaderListener.class.getName());
    context.setLoader(new WebappLoader(Thread.currentThread().getContextClassLoader()));

    context.addParameter("contextClass", AnnotationConfigWebApplicationContext.class.getName());
    context.addParameter("contextConfigLocation", AppConfig.class.getName());

    tomcat.start();
    tomcat.getServer().await();
}
 
Example 7
Source Project: flowable-engine   Source File: JPAWebConfigurer.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Initializes Spring and Spring MVC.
 */
private ServletRegistration.Dynamic initSpring(ServletContext servletContext, AnnotationConfigWebApplicationContext rootContext) {
    LOGGER.debug("Configuring Spring Web application context");
    AnnotationConfigWebApplicationContext dispatcherServletConfiguration = new AnnotationConfigWebApplicationContext();
    dispatcherServletConfiguration.setParent(rootContext);
    dispatcherServletConfiguration.register(DispatcherServletConfiguration.class);

    LOGGER.debug("Registering Spring MVC Servlet");
    ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherServletConfiguration));
    dispatcherServlet.addMapping("/service/*");
    dispatcherServlet.setMultipartConfig(new MultipartConfigElement((String) null));
    dispatcherServlet.setLoadOnStartup(1);
    dispatcherServlet.setAsyncSupported(true);

    return dispatcherServlet;
}
 
Example 8
Source Project: nem.deploy   Source File: AbstractNemServletContextListener.java    License: MIT License 6 votes vote down vote up
@Override
public void contextInitialized(final ServletContextEvent event) {
	try {
		final AnnotationConfigWebApplicationContext webCtx = new AnnotationConfigWebApplicationContext();

		webCtx.register(this.webAppInitializerClass);
		webCtx.setParent(this.appCtx);

		final ServletContext context = event.getServletContext();
		this.initialize(webCtx, context);

		context.setInitParameter("contextClass", "org.springframework.web.context.support.AnnotationConfigWebApplicationContext");

		if (this.useDosFilter) {
			addDosFilter(context);
		}

		addGzipFilter(context);
		addCorsFilter(context);
	} catch (final Exception e) {
		throw new RuntimeException(String.format("Exception in contextInitialized: %s", e.toString()), e);
	}
}
 
Example 9
private MockHttpServletResponse runTest(Class<?> configClass) throws ServletException, IOException {
	String basePath = "org/springframework/web/servlet/config/annotation";
	MockServletContext servletContext = new MockServletContext(basePath);
	MockServletConfig servletConfig = new MockServletConfig(servletContext);
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
	MockHttpServletResponse response = new MockHttpServletResponse();

	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.register(configClass);
	context.setServletContext(servletContext);
	context.refresh();
	DispatcherServlet servlet = new DispatcherServlet(context);
	servlet.init(servletConfig);
	servlet.service(request, response);
	return response;
}
 
Example 10
Source Project: sakai   Source File: WebAppConfiguration.java    License: Educational Community License v2.0 6 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
	AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
	rootContext.setServletContext(servletContext);
	rootContext.register(ThymeleafConfig.class);

	servletContext.addListener(new ToolListener());
	servletContext.addListener(new SakaiContextLoaderListener(rootContext));

	servletContext.addFilter("sakai.request", RequestFilter.class)
		.addMappingForUrlPatterns(
			EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE),
			true,
			"/*");

	Dynamic servlet = servletContext.addServlet("sakai.datemanager", new DispatcherServlet(rootContext));
	servlet.addMapping("/");
	servlet.setLoadOnStartup(1);
}
 
Example 11
Source Project: sakai   Source File: WebAppConfiguration.java    License: Educational Community License v2.0 6 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.setServletContext(servletContext);
    rootContext.register(ThymeleafConfig.class);

    servletContext.addListener(new SakaiContextLoaderListener(rootContext));

    servletContext.addFilter("sakai.request", RequestFilter.class)
            .addMappingForUrlPatterns(
                    EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE),
                    true,
                    "/*");

    Dynamic servlet = servletContext.addServlet("sakai.onedrive", new DispatcherServlet(rootContext));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);
}
 
Example 12
private MockHttpServletResponse runTest(Class<?> configClass) throws ServletException, IOException {
	String basePath = "org/springframework/web/servlet/config/annotation";
	MockServletContext servletContext = new MockServletContext(basePath);
	MockServletConfig servletConfig = new MockServletConfig(servletContext);
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
	MockHttpServletResponse response = new MockHttpServletResponse();

	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.register(configClass);
	context.setServletContext(servletContext);
	context.refresh();
	DispatcherServlet servlet = new DispatcherServlet(context);
	servlet.init(servletConfig);
	servlet.service(request, response);
	return response;
}
 
Example 13
@Before
public void setup() throws Exception {
	logger.debug("Setting up '" + this.testName.getMethodName() + "'");
	this.testFilter = new TestFilter();

	this.wac = new AnnotationConfigWebApplicationContext();
	this.wac.register(TestConfig.class, upgradeStrategyConfigClass());

	this.server = createWebSocketTestServer();
	this.server.setup();
	this.server.deployConfig(this.wac, this.testFilter);
	this.server.start();

	this.wac.setServletContext(this.server.getServletContext());
	this.wac.refresh();

	this.baseUrl = "http://localhost:" + this.server.getPort();
}
 
Example 14
@Override
public void onStartup(ServletContext container) {
    // Create the 'root' Spring application context
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    // Register application config
    rootContext.register(AppConfig.class);

    // Manage the lifecycle of the root application context
    container.addListener(new ContextLoaderListener(rootContext));

    // Register Encoding Filter
    addEncodingFilter(container);

    // Register Logging Filter
    addLoggingFilter(container);

    // Register and map the dispatcher servlet
    addServiceDispatcherServlet(container, rootContext);
}
 
Example 15
Source Project: flowable-engine   Source File: WebConfigurer.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Initializes Spring and Spring MVC.
 */
private ServletRegistration.Dynamic initSpring(ServletContext servletContext, AnnotationConfigWebApplicationContext rootContext) {
    LOGGER.debug("Configuring Spring Web application context");
    AnnotationConfigWebApplicationContext dispatcherServletConfiguration = new AnnotationConfigWebApplicationContext();
    dispatcherServletConfiguration.setParent(rootContext);
    dispatcherServletConfiguration.register(DispatcherServletConfiguration.class);

    LOGGER.debug("Registering Spring MVC Servlet");
    ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherServletConfiguration));
    dispatcherServlet.addMapping("/service/*");
    dispatcherServlet.setMultipartConfig(new MultipartConfigElement((String) null));
    dispatcherServlet.setLoadOnStartup(1);
    dispatcherServlet.setAsyncSupported(true);

    return dispatcherServlet;
}
 
Example 16
Source Project: flowable-engine   Source File: WebConfigurer.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Initializes Spring and Spring MVC.
 */
private ServletRegistration.Dynamic initSpring(ServletContext servletContext, AnnotationConfigWebApplicationContext rootContext) {
    LOGGER.debug("Configuring Spring Web application context");
    AnnotationConfigWebApplicationContext dispatcherServletConfiguration = new AnnotationConfigWebApplicationContext();
    dispatcherServletConfiguration.setParent(rootContext);
    dispatcherServletConfiguration.register(DispatcherServletConfiguration.class);

    LOGGER.debug("Registering Spring MVC Servlet");
    ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherServletConfiguration));
    dispatcherServlet.addMapping("/service/*");
    dispatcherServlet.setMultipartConfig(new MultipartConfigElement((String) null));
    dispatcherServlet.setLoadOnStartup(1);
    dispatcherServlet.setAsyncSupported(true);

    return dispatcherServlet;
}
 
Example 17
Source Project: activiti6-boot2   Source File: BaseJPARestTestCase.java    License: Apache License 2.0 6 votes vote down vote up
public static void createAndStartServer() {
  server = new Server(HTTP_SERVER_PORT);

  HashSessionIdManager idmanager = new HashSessionIdManager();
  server.setSessionIdManager(idmanager);

  AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
  applicationContext.register(JPAApplicationConfiguration.class);
  applicationContext.refresh();

  appContext = applicationContext;

  try {
    server.setHandler(getServletContextHandler(applicationContext));
    server.start();
  } catch (Exception e) {
    log.error("Error starting server", e);
  }
}
 
Example 18
Source Project: flowable-engine   Source File: WebConfigurer.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Initializes Spring and Spring MVC.
 */
private ServletRegistration.Dynamic initSpring(ServletContext servletContext, AnnotationConfigWebApplicationContext rootContext) {
    LOGGER.debug("Configuring Spring Web application context");
    AnnotationConfigWebApplicationContext dispatcherServletConfiguration = new AnnotationConfigWebApplicationContext();
    dispatcherServletConfiguration.setParent(rootContext);
    dispatcherServletConfiguration.register(DispatcherServletConfiguration.class);

    LOGGER.debug("Registering Spring MVC Servlet");
    ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherServletConfiguration));
    dispatcherServlet.addMapping("/service/*");
    dispatcherServlet.setMultipartConfig(new MultipartConfigElement((String) null));
    dispatcherServlet.setLoadOnStartup(1);
    dispatcherServlet.setAsyncSupported(true);

    return dispatcherServlet;
}
 
Example 19
Source Project: flowable-engine   Source File: WebConfigurer.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Initializes Spring and Spring MVC.
 */
private ServletRegistration.Dynamic initSpring(ServletContext servletContext, AnnotationConfigWebApplicationContext rootContext) {
    LOGGER.debug("Configuring Spring Web application context");
    AnnotationConfigWebApplicationContext dispatcherServletConfiguration = new AnnotationConfigWebApplicationContext();
    dispatcherServletConfiguration.setParent(rootContext);
    dispatcherServletConfiguration.register(DispatcherServletConfiguration.class);

    LOGGER.debug("Registering Spring MVC Servlet");
    ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherServletConfiguration));
    dispatcherServlet.addMapping("/service/*");
    dispatcherServlet.setMultipartConfig(new MultipartConfigElement((String) null));
    dispatcherServlet.setLoadOnStartup(1);
    dispatcherServlet.setAsyncSupported(true);

    return dispatcherServlet;
}
 
Example 20
Source Project: flowable-engine   Source File: WebConfigurer.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Initializes Spring and Spring MVC.
 */
private ServletRegistration.Dynamic initSpring(ServletContext servletContext, AnnotationConfigWebApplicationContext rootContext) {
    LOGGER.debug("Configuring Spring Web application context");
    AnnotationConfigWebApplicationContext dispatcherServletConfiguration = new AnnotationConfigWebApplicationContext();
    dispatcherServletConfiguration.setParent(rootContext);
    dispatcherServletConfiguration.register(DispatcherServletConfiguration.class);

    LOGGER.debug("Registering Spring MVC Servlet");
    ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherServletConfiguration));
    dispatcherServlet.addMapping("/service/*");
    dispatcherServlet.setMultipartConfig(new MultipartConfigElement((String) null));
    dispatcherServlet.setLoadOnStartup(1);
    dispatcherServlet.setAsyncSupported(true);

    return dispatcherServlet;
}
 
Example 21
Source Project: spring-analysis-note   Source File: ResourceUrlProviderTests.java    License: MIT License 5 votes vote down vote up
@Test  // SPR-12592
@SuppressWarnings("resource")
public void initializeOnce() {
	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.setServletContext(new MockServletContext());
	context.register(HandlerMappingConfiguration.class);
	context.refresh();

	assertThat(context.getBean(ResourceUrlProvider.class).getHandlerMap(),
			Matchers.hasKey(pattern("/resources/**")));
}
 
Example 22
@Override
public void onStartup(ServletContext servletContext) {
	beforeSessionRepositoryFilter(servletContext);
	if (this.configurationClasses != null) {
		AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext();
		rootAppContext.register(this.configurationClasses);
		servletContext.addListener(new ContextLoaderListener(rootAppContext));
	}
	insertSessionRepositoryFilter(servletContext);
	afterSessionRepositoryFilter(servletContext);
}
 
Example 23
Source Project: karate   Source File: MockSpringMvcServlet.java    License: MIT License 5 votes vote down vote up
private static Servlet initServlet() {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.register(MockDemoConfig.class);
    context.setServletContext(SERVLET_CONTEXT);
    DispatcherServlet servlet = new DispatcherServlet(context);
    ServletConfig servletConfig = new MockServletConfig();
    try {
        servlet.init(servletConfig);
        customize(servlet);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return servlet;
}
 
Example 24
private void initWebApplicationContext(Class<?> configClass) {
	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.setServletContext(new MockServletContext());
	context.register(configClass);
	context.refresh();
	this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
}
 
Example 25
@Test
public void getCorsConfigurationActual() throws Exception {
	AnnotationConfigWebApplicationContext cxt = new AnnotationConfigWebApplicationContext();
	cxt.register(TestConfig.class);
	cxt.refresh();

	MockHttpServletRequest request = new MockHttpServletRequest("POST", "/path");
	request.addHeader("Origin", "http://localhost:9000");
	CorsConfiguration corsConfig = getIntrospector(cxt).getCorsConfiguration(request);

	assertNotNull(corsConfig);
	assertEquals(Collections.singletonList("http://localhost:9000"), corsConfig.getAllowedOrigins());
	assertEquals(Collections.singletonList("POST"), corsConfig.getAllowedMethods());
}
 
Example 26
Source Project: junit-servers   Source File: WebApplicationConfiguration.java    License: MIT License 5 votes vote down vote up
private void initSpringMvc(ServletContext servletContext, AnnotationConfigWebApplicationContext context) {
	DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
	dispatcherServlet.setContextConfigLocation(configLocation());
	dispatcherServlet.setContextClass(AnnotationConfigWebApplicationContext.class);

	ServletRegistration.Dynamic dispatcher = servletContext.addServlet("spring", dispatcherServlet);
	dispatcher.setLoadOnStartup(1);
	dispatcher.addMapping("/*");
	dispatcher.addMapping("/");
}
 
Example 27
private ApplicationContext initContext(Class<?>... configClasses) {
	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.setServletContext(new MockServletContext());
	context.register(configClasses);
	context.refresh();
	return context;
}
 
Example 28
Source Project: tutorials   Source File: CustomWebAppInitializer.java    License: MIT License 5 votes vote down vote up
@Override
public void onStartup(ServletContext container) throws ServletException {

    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setConfigLocation("com.baeldung.web.log");
    container.addListener(new ContextLoaderListener(context));

    ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(context));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/"); 

    container.addFilter("customRequestLoggingFilter", CustomeRequestLoggingFilter.class).addMappingForServletNames(null, false, "dispatcher");
}
 
Example 29
Source Project: Spring-5.0-Cookbook   Source File: SpringWebinitializer.java    License: MIT License 5 votes vote down vote up
private void addRootContext(ServletContext container) {
  // Create the application context
  AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
  rootContext.register(SpringContextConfig.class); 
	 
  // Register application context with ContextLoaderListener
  container.addListener(new ContextLoaderListener(rootContext));
 container.setInitParameter("contextConfigLocation", "org.packt.web.reactor.security.config");
 container.setSessionTrackingModes(EnumSet.of(SessionTrackingMode.COOKIE)); // if URL, enable sessionManagement URL rewriting   
	 
}
 
Example 30
@Test
public void annotationConfigWebApplicationContext() {
	AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
	ctx.setEnvironment(prodWebEnv);
	ctx.setConfigLocation(EnvironmentAwareBean.class.getName());
	ctx.refresh();

	assertHasEnvironment(ctx, prodWebEnv);
	assertEnvironmentBeanRegistered(ctx);
	assertEnvironmentAwareInvoked(ctx, prodWebEnv);
}