Java Code Examples for org.springframework.web.context.support.StaticWebApplicationContext#registerSingleton()

The following examples show how to use org.springframework.web.context.support.StaticWebApplicationContext#registerSingleton() . 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: DelegatingFilterProxyTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithTargetBeanName() throws ServletException, IOException {
	MockServletContext sc = new MockServletContext();

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("targetFilter");
	filterProxy.init(new MockFilterConfig(sc));

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example 2
Source File: ResponseEntityExceptionHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void controllerAdviceWithNestedExceptionWithinDispatcherServlet() throws Exception {
	StaticWebApplicationContext ctx = new StaticWebApplicationContext();
	ctx.registerSingleton("controller", NestedExceptionThrowingController.class);
	ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
	ctx.refresh();

	DispatcherServlet servlet = new DispatcherServlet(ctx);
	servlet.init(new MockServletConfig());
	try {
		servlet.service(this.servletRequest, this.servletResponse);
	}
	catch (ServletException ex) {
		assertTrue(ex.getCause() instanceof IllegalStateException);
		assertTrue(ex.getCause().getCause() instanceof ServletRequestBindingException);
	}
}
 
Example 3
Source File: RequestMappingHandlerAdapterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void modelAttributeAdviceInParentContext() throws Exception {
	StaticWebApplicationContext parent = new StaticWebApplicationContext();
	parent.registerSingleton("maa", ModelAttributeAdvice.class);
	parent.refresh();
	this.webAppContext.setParent(parent);
	this.webAppContext.refresh();

	HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handle");
	this.handlerAdapter.afterPropertiesSet();
	ModelAndView mav = this.handlerAdapter.handle(this.request, this.response, handlerMethod);

	assertEquals("lAttr1", mav.getModel().get("attr1"));
	assertEquals("gAttr2", mav.getModel().get("attr2"));
}
 
Example 4
Source File: RequestMappingHandlerAdapterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void modelAttributeAdviceInParentContext() throws Exception {
	StaticWebApplicationContext parent = new StaticWebApplicationContext();
	parent.registerSingleton("maa", ModelAttributeAdvice.class);
	parent.refresh();
	this.webAppContext.setParent(parent);
	this.webAppContext.refresh();

	HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handle");
	this.handlerAdapter.afterPropertiesSet();
	ModelAndView mav = this.handlerAdapter.handle(this.request, this.response, handlerMethod);

	assertEquals("lAttr1", mav.getModel().get("attr1"));
	assertEquals("gAttr2", mav.getModel().get("attr2"));
}
 
Example 5
Source File: DelegatingFilterProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithTargetFilterLifecycle() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	MockFilterConfig proxyConfig = new MockFilterConfig(sc);
	proxyConfig.addInitParameter("targetBeanName", "targetFilter");
	proxyConfig.addInitParameter("targetFilterLifecycle", "true");
	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
	filterProxy.init(proxyConfig);
	assertEquals(proxyConfig, targetFilter.filterConfig);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertEquals(proxyConfig, targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example 6
Source File: DispatcherServletTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void detectHandlerMappingFromParent() throws ServletException, IOException {
	// create a parent context that includes a mapping
	StaticWebApplicationContext parent = new StaticWebApplicationContext();
	parent.setServletContext(getServletContext());
	parent.registerSingleton("parentHandler", ControllerFromParent.class, new MutablePropertyValues());

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("mappings", URL_KNOWN_ONLY_PARENT + "=parentHandler"));

	parent.registerSingleton("parentMapping", SimpleUrlHandlerMapping.class, pvs);
	parent.refresh();

	DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
	// will have parent
	complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
	complexDispatcherServlet.setNamespace("test");

	ServletConfig config = new MockServletConfig(getServletContext(), "complex");
	config.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, parent);
	complexDispatcherServlet.init(config);

	MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", URL_KNOWN_ONLY_PARENT);
	MockHttpServletResponse response = new MockHttpServletResponse();
	complexDispatcherServlet.service(request, response);

	assertFalse("Matched through parent controller/handler pair: not response=" + response.getStatus(),
			response.getStatus() == HttpServletResponse.SC_NOT_FOUND);
}
 
Example 7
Source File: DelegatingFilterProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testDelegatingFilterProxy() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	MockFilterConfig proxyConfig = new MockFilterConfig(sc);
	proxyConfig.addInitParameter("targetBeanName", "targetFilter");
	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
	filterProxy.init(proxyConfig);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example 8
Source File: DispatcherServletTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void detectHandlerMappingFromParent() throws ServletException, IOException {
	// create a parent context that includes a mapping
	StaticWebApplicationContext parent = new StaticWebApplicationContext();
	parent.setServletContext(getServletContext());
	parent.registerSingleton("parentHandler", ControllerFromParent.class, new MutablePropertyValues());

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("mappings", URL_KNOWN_ONLY_PARENT + "=parentHandler"));

	parent.registerSingleton("parentMapping", SimpleUrlHandlerMapping.class, pvs);
	parent.refresh();

	DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
	// will have parent
	complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
	complexDispatcherServlet.setNamespace("test");

	ServletConfig config = new MockServletConfig(getServletContext(), "complex");
	config.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, parent);
	complexDispatcherServlet.init(config);

	MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", URL_KNOWN_ONLY_PARENT);
	MockHttpServletResponse response = new MockHttpServletResponse();
	complexDispatcherServlet.service(request, response);

	assertFalse("Matched through parent controller/handler pair: not response=" + response.getStatus(),
			response.getStatus() == HttpServletResponse.SC_NOT_FOUND);
}
 
Example 9
Source File: HandlerMappingIntrospectorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void detectHandlerMappings() throws Exception {
	StaticWebApplicationContext cxt = new StaticWebApplicationContext();
	cxt.registerSingleton("hmA", SimpleUrlHandlerMapping.class);
	cxt.registerSingleton("hmB", SimpleUrlHandlerMapping.class);
	cxt.registerSingleton("hmC", SimpleUrlHandlerMapping.class);
	cxt.refresh();

	List<?> expected = Arrays.asList(cxt.getBean("hmA"), cxt.getBean("hmB"), cxt.getBean("hmC"));
	List<HandlerMapping> actual = getIntrospector(cxt).getHandlerMappings();

	assertEquals(expected, actual);
}
 
Example 10
Source File: ViewResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithSpecificContextBeans() throws Exception {
	MockServletContext sc = new MockServletContext();
	final StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.registerSingleton("myBean", TestBean.class);
	wac.registerSingleton("myBean2", TestBean.class);
	wac.setServletContext(sc);
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	Properties props = new Properties();
	props.setProperty("key1", "value1");
	vr.setAttributes(props);
	Map map = new HashMap();
	map.put("key2", new Integer(2));
	vr.setAttributesMap(map);
	vr.setExposedContextBeanNames(new String[] {"myBean2"});
	vr.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest(sc) {
		@Override
		public RequestDispatcher getRequestDispatcher(String path) {
			return new MockRequestDispatcher(path) {
				@Override
				public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) {
					assertTrue("Correct rc attribute", forwardRequest.getAttribute("rc") == null);
					assertEquals("value1", forwardRequest.getAttribute("key1"));
					assertEquals(new Integer(2), forwardRequest.getAttribute("key2"));
					assertNull(forwardRequest.getAttribute("myBean"));
					assertSame(wac.getBean("myBean2"), forwardRequest.getAttribute("myBean2"));
				}
			};
		}
	};
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	View view = vr.resolveViewName("example1", Locale.getDefault());
	view.render(new HashMap(), request, response);
}
 
Example 11
Source File: ViewResolverRegistryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() {
	StaticWebApplicationContext context = new StaticWebApplicationContext();
	context.registerSingleton("freeMarkerConfigurer", FreeMarkerConfigurer.class);
	context.registerSingleton("scriptTemplateConfigurer", ScriptTemplateConfigurer.class);
	this.registry = new ViewResolverRegistry(context);
}
 
Example 12
Source File: HandlerMappingIntrospectorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void getMatchable() throws Exception {
	MutablePropertyValues pvs = new MutablePropertyValues(
			Collections.singletonMap("urlMap", Collections.singletonMap("/path", new Object())));

	StaticWebApplicationContext cxt = new StaticWebApplicationContext();
	cxt.registerSingleton("hm", SimpleUrlHandlerMapping.class, pvs);
	cxt.refresh();

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path");
	MatchableHandlerMapping hm = getIntrospector(cxt).getMatchableHandlerMapping(request);

	assertEquals(cxt.getBean("hm"), hm);
	assertNull("Attributes changes not ignored", request.getAttribute(BEST_MATCHING_PATTERN_ATTRIBUTE));
}
 
Example 13
Source File: ResponseEntityExceptionHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void controllerAdviceWithNestedException() {
	StaticWebApplicationContext ctx = new StaticWebApplicationContext();
	ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
	ctx.refresh();

	ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver();
	resolver.setApplicationContext(ctx);
	resolver.afterPropertiesSet();

	IllegalStateException ex = new IllegalStateException(new ServletRequestBindingException("message"));
	assertNull(resolver.resolveException(this.servletRequest, this.servletResponse, null, ex));
}
 
Example 14
Source File: ResponseEntityExceptionHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void controllerAdviceWithinDispatcherServlet() throws Exception {
	StaticWebApplicationContext ctx = new StaticWebApplicationContext();
	ctx.registerSingleton("controller", ExceptionThrowingController.class);
	ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
	ctx.refresh();

	DispatcherServlet servlet = new DispatcherServlet(ctx);
	servlet.init(new MockServletConfig());
	servlet.service(this.servletRequest, this.servletResponse);

	assertEquals(400, this.servletResponse.getStatus());
	assertEquals("error content", this.servletResponse.getContentAsString());
	assertEquals("someHeaderValue", this.servletResponse.getHeader("someHeader"));
}
 
Example 15
Source File: DelegatingFilterProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testDelegatingFilterProxyNotInjectedWithRootPreferred() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.refresh();
	sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);
	sc.setAttribute("another", wac);

	StaticWebApplicationContext wacToUse = new StaticWebApplicationContext();
	wacToUse.setServletContext(sc);
	String beanName = "targetFilter";
	wacToUse.registerSingleton(beanName, MockFilter.class);
	wacToUse.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wacToUse);

	MockFilter targetFilter = (MockFilter) wacToUse.getBean(beanName);

	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(beanName);
	filterProxy.setServletContext(sc);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example 16
Source File: DelegatingFilterProxyTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testDelegatingFilterProxyWithLazyContextStartup() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();

	MockFilterConfig proxyConfig = new MockFilterConfig(sc);
	proxyConfig.addInitParameter("targetBeanName", "targetFilter");
	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
	filterProxy.init(proxyConfig);

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example 17
Source File: ViewResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithSpecificContextBeans() throws Exception {
	MockServletContext sc = new MockServletContext();
	final StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.registerSingleton("myBean", TestBean.class);
	wac.registerSingleton("myBean2", TestBean.class);
	wac.setServletContext(sc);
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	Properties props = new Properties();
	props.setProperty("key1", "value1");
	vr.setAttributes(props);
	Map<String, Object> map = new HashMap<>();
	map.put("key2", new Integer(2));
	vr.setAttributesMap(map);
	vr.setExposedContextBeanNames(new String[] {"myBean2"});
	vr.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest(sc) {
		@Override
		public RequestDispatcher getRequestDispatcher(String path) {
			return new MockRequestDispatcher(path) {
				@Override
				public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) {
					assertTrue("Correct rc attribute", forwardRequest.getAttribute("rc") == null);
					assertEquals("value1", forwardRequest.getAttribute("key1"));
					assertEquals(new Integer(2), forwardRequest.getAttribute("key2"));
					assertNull(forwardRequest.getAttribute("myBean"));
					assertSame(wac.getBean("myBean2"), forwardRequest.getAttribute("myBean2"));
				}
			};
		}
	};
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	View view = vr.resolveViewName("example1", Locale.getDefault());
	view.render(new HashMap<String, Object>(), request, response);
}
 
Example 18
Source File: DelegatingFilterProxyTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testDelegatingFilterProxyNotInjectedWacServletAttrPreferred()
		throws ServletException, IOException {

	ServletContext sc = new MockServletContext();
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.refresh();
	sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);

	StaticWebApplicationContext wacToUse = new StaticWebApplicationContext();
	wacToUse.setServletContext(sc);
	String beanName = "targetFilter";
	String attrName = "customAttrName";
	wacToUse.registerSingleton(beanName, MockFilter.class);
	wacToUse.refresh();
	sc.setAttribute(attrName, wacToUse);

	MockFilter targetFilter = (MockFilter) wacToUse.getBean(beanName);

	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(beanName);
	filterProxy.setContextAttribute(attrName);
	filterProxy.setServletContext(sc);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example 19
Source File: DelegatingFilterProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testDelegatingFilterProxyAndCustomContextAttribute() throws ServletException, IOException {
	ServletContext sc = new MockServletContext();

	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.registerSingleton("targetFilter", MockFilter.class);
	wac.refresh();
	sc.setAttribute("CUSTOM_ATTR", wac);

	MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");

	MockFilterConfig proxyConfig = new MockFilterConfig(sc);
	proxyConfig.addInitParameter("targetBeanName", "targetFilter");
	proxyConfig.addInitParameter("contextAttribute", "CUSTOM_ATTR");
	DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
	filterProxy.init(proxyConfig);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	filterProxy.doFilter(request, response, null);

	assertNull(targetFilter.filterConfig);
	assertEquals(Boolean.TRUE, request.getAttribute("called"));

	filterProxy.destroy();
	assertNull(targetFilter.filterConfig);
}
 
Example 20
Source File: DispatcherServletInitializerTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected WebApplicationContext createServletApplicationContext() {
	StaticWebApplicationContext servletContext = new StaticWebApplicationContext();
	servletContext.registerSingleton("bean", MyBean.class);
	return servletContext;
}