Java Code Examples for org.springframework.web.context.support.XmlWebApplicationContext#setServletContext()
The following examples show how to use
org.springframework.web.context.support.XmlWebApplicationContext#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: SimpleUrlHandlerMappingTests.java From spring-analysis-note with MIT License | 6 votes |
@Test @SuppressWarnings("resource") public void handlerBeanNotFound() { MockServletContext sc = new MockServletContext(""); XmlWebApplicationContext root = new XmlWebApplicationContext(); root.setServletContext(sc); root.setConfigLocations("/org/springframework/web/servlet/handler/map1.xml"); root.refresh(); XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.setParent(root); wac.setServletContext(sc); wac.setNamespace("map2err"); wac.setConfigLocations("/org/springframework/web/servlet/handler/map2err.xml"); try { wac.refresh(); fail("Should have thrown NoSuchBeanDefinitionException"); } catch (FatalBeanException ex) { NoSuchBeanDefinitionException nestedEx = (NoSuchBeanDefinitionException) ex.getCause(); assertEquals("mainControlle", nestedEx.getBeanName()); } }
Example 2
Source File: DispatcherServletChannelInitializer.java From nettyholdspringmvc with Apache License 2.0 | 6 votes |
public DispatcherServletChannelInitializer() throws ServletException { MockServletContext servletContext = new MockServletContext(); MockServletConfig servletConfig = new MockServletConfig(servletContext); servletConfig.addInitParameter("contextConfigLocation","classpath:/META-INF/spring/root-context.xml"); servletContext.addInitParameter("contextConfigLocation","classpath:/META-INF/spring/root-context.xml"); //AnnotationConfigWebApplicationContext wac = new AnnotationConfigWebApplicationContext(); XmlWebApplicationContext wac = new XmlWebApplicationContext(); //ClassPathXmlApplicationContext wac = new ClassPathXmlApplicationContext(); wac.setServletContext(servletContext); wac.setServletConfig(servletConfig); wac.setConfigLocation("classpath:/servlet-context.xml"); //wac.register(WebConfig.class); wac.refresh(); this.dispatcherServlet = new DispatcherServlet(wac); this.dispatcherServlet.init(servletConfig); }
Example 3
Source File: WebAppInitializer.java From tutorials with MIT License | 6 votes |
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 4
Source File: SimpleUrlHandlerMappingTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void handlerBeanNotFound() throws Exception { MockServletContext sc = new MockServletContext(""); XmlWebApplicationContext root = new XmlWebApplicationContext(); root.setServletContext(sc); root.setConfigLocations(new String[] {"/org/springframework/web/servlet/handler/map1.xml"}); root.refresh(); XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.setParent(root); wac.setServletContext(sc); wac.setNamespace("map2err"); wac.setConfigLocations(new String[] {"/org/springframework/web/servlet/handler/map2err.xml"}); try { wac.refresh(); fail("Should have thrown NoSuchBeanDefinitionException"); } catch (FatalBeanException ex) { NoSuchBeanDefinitionException nestedEx = (NoSuchBeanDefinitionException) ex.getCause(); assertEquals("mainControlle", nestedEx.getBeanName()); } }
Example 5
Source File: SpringResourceLoader.java From rice with Educational Community License v2.0 | 6 votes |
@Override public void start() throws Exception { if(!isStarted()){ LOG.info("Creating Spring context " + StringUtils.join(this.fileLocs, ",")); if (parentSpringResourceLoader != null && parentContext != null) { throw new ConfigurationException("Both a parentSpringResourceLoader and parentContext were defined. Only one can be defined!"); } if (parentSpringResourceLoader != null) { parentContext = parentSpringResourceLoader.getContext(); } if (servletContextcontext != null) { XmlWebApplicationContext lContext = new XmlWebApplicationContext(); lContext.setServletContext(servletContextcontext); lContext.setParent(parentContext); lContext.setConfigLocations(this.fileLocs.toArray(new String[] {})); lContext.refresh(); context = lContext; } else { this.context = new ClassPathXmlApplicationContext(this.fileLocs.toArray(new String[] {}), parentContext); } super.start(); } }
Example 6
Source File: WiringTests.java From cas4.0.x-server-wechat with Apache License 2.0 | 6 votes |
@Before public void setUp() { applicationContext = new XmlWebApplicationContext(); applicationContext.setConfigLocations(new String[]{ "file:src/main/webapp/WEB-INF/cas-management-servlet.xml", "file:src/main/webapp/WEB-INF/managementConfigContext.xml", "file:src/main/webapp/WEB-INF/spring-configuration/*.xml"}); applicationContext.setServletContext(new MockServletContext(new ResourceLoader() { @Override public Resource getResource(final String location) { return new FileSystemResource("src/main/webapp" + location); } @Override public ClassLoader getClassLoader() { return getClassLoader(); } })); applicationContext.refresh(); }
Example 7
Source File: WiringTests.java From cas4.0.x-server-wechat with Apache License 2.0 | 6 votes |
@Before public void setUp() { applicationContext = new XmlWebApplicationContext(); applicationContext.setConfigLocations(new String[]{ "file:src/main/webapp/WEB-INF/cas-servlet.xml", "file:src/main/webapp/WEB-INF/deployerConfigContext.xml", "file:src/main/webapp/WEB-INF/spring-configuration/*.xml"}); applicationContext.setServletContext(new MockServletContext(new ResourceLoader() { @Override public Resource getResource(final String location) { return new FileSystemResource("src/main/webapp" + location); } @Override public ClassLoader getClassLoader() { return getClassLoader(); } })); applicationContext.refresh(); }
Example 8
Source File: WiringTests.java From springboot-shiro-cas-mybatis with MIT License | 6 votes |
@Before public void setUp() { applicationContext = new XmlWebApplicationContext(); applicationContext.setConfigLocations( "file:src/main/webapp/WEB-INF/cas-management-servlet.xml", "file:src/main/webapp/WEB-INF/managementConfigContext.xml", "file:src/main/webapp/WEB-INF/spring-configuration/*.xml"); applicationContext.setServletContext(new MockServletContext(new ResourceLoader() { @Override public Resource getResource(final String location) { return new FileSystemResource("src/main/webapp" + location); } @Override public ClassLoader getClassLoader() { return getClassLoader(); } })); applicationContext.refresh(); }
Example 9
Source File: WiringTests.java From springboot-shiro-cas-mybatis with MIT License | 6 votes |
@Before public void setUp() { applicationContext = new XmlWebApplicationContext(); applicationContext.setConfigLocations( "file:src/main/webapp/WEB-INF/cas-servlet.xml", "file:src/main/webapp/WEB-INF/deployerConfigContext.xml", "file:src/main/webapp/WEB-INF/spring-configuration/*.xml"); applicationContext.setServletContext(new MockServletContext(new ResourceLoader() { @Override public Resource getResource(final String location) { return new FileSystemResource("src/main/webapp" + location); } @Override public ClassLoader getClassLoader() { return getClassLoader(); } })); applicationContext.refresh(); }
Example 10
Source File: XmlWebApplicationContextTests.java From java-technology-stack with MIT License | 6 votes |
@Test @SuppressWarnings("resource") public void withoutMessageSource() throws Exception { MockServletContext sc = new MockServletContext(""); XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.setParent(root); wac.setServletContext(sc); wac.setNamespace("testNamespace"); wac.setConfigLocations(new String[] {"/org/springframework/web/context/WEB-INF/test-servlet.xml"}); wac.refresh(); try { wac.getMessage("someMessage", null, Locale.getDefault()); fail("Should have thrown NoSuchMessageException"); } catch (NoSuchMessageException ex) { // expected; } String msg = wac.getMessage("someMessage", null, "default", Locale.getDefault()); assertTrue("Default message returned", "default".equals(msg)); }
Example 11
Source File: SimpleUrlHandlerMappingTests.java From java-technology-stack with MIT License | 6 votes |
@Test @SuppressWarnings("resource") public void handlerBeanNotFound() { MockServletContext sc = new MockServletContext(""); XmlWebApplicationContext root = new XmlWebApplicationContext(); root.setServletContext(sc); root.setConfigLocations("/org/springframework/web/servlet/handler/map1.xml"); root.refresh(); XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.setParent(root); wac.setServletContext(sc); wac.setNamespace("map2err"); wac.setConfigLocations("/org/springframework/web/servlet/handler/map2err.xml"); try { wac.refresh(); fail("Should have thrown NoSuchBeanDefinitionException"); } catch (FatalBeanException ex) { NoSuchBeanDefinitionException nestedEx = (NoSuchBeanDefinitionException) ex.getCause(); assertEquals("mainControlle", nestedEx.getBeanName()); } }
Example 12
Source File: XmlWebApplicationContextTests.java From spring-analysis-note with MIT License | 6 votes |
@Test @SuppressWarnings("resource") public void withoutMessageSource() throws Exception { MockServletContext sc = new MockServletContext(""); XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.setParent(root); wac.setServletContext(sc); wac.setNamespace("testNamespace"); wac.setConfigLocations(new String[] {"/org/springframework/web/context/WEB-INF/test-servlet.xml"}); wac.refresh(); try { wac.getMessage("someMessage", null, Locale.getDefault()); fail("Should have thrown NoSuchMessageException"); } catch (NoSuchMessageException ex) { // expected; } String msg = wac.getMessage("someMessage", null, "default", Locale.getDefault()); assertTrue("Default message returned", "default".equals(msg)); }
Example 13
Source File: MvcNamespaceTests.java From java-technology-stack with MIT License | 5 votes |
@Before public void setUp() throws Exception { TestMockServletContext servletContext = new TestMockServletContext(); appContext = new XmlWebApplicationContext(); appContext.setServletContext(servletContext); LocaleContextHolder.setLocale(Locale.US); String attributeName = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE; appContext.getServletContext().setAttribute(attributeName, appContext); handler = new TestController(); handlerMethod = new InvocableHandlerMethod(handler, TestController.class.getMethod("testBind", Date.class, Double.class, TestBean.class, BindingResult.class)); }
Example 14
Source File: PathMatchingUrlHandlerMappingTests.java From java-technology-stack with MIT License | 5 votes |
@Before public void setUp() throws Exception { MockServletContext sc = new MockServletContext(""); wac = new XmlWebApplicationContext(); wac.setServletContext(sc); wac.setConfigLocations(new String[] {CONF}); wac.refresh(); hm = (HandlerMapping) wac.getBean("urlMapping"); }
Example 15
Source File: MvcNamespaceTests.java From spring-analysis-note with MIT License | 5 votes |
@Before public void setup() throws Exception { TestMockServletContext servletContext = new TestMockServletContext(); appContext = new XmlWebApplicationContext(); appContext.setServletContext(servletContext); LocaleContextHolder.setLocale(Locale.US); String attributeName = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE; appContext.getServletContext().setAttribute(attributeName, appContext); handler = new TestController(); handlerMethod = new InvocableHandlerMethod(handler, TestController.class.getMethod("testBind", Date.class, Double.class, TestBean.class, BindingResult.class)); }
Example 16
Source File: BeanNameUrlHandlerMappingTests.java From spring-analysis-note with MIT License | 5 votes |
@Before public void setUp() throws Exception { MockServletContext sc = new MockServletContext(""); wac = new XmlWebApplicationContext(); wac.setServletContext(sc); wac.setConfigLocations(new String[] {CONF}); wac.refresh(); }
Example 17
Source File: PathMatchingUrlHandlerMappingTests.java From spring-analysis-note with MIT License | 5 votes |
@Before public void setUp() throws Exception { MockServletContext sc = new MockServletContext(""); wac = new XmlWebApplicationContext(); wac.setServletContext(sc); wac.setConfigLocations(new String[] {CONF}); wac.refresh(); hm = (HandlerMapping) wac.getBean("urlMapping"); }
Example 18
Source File: MockServletContextWebContextLoader.java From olat with Apache License 2.0 | 4 votes |
@Override public final XmlWebApplicationContext loadContext(final String... locations) throws Exception { System.out.println("Loading ApplicationContext for locations [" + StringUtils.arrayToCommaDelimitedString(locations) + "]."); final XmlWebApplicationContext appContext = new XmlWebApplicationContext(); final MockServletContext servletContext = new MockServletContext(); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appContext); appContext.setServletContext(servletContext); appContext.setConfigLocations(locations); appContext.refresh(); appContext.registerShutdownHook(); return appContext; }
Example 19
Source File: KualiInitializeListener.java From rice with Educational Community License v2.0 | 4 votes |
/** * ServletContextListener interface implementation that schedules the start of the lifecycle */ @Override public void contextInitialized(ServletContextEvent sce) { long startInit = System.currentTimeMillis(); LOG.info("Initializing Kuali Rice Application..."); // Stop Quartz from "phoning home" on every startup System.setProperty("org.terracotta.quartz.skipUpdateCheck", "true"); List<String> configLocations = new ArrayList<String>(); String additionalConfigLocations = System.getProperty(KewApiConstants.ADDITIONAL_CONFIG_LOCATIONS_PARAM); if (!StringUtils.isBlank(additionalConfigLocations)) { String[] additionalConfigLocationArray = additionalConfigLocations.split(","); for (String additionalConfigLocation : additionalConfigLocationArray) { configLocations.add(additionalConfigLocation); } } String bootstrapSpringBeans = ""; if (!StringUtils.isBlank(System.getProperty(WEB_BOOTSTRAP_SPRING_FILE))) { bootstrapSpringBeans = System.getProperty(WEB_BOOTSTRAP_SPRING_FILE); } else if (!StringUtils.isBlank(sce.getServletContext().getInitParameter(WEB_BOOTSTRAP_SPRING_FILE))) { String bootstrapSpringInitParam = sce.getServletContext().getInitParameter(WEB_BOOTSTRAP_SPRING_FILE); // if the value comes through as ${bootstrap.spring.beans}, we ignore it if (!DEFAULT_SPRING_BEANS_REPLACEMENT_VALUE.equals(bootstrapSpringInitParam)) { bootstrapSpringBeans = bootstrapSpringInitParam; LOG.info("Found bootstrap Spring Beans file defined in servlet context: " + bootstrapSpringBeans); } } Properties baseProps = new Properties(); baseProps.putAll(getContextParameters(sce.getServletContext())); baseProps.putAll(System.getProperties()); JAXBConfigImpl config = new JAXBConfigImpl(baseProps); ConfigContext.init(config); context = new XmlWebApplicationContext(); if (!StringUtils.isEmpty(bootstrapSpringBeans)) { context.setConfigLocation(bootstrapSpringBeans); } context.setServletContext(sce.getServletContext()); // Provide an optional method for bootstrapping a Spring property source Optional<PropertySource<?>> ps = PropertySources.getPropertySource(sce, "web.bootstrap.spring.psc"); if (ps.isPresent()) { PropertySources.addFirst(context, ps.get()); } try { context.refresh(); } catch (RuntimeException e) { LOG.error("problem during context.refresh()", e); throw e; } context.start(); long endInit = System.currentTimeMillis(); LOG.info("...Kuali Rice Application successfully initialized, startup took " + (endInit - startInit) + " ms."); }
Example 20
Source File: MockServletContextWebContextLoader.java From olat with Apache License 2.0 | 4 votes |
@Override public final XmlWebApplicationContext loadContext(final String... locations) throws Exception { System.out.println("Loading ApplicationContext for locations [" + StringUtils.arrayToCommaDelimitedString(locations) + "]."); final XmlWebApplicationContext appContext = new XmlWebApplicationContext(); final MockServletContext servletContext = new MockServletContext(); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appContext); appContext.setServletContext(servletContext); appContext.setConfigLocations(locations); appContext.refresh(); appContext.registerShutdownHook(); return appContext; }