Code example for XmlWebApplicationContext

Methods: getBean, refresh, setConfigLocation, setServletContext

0
public class SimpleBeanAccessTests { 
	 
	@Test 
	public void test() { 
 
		XmlWebApplicationContext wac = new XmlWebApplicationContext(); 
		wac.setConfigLocation("file:src/main/webapp/WEB-INF/spring/root-context.xml"); 
		wac.setServletContext(new MockServletContext()); 
		wac.refresh(); 
		 
		ServletWebRequest webRequest = new ServletWebRequest(new MockHttpServletRequest()); 
		RequestContextHolder.setRequestAttributes(webRequest); 
		 
		MyBeanClass myBeanClass = wac.getBean("myBeanClass", MyBeanClass.class); 
		assertNull(myBeanClass); 
		 
		MyBeanClass myProxiedBeanClass = wac.getBean("myProxiedBeanClass", MyBeanClass.class); 
		assertNotNull(myProxiedBeanClass); 
		assertEquals("foo", myProxiedBeanClass.getFoo());	// this will cause NPE 
 
		RequestContextHolder.resetRequestAttributes(); 
	}