Java Code Examples for org.springframework.web.context.support.AnnotationConfigWebApplicationContext#setServletContext()

The following examples show how to use org.springframework.web.context.support.AnnotationConfigWebApplicationContext#setServletContext() . 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: WebAppInitializer.java    From tutorials with MIT License 6 votes vote down vote up
public void onStartup(ServletContext container) throws ServletException {

        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(WebMvcConfigure.class);
        ctx.setServletContext(container);

        ServletRegistration.Dynamic servletOne = container.addServlet("SpringProgrammaticDispatcherServlet", new DispatcherServlet(ctx));
        servletOne.setLoadOnStartup(1);
        servletOne.addMapping("/");

        XmlWebApplicationContext xctx = new XmlWebApplicationContext();
        xctx.setConfigLocation("/WEB-INF/context.xml");
        xctx.setServletContext(container);

        ServletRegistration.Dynamic servletTwo = container.addServlet("SpringProgrammaticXMLDispatcherServlet", new DispatcherServlet(xctx));
        servletTwo.setLoadOnStartup(1);
        servletTwo.addMapping("/");
    }
 
Example 2
Source File: WebAppConfiguration.java    From sakai with 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 3
Source File: WebAppConfiguration.java    From sakai with 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));
    
    FilterRegistration requestFilterRegistration = servletContext.addFilter("sakai.request", RequestFilter.class);
    requestFilterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE), true, "/*");
    requestFilterRegistration.setInitParameter(RequestFilter.CONFIG_UPLOAD_ENABLED, "true");       

    Dynamic servlet = servletContext.addServlet("sakai-site-group-manager", new DispatcherServlet(rootContext));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);
}
 
Example 4
Source File: WebAppConfiguration.java    From sakai with 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.message.bundle.manager", new DispatcherServlet(rootContext));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);
}
 
Example 5
Source File: WebAppConfiguration.java    From sakai with 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));
    
    FilterRegistration requestFilterRegistration = servletContext.addFilter("sakai.request", RequestFilter.class);
    requestFilterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE), true, "/*");
    requestFilterRegistration.setInitParameter(RequestFilter.CONFIG_UPLOAD_ENABLED, "true");       

    Dynamic servlet = servletContext.addServlet("sakai-site-group-manager", new DispatcherServlet(rootContext));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);
}
 
Example 6
Source File: ViewResolutionIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
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 7
Source File: WebAppConfiguration.java    From sakai with 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.message.bundle.manager", new DispatcherServlet(rootContext));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);
}
 
Example 8
Source File: WebInitializer.java    From springMvc4.x-project with Apache License 2.0 5 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext)
		throws ServletException {
	AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
	ctx.register(MyMvcConfig.class);
	ctx.setServletContext(servletContext); // ②

	Dynamic servlet = servletContext.addServlet("dispatcher",new DispatcherServlet(ctx)); // 3
	servlet.addMapping("/");
	servlet.setLoadOnStartup(1);
}
 
Example 9
Source File: WebInitializer.java    From audit4j-demo with Apache License 2.0 5 votes vote down vote up
public void onStartup(ServletContext servletContext) {
	AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
	ctx.setConfigLocation("org.audt4j.demo.hibernate.config");
	servletContext.addListener(new ContextLoaderListener(ctx));
	ctx.setServletContext(servletContext);
	Dynamic servlet = servletContext.addServlet("dispatcher",
			new DispatcherServlet(ctx));
	servlet.addMapping("/*");
	servlet.setLoadOnStartup(1);
	FilterRegistration.Dynamic springSecurityFilterChain = servletContext
			.addFilter("springSecurityFilterChain",
					new DelegatingFilterProxy());
	springSecurityFilterChain.addMappingForUrlPatterns(null, false, "/*");
	springSecurityFilterChain.setAsyncSupported(true);
}
 
Example 10
Source File: AppInitializer.java    From rollbar-java with MIT License 5 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
  AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
  ctx.register(RollbarConfig.class);
  ctx.setServletContext(servletContext);

  ServletRegistration.Dynamic servlet =
          servletContext.addServlet("dispatcher",
                  new DispatcherServlet(ctx));
  servlet.setLoadOnStartup(1);
  servlet.addMapping("/");

}
 
Example 11
Source File: MockSpringMvcServlet.java    From karate with 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 12
Source File: AppInitializer.java    From FRC-2018-Public with MIT License 5 votes vote down vote up
public void onStartup(ServletContext container) throws ServletException {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(AppConfig.class);
    ctx.setServletContext(container);

    ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));

    servlet.setLoadOnStartup(1);
    servlet.addMapping("/");
}
 
Example 13
Source File: DispatcherServletChannelInitializer.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public DispatcherServletChannelInitializer(Class<? extends WebMvcConfigurerAdapter> clasConfig) throws ServletException {
	MockServletContext servletContext = new MockServletContext();
	MockServletConfig servletConfig = new MockServletConfig(servletContext);

	//the alternative for web.xml
	AnnotationConfigWebApplicationContext wac = new AnnotationConfigWebApplicationContext();
	wac.setServletContext(servletContext);
	wac.setServletConfig(servletConfig);
	wac.register(clasConfig);
	wac.refresh();

	this.dispatcherServlet = new DispatcherServlet(wac);
	this.dispatcherServlet.init(servletConfig);
}
 
Example 14
Source File: MvcUriComponentsBuilderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
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 15
Source File: WebInitializer.java    From springMvc4.x-project with Apache License 2.0 5 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext)
		throws ServletException {
	AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
	ctx.register(MyMvcConfig.class);
	ctx.setServletContext(servletContext); // ②

	Dynamic servlet = servletContext.addServlet("dispatcher",new DispatcherServlet(ctx)); // 3
	servlet.addMapping("/");
	servlet.setLoadOnStartup(1);
}
 
Example 16
Source File: StudentControllerConfig.java    From tutorials with MIT License 5 votes vote down vote up
public void onStartup(ServletContext sc) throws ServletException {
    AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
    root.register(WebConfig.class);
    root.setServletContext(sc);
    sc.addListener(new ContextLoaderListener(root));

    DispatcherServlet dv = new DispatcherServlet(root);
    
    ServletRegistration.Dynamic appServlet = sc.addServlet("test-mvc", dv);
    appServlet.setLoadOnStartup(1);
    appServlet.addMapping("/test/*");
}
 
Example 17
Source File: ResourceUrlProviderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void initializeOnce() throws Exception {
	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.setServletContext(new MockServletContext());
	context.register(HandlerMappingConfiguration.class);
	context.refresh();
	ResourceUrlProvider translator = context.getBean(ResourceUrlProvider.class);
	assertThat(translator.getHandlerMap(), Matchers.hasKey("/resources/**"));
	assertFalse(translator.isAutodetect());
}
 
Example 18
Source File: WebMvcConfigurationSupportTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private ApplicationContext initContext(Class<?>... configClasses) {
	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.setServletContext(new MockServletContext());
	context.register(configClasses);
	context.refresh();
	return context;
}
 
Example 19
Source File: WebInitializer.java    From springMvc4.x-project with Apache License 2.0 5 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext)
		throws ServletException {
	AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
	ctx.register(MyMvcConfig.class);
	ctx.setServletContext(servletContext); // ②

	Dynamic servlet = servletContext.addServlet("dispatcher",new DispatcherServlet(ctx)); // 3
	servlet.addMapping("/");
	servlet.setLoadOnStartup(1);
}
 
Example 20
Source File: WebMvcConfigurationSupportTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private ApplicationContext initContext(Class<?>... configClasses) {
	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.setServletContext(new MockServletContext());
	context.register(configClasses);
	context.refresh();
	return context;
}