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

The following are top voted examples for showing how to use org.springframework.web.context.support.XmlWebApplicationContext. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: springboot-shiro-cas-mybatis   File: WiringTests.java   View source code 7 votes vote down vote up
@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 2
Project: springboot-shiro-cas-mybatis   File: WiringTests.java   View source code 6 votes vote down vote up
@Before
public void setUp() {
    applicationContext = new XmlWebApplicationContext();
    applicationContext.setConfigLocations(
            "classpath:/webappContext.xml",
            "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 3
Project: springboot-shiro-cas-mybatis   File: WiringTests.java   View source code 6 votes vote down vote up
@Before
public void setUp() {
    applicationContext = new XmlWebApplicationContext();
    applicationContext.setConfigLocations(
            "classpath:/cas-management-servlet.xml",
            "classpath:/managementConfigContext.xml",
            "classpath:/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 WiringTests.class.getClassLoader();
        }
    }));
    applicationContext.refresh();
}
 
Example 4
Project: springboot-shiro-cas-mybatis   File: WiringTests.java   View source code 6 votes vote down vote up
@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 5
Project: cas4.0.x-server-wechat   File: WiringTests.java   View source code 6 votes vote down vote up
@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 6
Project: cas4.0.x-server-wechat   File: WiringTests.java   View source code 6 votes vote down vote up
@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
Project: cas-server-4.2.1   File: WiringTests.java   View source code 6 votes vote down vote up
@Before
public void setUp() {
    applicationContext = new XmlWebApplicationContext();
    applicationContext.setConfigLocations(
            "classpath:/webappContext.xml",
            "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
Project: cas-server-4.2.1   File: WiringTests.java   View source code 6 votes vote down vote up
@Before
public void setUp() {
    applicationContext = new XmlWebApplicationContext();
    applicationContext.setConfigLocations(
            "classpath:/cas-management-servlet.xml",
            "classpath:/managementConfigContext.xml",
            "classpath:/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 WiringTests.class.getClassLoader();
        }
    }));
    applicationContext.refresh();
}
 
Example 9
Project: spring4-understanding   File: SimpleUrlHandlerMappingTests.java   View source code 6 votes vote down vote up
@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 10
Project: spring4-understanding   File: XmlWebApplicationContextTests.java   View source code 6 votes vote down vote up
@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
Project: spring4-understanding   File: ContextLoaderTests.java   View source code 6 votes vote down vote up
@Test
public void testContextLoaderListenerWithDefaultContext() {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
			"/org/springframework/web/context/WEB-INF/applicationContext.xml " +
			"/org/springframework/web/context/WEB-INF/context-addition.xml");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	listener.contextInitialized(event);
	WebApplicationContext context = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
	assertTrue("Correct WebApplicationContext exposed in ServletContext", context instanceof XmlWebApplicationContext);
	assertTrue(WebApplicationContextUtils.getRequiredWebApplicationContext(sc) instanceof XmlWebApplicationContext);
	LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle");
	assertTrue("Has father", context.containsBean("father"));
	assertTrue("Has rod", context.containsBean("rod"));
	assertTrue("Has kerry", context.containsBean("kerry"));
	assertTrue("Not destroyed", !lb.isDestroyed());
	assertFalse(context.containsBean("beans1.bean1"));
	assertFalse(context.containsBean("beans1.bean2"));
	listener.contextDestroyed(event);
	assertTrue("Destroyed", lb.isDestroyed());
	assertNull(sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
	assertNull(WebApplicationContextUtils.getWebApplicationContext(sc));
}
 
Example 12
Project: quartzdesk-executor   File: SpringProfilesActivator.java   View source code 6 votes vote down vote up
@Override
public void initialize( XmlWebApplicationContext applicationContext )
{
  ConfigurableEnvironment env = applicationContext.getEnvironment();

  WorkDir workDir;
  try
  {
    workDir = new WorkDir( applicationContext.getServletContext() );
  }
  catch ( IOException e )
  {
    throw new ApplicationContextException( "Error obtaining QuartzDesk Executor work directory.", e );
  }

  String databaseProfile = getDatabaseProfile( workDir );

  String[] activeProfiles = new String[] { databaseProfile };

  log.info( "Activating Spring profiles: {}", Arrays.toString( activeProfiles ) );

  env.setActiveProfiles( activeProfiles );
}
 
Example 13
Project: aws-serverless-java-container   File: LambdaHandler.java   View source code 6 votes vote down vote up
public AwsProxyResponse handleRequest(AwsProxyRequest awsProxyRequest, Context context)
{
     if (!isinitialized) {
         isinitialized = true;
         try {
         	XmlWebApplicationContext wc = new XmlWebApplicationContext();
         	wc.setConfigLocation("classpath:/staticAppContext.xml");
             handler = SpringLambdaContainerHandler.getAwsProxyHandler(wc);
         } catch (ContainerInitializationException e) {
             e.printStackTrace();
             return null;
         }
     }
     AwsProxyResponse res = handler.proxy(awsProxyRequest, context);
     return res;
}
 
Example 14
Project: cas-server-webapp   File: WiringTests.java   View source code 6 votes vote down vote up
@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 15
Project: nbone   File: SpringWebApplicationInitializer.java   View source code 6 votes vote down vote up
/**
* <p>默认关闭<p>
* 加载spring mvc 统一调度器
* @param servletContext
*/
  protected void initDispatcherServlet(ServletContext servletContext){
  	String enable  = servletContext.getInitParameter("enableDispatcherServlet");
  	if(BooleanUtils.valueOf(enable)){
  		//classpath*:/spring-mvc*.xml,/WEB-INF/spring-mvc*.xml
  		XmlWebApplicationContext appContext = new XmlWebApplicationContext();
  		appContext.setConfigLocation("classpath*:/spring/spring-mvc*.xml,/WEB-INF/spring/spring-mvc*.xml");
  		
  		
  		ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(appContext));
  		dispatcher.setLoadOnStartup(1);
  		dispatcher.addMapping("/");
  		
  	}

  }
 
Example 16
Project: cas4.1.9   File: WiringTests.java   View source code 6 votes vote down vote up
@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 17
Project: cas4.1.9   File: WiringTests.java   View source code 6 votes vote down vote up
@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 18
Project: communote-server   File: DispatcherServletInitializer.java   View source code 6 votes vote down vote up
private void createMainDispatcherServlet(ApplicationContext rootContext) {
    mainWebApplicationContext = new XmlWebApplicationContext();
    mainWebApplicationContext
            .setConfigLocation(getRequiredInitParameter("communoteWebContextConfigLocation"));
    mainWebApplicationContext.setParent(rootContext);
    // add ContextLoaderListener with web-ApplicationContext which publishes it under a
    // ServletContext attribute and closes it on shutdown. The former is required for
    // WebApplicationContextUtils which are used by DelegatingFilterProxy (spring security).
    // Closing is also done by dispatcher servlet's implementation of destroy method.
    this.servletContext.addListener(new ContextLoaderListener(mainWebApplicationContext));
    DispatcherServlet dispatcherServlet = new DispatcherServlet(mainWebApplicationContext);
    ServletRegistration.Dynamic addedServlet = this.servletContext.addServlet(
            getInitParameter("communoteServletName", "communote"), dispatcherServlet);
    addedServlet.setLoadOnStartup(1);
    addedServlet.addMapping(getRequiredInitParameter("communoteServletUrlPattern"));
    this.mainDispatcherServlet = dispatcherServlet;
}
 
Example 19
Project: kc-rice   File: SpringResourceLoader.java   View source code 6 votes vote down vote up
@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 20
Project: cas-4.0.1   File: WiringTests.java   View source code 6 votes vote down vote up
@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 21
Project: cas-4.0.1   File: WiringTests.java   View source code 6 votes vote down vote up
@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 22
Project: openmeetings   File: InstallWizard.java   View source code 6 votes vote down vote up
@Override
protected void onSubmit() {
	try {
		ConnectionPropertiesPatcher.patch(getModelObject());
		XmlWebApplicationContext ctx = (XmlWebApplicationContext)getWebApplicationContext(Application.get().getServletContext());
		if (ctx == null) {
			form.error(new StringResourceModel("install.wizard.db.step.error.patch", InstallWizard.this).setParameters("Web context is NULL").getObject());
			log.error("Web context is NULL");
			return;
		}
		LocalEntityManagerFactoryBean emb = ctx.getBeanFactory().getBean(LocalEntityManagerFactoryBean.class);
		emb.afterPropertiesSet();
		dbType = getModelObject().getDbType();
	} catch (Exception e) {
		form.error(new StringResourceModel("install.wizard.db.step.error.patch", InstallWizard.this).setParameters(e.getMessage()).getObject());
		log.error("error while patching", e);
	}
}
 
Example 23
Project: p00   File: WiringTests.java   View source code 6 votes vote down vote up
@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 24
Project: p00   File: WiringTests.java   View source code 6 votes vote down vote up
@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 25
Project: cas-server-4.0.1   File: WiringTests.java   View source code 6 votes vote down vote up
@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 26
Project: cas-server-4.0.1   File: WiringTests.java   View source code 6 votes vote down vote up
@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 27
Project: starter-kit-spring-maven   File: ServletInitializer.java   View source code 6 votes vote down vote up
@Override
public void onStartup(ServletContext container) throws ServletException {
    XmlWebApplicationContext context = new XmlWebApplicationContext();

    context.setConfigLocations(CONTEXT_CONFIG_LOCATIONS);

    DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
    dispatcherServlet.setPublishContext(true);

    FilterRegistration.Dynamic sessionFilter = container.addFilter("sessionFilter", new DelegatingFilterProxy("sessionFilter"));

    sessionFilter.addMappingForUrlPatterns(null, false, "*.jsp");

    this.servlets.stream().forEach( servlet -> {
        addServlet(container, sessionFilter, servlet);
    });
}
 
Example 28
Project: gwt-sl   File: HandlerTest.java   View source code 6 votes vote down vote up
@Override
@Before
public void setUp() throws Exception {
	super.setUp();
	ServletContext servletContext = new MockServletContext(
			new FileSystemResourceLoader());
	requestService = new MockHttpServletRequest("PUT", "/service");
	applicationContext = new XmlWebApplicationContext();
	applicationContext.setServletContext(servletContext);
	applicationContext.setConfigLocations(new String[] {
			"src/main/webapp/WEB-INF/handler-servlet.xml",
			"src/main/webapp/WEB-INF/applicationContext.xml" });
	try{
	applicationContext.refresh();
	}
	catch (Throwable e){
		e.printStackTrace();
	}
	handler = (GWTHandler) applicationContext.getBean("urlMapping", GWTHandler.class);
}
 
Example 29
Project: gwt-sl   File: TestRPCExporter.java   View source code 6 votes vote down vote up
@Override
@Before
public void setUp() throws Exception {

	requestPayload = readResource("request_add.txt");
	responsePayload = readResource("response_add.txt");
	MockServletContext servletContext = new MockServletContext(new ResourceLoaderSupport());
	MockServletConfig servletConfig = new MockServletConfig(servletContext);
	applicationContext = new XmlWebApplicationContext();
	applicationContext.setServletContext(servletContext);
	applicationContext.setServletConfig(servletConfig);
	applicationContext.setConfigLocations(new String[] { "src/main/webapp/WEB-INF/exporter-servlet.xml",
			"src/main/webapp/WEB-INF/applicationContext.xml" });
	applicationContext.refresh();
	handlerMapping = (SimpleUrlHandlerMapping) applicationContext
			.getBean("org.springframework.web.servlet.handler.SimpleUrlHandlerMapping");
}
 
Example 30
Project: class-guard   File: SimpleUrlHandlerMappingTests.java   View source code 6 votes vote down vote up
@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 31
Project: class-guard   File: XmlWebApplicationContextTests.java   View source code 6 votes vote down vote up
public void testWithoutMessageSource() 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 32
Project: class-guard   File: ContextLoaderTests.java   View source code 6 votes vote down vote up
@Test
public void testContextLoaderListenerWithDefaultContext() {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
			"/org/springframework/web/context/WEB-INF/applicationContext.xml " +
			"/org/springframework/web/context/WEB-INF/context-addition.xml");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	listener.contextInitialized(event);
	WebApplicationContext context = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
	assertTrue("Correct WebApplicationContext exposed in ServletContext", context instanceof XmlWebApplicationContext);
	assertTrue(WebApplicationContextUtils.getRequiredWebApplicationContext(sc) instanceof XmlWebApplicationContext);
	LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle");
	assertTrue("Has father", context.containsBean("father"));
	assertTrue("Has rod", context.containsBean("rod"));
	assertTrue("Has kerry", context.containsBean("kerry"));
	assertTrue("Not destroyed", !lb.isDestroyed());
	assertFalse(context.containsBean("beans1.bean1"));
	assertFalse(context.containsBean("beans1.bean2"));
	listener.contextDestroyed(event);
	assertTrue("Destroyed", lb.isDestroyed());
	assertNull(sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
	assertNull(WebApplicationContextUtils.getWebApplicationContext(sc));
}
 
Example 33
Project: rice   File: SpringResourceLoader.java   View source code 6 votes vote down vote up
@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 34
Project: rsine   File: Rsine.java   View source code 6 votes vote down vote up
private static Server startServer() throws Exception {
    Server server = new Server(cmdParams.port);

    XmlWebApplicationContext context = new XmlWebApplicationContext();
    context.setConfigLocation("classpath:application-context.xml");

    ServletContextHandler contextHandler = new ServletContextHandler();
    contextHandler.setContextPath("/");

    contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), "/*");
    contextHandler.addEventListener(new ContextLoaderListener(context));
    server.setHandler(contextHandler);

    server.start();
    return server;
}
 
Example 35
Project: kuali_rice   File: SpringResourceLoader.java   View source code 6 votes vote down vote up
@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 36
Project: dhus-core   File: TestContextLoader.java   View source code 5 votes vote down vote up
@Override
public ApplicationContext loadContext (String... locations) throws Exception
{
   XmlWebApplicationContext ctx = new XmlWebApplicationContext ();
   ctx.setConfigLocations (locations);
   ctx.getEnvironment ().setActiveProfiles ("test");
   ctx.refresh ();
   AnnotationConfigUtils.registerAnnotationConfigProcessors (
      (BeanDefinitionRegistry) ctx.getBeanFactory ());
   ctx.registerShutdownHook ();
   return ctx;
}
 
Example 37
Project: OperatieBRP   File: BrpApplicatieStartListener.java   View source code 5 votes vote down vote up
@Override
public final void onApplicationEvent(final ApplicationContextEvent event) {
    try {
        final ApplicationContext context = event.getApplicationContext();
        if (context instanceof XmlWebApplicationContext) {
            final XmlWebApplicationContext ctx = (XmlWebApplicationContext) context;
            String webContextName = ctx.getServletContext().getServletContextName();
            if (webContextName.contains("/")) {
                webContextName = webContextName.substring(webContextName.indexOf('/'));
            }
            ctx.setDisplayName(webContextName);
            MDC.put(LeveringVeld.MDC_APPLICATIE_NAAM, webContextName).close();
        }
        LOGGER.debug("Context event '{}'; id='{}', displayName='{}'", event.getClass().getSimpleName(), event
                .getApplicationContext().getId(), event.getApplicationContext().getDisplayName());

        if (event instanceof ContextClosedEvent) {
            LOGGER.info("==> Context voor applicatie '{}' is gestopt", event.getApplicationContext().getId());
        }
        /* Wanneer een applicatie is gestart, dan wordt een ContextRefreshedEvent gestuurt **/
        if (event instanceof ContextRefreshedEvent) {
            LOGGER.info("==> Context voor applicatie '{}' is gestart/herstart", event.getApplicationContext()
                    .getId());
            logJvmSettings();
        }
    } catch (final ApplicationContextException ex) {
        LOGGER.error("Fout bij het opstarten van de applicatiecontext bij event {}", event.toString(), ex);
    }


}
 
Example 38
Project: Webstore   File: AppInitializer.java   View source code 5 votes vote down vote up
@Override
public void onStartup(ServletContext container) throws ServletException {
    XmlWebApplicationContext appContext = new XmlWebApplicationContext();
    appContext.setConfigLocation("/WEB-INF/spring/webcontext/DispatcherServlet-context.xml");

    ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(appContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");
}
 
Example 39
Project: dwr   File: DwrControllerTest.java   View source code 5 votes vote down vote up
/**
 * Subclasses can invoke this to get a context key for the given location.
 * This doesn't affect the applicationContext instance variable in this class.
 * Dependency Injection cannot be applied from such contexts.
 */
//@Override
protected ConfigurableApplicationContext loadContextLocations(String[] locations)
{
    log.info("Loading my own config for: " + StringUtils.arrayToCommaDelimitedString(locations));

    XmlWebApplicationContext ctx = new XmlWebApplicationContext();
    ctx.setConfigLocations(locations);
    ctx.setServletContext(new MockServletContext());
    ctx.refresh();
    return ctx;
}
 
Example 40
Project: spring4-understanding   File: ControllerClassNameHandlerMappingTests.java   View source code 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	MockServletContext sc = new MockServletContext("");
	this.wac = new XmlWebApplicationContext();
	this.wac.setServletContext(sc);
	this.wac.setConfigLocations(new String[] {LOCATION});
	this.wac.refresh();
	this.hm = (HandlerMapping) this.wac.getBean("mapping");
	this.hm2 = (HandlerMapping) this.wac.getBean("mapping2");
	this.hm3 = (HandlerMapping) this.wac.getBean("mapping3");
	this.hm4 = (HandlerMapping) this.wac.getBean("mapping4");
}